-
Notifications
You must be signed in to change notification settings - Fork 43
Document the 'checks' module #3611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
5a29a4c
to
3b3a197
Compare
3b3a197
to
ff8866d
Compare
507d790
to
e9f2a64
Compare
ead4aa2
to
9f2005c
Compare
3e881a1
to
0334428
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM from my side.
The documentation seems really nice and useful. It would be good to add a documentation page link to the checks
repo README after merge.
Thanks, will make a PR after merging. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally OK, but some places seem hard to read; I suggested simpler/shorter wordings.
**Since:** :doc:`2.11.0 </release/2.11.0>` | ||
|
||
The ``checks`` module provides the ability to check the types of arguments passed to a Lua function. | ||
You need to call the :ref:`checks(type_1, ...) <checks-checks>` function inside the target Lua function and pass :ref:`one or more <checks_number_of_arguments>` type qualifiers to check the corresponding argument types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'd start with "to check a function's argument types, call ..." to be more goal-oriented. In the current wording, the path to reader's goal is too long.
- A :ref:`string type qualifier <checks_string_type_qualifier>` checks whether a function's argument conforms to the specified type. | ||
- A :ref:`table type qualifier <checks_table_type_qualifiers>` checks whether the values of a table passed as an argument conform to the specified types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add short examples directly in the text?
Loading checks | ||
-------------- | ||
|
||
The :ref:`checks API <checks_api_reference>` is available in a script without loading the module using ``require('checks')``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe say that require
is needed only if the module is installed from repo?
I actually don't get how our devs decide which modules need require
and which don't, but the explicit sentence "this module doesn't need require" looks a bit awkward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't get how our devs decide which modules need require and which don't
Someone just decides that's rawset(_G, 'checks', checks)
in the module is good idea five years ago and then no one breaks it since they don't have a reason to do so. Actually, in case of repo-installed checks
you also don't need to require it more than once. Once you have a require in any of your scripts, it is available in any code called later. It's just that built-in modules are required inside the core Tarantool code, so the first require already had happened for any application developer.
Actually, I prefer calling local checks = require('checks')
in any script, but, well, it is a fact that it is not actually needed. My original comment was related to checkers
-- it is available in global after first require('checks')
(which had happen already internally), but you cannot do require('checkers')
-- it is only in global.
Number of arguments to check | ||
---------------------------- | ||
|
||
Depending on the number of arguments to be checked, you can pass one or more type qualifiers to the :ref:`checks(type_1, ...) <checks-checks>` function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentence reads a bit too formal.
Maybe smth simpler, like: "For each argument to check, you need to specify its own type qualifier"?
:end-before: -- Tests | ||
:dedent: | ||
|
||
To turn off checking of specific arguments, use the :ref:`? placeholder <checks_any_type>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I get it right that ?
is for checking arbitrary subsets of arguments? For example, in f(a, b, c, d)
, check only a
and d
? If yes, then maybe show an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, skip checking
seems better to me here than turn off checking
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I get it right that
?
is for checking arbitrary subsets of arguments? For example, inf(a, b, c, d)
, check onlya
andd
? If yes, then maybe show an example?
Input count must match with checks expected count (excluding varargs).
tarantool> g = function(a, b) checks('number', 'number') end
---
...
tarantool> g(1, 2)
---
...
tarantool> g(1)
---
- error: '[string "g = function(a, b) checks(''number'', ''number'')..."]:1: bad argument
#2 to nil (number expected, got nil)'
...
tarantool> g = function(a, b) checks('number') end
---
...
tarantool> g(1, 2)
---
- error: '[string "g = function(a, b) checks(''number'') end"]:1: checks: argument
"b" is not checked'
...
tarantool> g = function(a, b) checks('number', '?') end
---
...
tarantool> g(1)
---
...
So, in case you want to check some args and you do not care about other args, you must check them with '?'
since you cannot simply skip them. It makes even more sence in case you don't want to check some argument in the middle. It is similar for table fields -- if you want to allow optional field X with arbitrary value, you use '?'
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I get it right that ? is for checking arbitrary subsets of arguments? For example, in f(a, b, c, d), check only a and d? If yes, then maybe show an example?
You are right. Will update the example to make it more clear.
- See also | ||
|
||
* - ``checks('datetime')`` | ||
- Check whether the specified value is :ref:`datetime_object <datetime_obj>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see much point in repeating the same sentence in all rows changing only the type (which is obvious from the first column). Can we restructure the table somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auto-generated lists of API members often look like this. I think, it's not a problem.
*************** | ||
|
||
A string type qualifier can accept the name of a custom function that performs arbitrary validations. | ||
To achieve this, the function should be added to the :ref:`checkers <checks-checkers>` table and should return ``true`` if the value is valid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rephrase to direct instruction: "write a function and add to the table"?
|
||
.. _checks_any_type: | ||
|
||
Turning off argument checking |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turn off > Skip?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure am I right or not but to me skip
has a slightly different meaning and suits better for cases when we have some kind of a loop. Here, we turn off checking of the specified argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it this way:
- by default, no args are checked - checking is turned off for all of them.
- turn off checking means return the default (no check) after turning on checking.
- but this text tells about the case when nobody turned on checking for these args :)
"skip" came to me because I see args as a sequence. So I read a check
call with a ?
like this: check arg1
, check arg2
, skip arg3
, check arg4
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I got this :) Will update
Table type qualifier | ||
-------------------- | ||
|
||
The type qualifier may be a table. In this case, the following checks are made: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This description wasn't enough for me to understand what this is used for. Maybe write that it's for cases when arguments are tables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with a sentence from intro.
|
||
.. NOTE:: | ||
|
||
A table type qualifier may be nested and use tables, too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Table qualifiers can be nested"?
069c74c
to
1147c98
Compare
1147c98
to
4f60474
Compare
Added a new page for the
checks
module:https://docs.d.tarantool.io/en/doc/embedded-checks/reference/reference_lua/checks/