OpenAPI Schema Object is equivalent of JSON Schema with some minor changes.
In theory, python objects are dictionaries with some some syntactic sugar added.
With the exception of __dunder__ fields, a __dict__ of python object could be directly validated against a JSON Schema.
Representation is not as simple, as the object model of JSON Schema is pretty much opposite of the object model of type-hinted python, as enforced by mypy.
Since schema object represents only a collection of constraints, any object that meets the constraints is valid. In particular, in case of the definition
type: object
properties:
- field:
type: string
required:
- field
additionalProperties: falsecan be exactly represented as
class SomeType:
field: str | Nonebut if additionalProperties is skipped, then any object, that has at least this property, would've validated against the schema.
Such schema is well represented by a Protocol
Atomic types are types other than object and array.
They are represented by their python equivalents:
- str
- int
- float
Objects are represented by python classes, and separately, where they're used, by the class name.
#TODO naming of classes
When omitted, it means the value may be of any type, supported by JSON Schema.
When no other keywords are present, it means any atomic, container (object or array) or null values are valid, and it's represented as typing.Any.
When serializing, the serialization method can be assumed from the data type.
- Atomic values can be serialized in the obvious way
- objects can be serialized if they are of instances of a type with a known serialization method (pydantic, dataclasses, etc),
- arrays can be serialized as long their items can be serialized.
- title
- multipleOf
- maximum
- exclusiveMaximum
- minimum
- exclusiveMinimum
- maxLength
- minLength
- pattern
- maxItems
- minItems
- uniqueItems
- maxProperties
- minProperties
required
- enum