Skip to content

Commit 1b740e5

Browse files
authored
Merge branch 'master' into add/pyproject
2 parents ab492ab + 532fd99 commit 1b740e5

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

AUTHORS.rst

+2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ Contributors
1414

1515
* Avraham Shukron <[email protected]>
1616
* Chris Targett <[email protected]>
17+
* Daniel Schiavini <[email protected]>
1718
* Dima Kuznetsov <[email protected]>
1819
* Hugo van Kemenade <[email protected]>
1920
* Jannis Leidel <[email protected]>
2021
* Johannes Garimort <[email protected]>
2122
* Omer Anson <[email protected]>
2223
* Pavel Lipchak <[email protected]>
24+
* Roberto Fernandez Diaz <[email protected]>
2325
* Vorren

HISTORY.rst

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
History
44
-------
55

6+
2.5.0 (2021-07-26)
7+
++++++++++++++++++
8+
9+
* Improvied error messages for field validation errors.
10+
* Allowed to validate non model list items.
11+
* Added DictField.
12+
613
2.4.1 (2021-02-19)
714
++++++++++++++++++
815

README.rst

+100-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,72 @@ Features
249249
"type": "object"
250250
}
251251
252-
For more information, please see topic about validation in documentation.
252+
You can also validate scalars, when needed:
253+
254+
.. code-block:: python
255+
256+
>>> class Person(models.Base):
257+
...
258+
... name = fields.StringField(
259+
... required=True,
260+
... validators=[
261+
... validators.Regex('^[A-Za-z]+$'),
262+
... validators.Length(3, 25),
263+
... ],
264+
... )
265+
... age = fields.IntField(
266+
... nullable=True,
267+
... validators=[
268+
... validators.Min(18),
269+
... validators.Max(101),
270+
... ]
271+
... )
272+
... nickname = fields.StringField(
273+
... required=True,
274+
... nullable=True
275+
... )
276+
...
277+
278+
>>> def only_odd_numbers(item):
279+
... if item % 2 != 1:
280+
... raise validators.ValidationError("Only odd numbers are accepted")
281+
...
282+
>>> class Person(models.Base):
283+
... lucky_numbers = fields.ListField(int, item_validators=[only_odd_numbers])
284+
... item_validator_str = fields.ListField(
285+
... str,
286+
... item_validators=[validators.Length(10, 20), validators.Regex(r"\w+")],
287+
... validators=[validators.Length(1, 2)],
288+
... )
289+
...
290+
>>> Person.to_json_schema()
291+
{
292+
"type": "object",
293+
"additionalProperties": false,
294+
"properties": {
295+
"item_validator_str": {
296+
"type": "array",
297+
"items": {
298+
"type": "string",
299+
"minLength": 10,
300+
"maxLength": 20,
301+
"pattern": "/\\w+/"
302+
},
303+
"minItems": 1,
304+
"maxItems": 2
305+
},
306+
"lucky_numbers": {
307+
"type": "array",
308+
"items": {
309+
"type": "number"
310+
}
311+
}
312+
}
313+
}
314+
315+
(Note that `only_odd_numbers` did not modify schema, since only class based validators are
316+
able to do that, though it will still work as expected in python. Use class based validators
317+
that can be expressed in json schema if you want to be 100% correct on schema side.)
253318

254319
* Lazy loading, best for circular references:
255320

@@ -331,6 +396,40 @@ Features
331396
}
332397
}
333398
399+
* Dealing with schemaless data
400+
401+
(Plese note that using schemaless fields can cause your models to get out of control - especially if
402+
you are the one responsible for data schema. On the other hand there is usually the case when incomming
403+
data are with no schema defined and schemaless fields are the way to go.)
404+
405+
.. code-block:: python
406+
407+
>>> class Event(models.Base):
408+
...
409+
... name = fields.StringField()
410+
... size = fields.FloatField()
411+
... extra = fields.DictField()
412+
413+
>>> Event.to_json_schema()
414+
{
415+
"type": "object",
416+
"additionalProperties": false,
417+
"properties": {
418+
"extra": {
419+
"type": "object"
420+
},
421+
"name": {
422+
"type": "string"
423+
},
424+
"size": {
425+
"type": "float"
426+
}
427+
}
428+
}
429+
430+
`DictField` allow to pass any dict of values (`"type": "object"`), but note, that it will not make any validation
431+
on values except for the dict type.
432+
334433
* Compare JSON schemas:
335434

336435
.. code-block:: python

jsonmodels/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
__author__ = 'Szczepan Cieślik'
22
__email__ = '[email protected]'
3-
__version__ = '2.4.1'
3+
__version__ = '2.5.0'

0 commit comments

Comments
 (0)