Feature/add doc #21
39
docs/source/usage.allof.rst
Normal file
39
docs/source/usage.allof.rst
Normal 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
|
||||||
41
docs/source/usage.anyof.rst
Normal file
41
docs/source/usage.anyof.rst
Normal 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
|
||||||
@@ -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).
|
||||||
@@ -43,3 +43,5 @@ For more complex schemas and types see our documentation on
|
|||||||
usage.array
|
usage.array
|
||||||
usage.object
|
usage.object
|
||||||
usage.reference
|
usage.reference
|
||||||
|
usage.allof
|
||||||
|
usage.anyof
|
||||||
Reference in New Issue
Block a user