From ac239c26173ada9e3a2998574f8ec4dcca2fca6f Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sat, 21 Jun 2025 18:20:44 -0300 Subject: [PATCH] Adds Docs for AllOf and AnyOf --- docs/source/usage.allof.rst | 39 +++++++++++++++++++++++++++++++ docs/source/usage.anyof.rst | 41 +++++++++++++++++++++++++++++++++ docs/source/usage.reference.rst | 6 ++++- docs/source/usage.rst | 18 ++++++++------- 4 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 docs/source/usage.allof.rst create mode 100644 docs/source/usage.anyof.rst diff --git a/docs/source/usage.allof.rst b/docs/source/usage.allof.rst new file mode 100644 index 0000000..2ef7269 --- /dev/null +++ b/docs/source/usage.allof.rst @@ -0,0 +1,39 @@ +AllOf Type +================= + +The AllOf type is used to combine multiple schemas into a single schema. It allows you to specify that an object must conform to all of the specified schemas. + + +Examples +----------------- + + +.. code-block:: python + + from jambo import SchemaConverter + + schema = { + "title": "Person", + "description": "A person", + "type": "object", + "properties": { + "name": { + "allOf": [ + {"type": "string", "maxLength": 11}, + {"type": "string", "maxLength": 4}, + {"type": "string", "minLength": 1}, + {"type": "string", "minLength": 2}, + ] + }, + }, + } + + Model = SchemaConverter.build(schema) + + obj = Model(name="J") + print(obj) # Output: Person(name='J') + + try: + obj = Model(name="") # This will raise a validation error + except ValueError as e: + print("Validation fails as expected:", e) # Output: Validation fails as expected: 1 validation error for Person \ No newline at end of file diff --git a/docs/source/usage.anyof.rst b/docs/source/usage.anyof.rst new file mode 100644 index 0000000..154645e --- /dev/null +++ b/docs/source/usage.anyof.rst @@ -0,0 +1,41 @@ +AnyOf Type +================= + +The AnyOf type is used to specify that an object can conform to any one of the specified schemas. It allows for flexibility in the structure of the data, as it can match multiple possible schemas. + + +Examples +----------------- + + +.. code-block:: python + + from jambo import SchemaConverter + + + schema = { + "title": "Person", + "description": "A person", + "type": "object", + "properties": { + "id": { + "anyOf": [ + {"type": "integer"}, + {"type": "string"}, + ] + }, + }, + } + + Model = SchemaConverter.build(schema) + + obj1 = Model(id="1") + print(obj1) # Output: Person(id='1') + + obj2 = Model(id=1) + print(obj2) # Output: Person(id=1) + + try: + obj3 = Model(name=1.1) # This will raise a validation error + except ValueError as e: + print("Validation fails as expected:", e) # Output: Validation fails as expected: 1 validation error for Person \ No newline at end of file diff --git a/docs/source/usage.reference.rst b/docs/source/usage.reference.rst index 33ee6ae..b5f4ddf 100644 --- a/docs/source/usage.reference.rst +++ b/docs/source/usage.reference.rst @@ -78,4 +78,8 @@ Examples but you can access the model class by using the `Model.__fields__` attribute to get the field definitions, or by using the `Model.model_fields` property to get a dictionary of field names and their types. -.. \ No newline at end of file +.. warning:: + + The JSON Schema Reference specification allows for uri referneces, + but Jambo currently only supports root references (using the `#` symbol) + and def references (using the `$def` property). \ No newline at end of file diff --git a/docs/source/usage.rst b/docs/source/usage.rst index a86b2f4..749dfc0 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -34,12 +34,14 @@ If passed a description inside the schema it will also add it to the Pydantic mo For more complex schemas and types see our documentation on .. toctree:: - :maxdepth: 2 - :caption: Contents: + :maxdepth: 2 + :caption: Contents: - usage.string - usage.numeric - usage.bool - usage.array - usage.object - usage.reference \ No newline at end of file + usage.string + usage.numeric + usage.bool + usage.array + usage.object + usage.reference + usage.allof + usage.anyof \ No newline at end of file