Skip to content

Commit

Permalink
Merge branch 'master' into add/pyproject
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Nov 13, 2021
2 parents ab492ab + 532fd99 commit 1b740e5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
2 changes: 2 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ Contributors

* Avraham Shukron <avraham.shukron@gmail.com>
* Chris Targett <chris.targett@xlevus.net>
* Daniel Schiavini <d.schiavini@ultimaker.com>
* Dima Kuznetsov <dima.kuznetsov@toganetworks.com>
* Hugo van Kemenade <hugovk@users.noreply.github.com>
* Jannis Leidel <jannis@leidel.info>
* Johannes Garimort <johannes.garimort@gmx.net>
* Omer Anson <omer.anson@toganetworks.com>
* Pavel Lipchak <kazemat92@gmail.com>
* Roberto Fernandez Diaz <robertofd1995@users.noreply.github.com>
* Vorren
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
History
-------

2.5.0 (2021-07-26)
++++++++++++++++++

* Improvied error messages for field validation errors.
* Allowed to validate non model list items.
* Added DictField.

2.4.1 (2021-02-19)
++++++++++++++++++

Expand Down
101 changes: 100 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,72 @@ Features
"type": "object"
}
For more information, please see topic about validation in documentation.
You can also validate scalars, when needed:

.. code-block:: python
>>> class Person(models.Base):
...
... name = fields.StringField(
... required=True,
... validators=[
... validators.Regex('^[A-Za-z]+$'),
... validators.Length(3, 25),
... ],
... )
... age = fields.IntField(
... nullable=True,
... validators=[
... validators.Min(18),
... validators.Max(101),
... ]
... )
... nickname = fields.StringField(
... required=True,
... nullable=True
... )
...
>>> def only_odd_numbers(item):
... if item % 2 != 1:
... raise validators.ValidationError("Only odd numbers are accepted")
...
>>> class Person(models.Base):
... lucky_numbers = fields.ListField(int, item_validators=[only_odd_numbers])
... item_validator_str = fields.ListField(
... str,
... item_validators=[validators.Length(10, 20), validators.Regex(r"\w+")],
... validators=[validators.Length(1, 2)],
... )
...
>>> Person.to_json_schema()
{
"type": "object",
"additionalProperties": false,
"properties": {
"item_validator_str": {
"type": "array",
"items": {
"type": "string",
"minLength": 10,
"maxLength": 20,
"pattern": "/\\w+/"
},
"minItems": 1,
"maxItems": 2
},
"lucky_numbers": {
"type": "array",
"items": {
"type": "number"
}
}
}
}
(Note that `only_odd_numbers` did not modify schema, since only class based validators are
able to do that, though it will still work as expected in python. Use class based validators
that can be expressed in json schema if you want to be 100% correct on schema side.)

* Lazy loading, best for circular references:

Expand Down Expand Up @@ -331,6 +396,40 @@ Features
}
}
* Dealing with schemaless data

(Plese note that using schemaless fields can cause your models to get out of control - especially if
you are the one responsible for data schema. On the other hand there is usually the case when incomming
data are with no schema defined and schemaless fields are the way to go.)

.. code-block:: python
>>> class Event(models.Base):
...
... name = fields.StringField()
... size = fields.FloatField()
... extra = fields.DictField()
>>> Event.to_json_schema()
{
"type": "object",
"additionalProperties": false,
"properties": {
"extra": {
"type": "object"
},
"name": {
"type": "string"
},
"size": {
"type": "float"
}
}
}
`DictField` allow to pass any dict of values (`"type": "object"`), but note, that it will not make any validation
on values except for the dict type.

* Compare JSON schemas:

.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion jsonmodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__author__ = 'Szczepan Cieślik'
__email__ = 'szczepan.cieslik@gmail.com'
__version__ = '2.4.1'
__version__ = '2.5.0'

0 comments on commit 1b740e5

Please sign in to comment.