From 8bffe9b606da6d596db9d635b6dae5d8dea1444a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szczepan=20Cie=C5=9Blik?= Date: Mon, 26 Jul 2021 20:34:04 +0200 Subject: [PATCH 1/2] Enhance docs --- README.rst | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index bbf75ef..1e6b539 100644 --- a/README.rst +++ b/README.rst @@ -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: @@ -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 From 8d8cd589bae97096f1a5652428741fe459d7f342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szczepan=20Cie=C5=9Blik?= Date: Mon, 26 Jul 2021 21:46:49 +0200 Subject: [PATCH 2/2] Version 2.5.0 --- AUTHORS.rst | 2 ++ HISTORY.rst | 7 +++++++ jsonmodels/__init__.py | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 05b405e..5f0991b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -14,10 +14,12 @@ Contributors * Avraham Shukron * Chris Targett +* Daniel Schiavini * Dima Kuznetsov * Hugo van Kemenade * Jannis Leidel * Johannes Garimort * Omer Anson * Pavel Lipchak +* Roberto Fernandez Diaz * Vorren diff --git a/HISTORY.rst b/HISTORY.rst index 9a91222..440f808 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) ++++++++++++++++++ diff --git a/jsonmodels/__init__.py b/jsonmodels/__init__.py index 286003f..c4f71c3 100644 --- a/jsonmodels/__init__.py +++ b/jsonmodels/__init__.py @@ -1,3 +1,3 @@ __author__ = 'Szczepan Cieślik' __email__ = 'szczepan.cieslik@gmail.com' -__version__ = '2.4.1' +__version__ = '2.5.0'