Adds Docs for AllOf and AnyOf

This commit is contained in:
2025-06-21 18:20:44 -03:00
parent dee8b02d26
commit ac239c2617
4 changed files with 95 additions and 9 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -78,4 +78,8 @@ Examples
but you can access the model class by using the `Model.__fields__` attribute to get the field definitions, 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. or by using the `Model.model_fields` property to get a dictionary of field names and their types.
.. .. 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).

View File

@@ -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 For more complex schemas and types see our documentation on
.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2
:caption: Contents: :caption: Contents:
usage.string usage.string
usage.numeric usage.numeric
usage.bool usage.bool
usage.array usage.array
usage.object usage.object
usage.reference usage.reference
usage.allof
usage.anyof