Skip to content
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

Add maxlength minlength examples #509

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -55,6 +55,7 @@ Contributors
- Sergey Leshchenko
- Tobias Betz
- Trong Hieu HA
- Vipul Gupta
- Waldir Pimenta
vipulgupta2048 marked this conversation as resolved.
Show resolved Hide resolved
- calve
- gilbsgilbs
Expand Down
36 changes: 34 additions & 2 deletions docs/validation-rules.rst
Expand Up @@ -444,7 +444,23 @@ The assigned data can be of any type.
min, max
--------

Minimum and maximum value allowed for any types that implement comparison operators.
Minimum and maximum value allowed for any types that implement comparison operators. See
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proposal: "… implement the corresponding comparison method (__lt__ or __gt__)."

i think a concrete, simple example here would be better than the more complex ones that a referred.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposal seems fine, but not sure what are (__lt__ or __gt__), I can just add them if you want. I just felt that by comparison operators we are going for \*of-rules

Roger that, a simple example coming right up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these "dunder" (from double underscore) or also known as "magic" methods do implement these operations that the operators imply. so, maybe just "comparison operations" instead of "operator"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @funkyfuture for the explanation.

`\*of-rules`_ for examples.

.. doctest::

>>> schema = {'weight':{'type':'float', 'min': 10.1,'max':10.9}}
>>> document = {'weight': 10.3}

>>> v.validate(document, schema)
True

>>> document = {'weight': 12}
>>> v.validate(document, schema)
False

>>> v.errors
{'weight': ['max value is 10.9']}

.. versionchanged:: 1.0
Allows any type to be compared.
Expand All @@ -455,7 +471,23 @@ Minimum and maximum value allowed for any types that implement comparison operat
minlength, maxlength
--------------------

Minimum and maximum length allowed for iterables.
Minimum and maximum length allowed for any objects that implements __len__. Examples are as follows:

.. doctest::

>>> schema = {'numbers': {'type': 'list', 'minlength': 1, 'maxlength': 3}}
>>> document = {'numbers': [256, 2048, 23]}

>>> v.validate(document, schema)
True

>>> document = {'numbers': [256, 2048, 23, 2]}
>>> v.validate(document, schema)
False

>>> v.errors
{'numbers': ['max length is 3']}


noneof
------
Expand Down