Combined types (allOf, anyOf, oneOf, not) #10
rafalkrupinski
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It's an important point and I'm being reminded of it again :)
Lets keep in mind there is a difference between type hinting and validation.
typing.Union has been good enough for me, for cases of oneOf[integer, string] or oneOf[object, list]
I think the problem is coming from the approach taken by jsonschema/openapi, that everything is allowed by default (no type := any type, additionalProperties is true) unless it's narrowed down.
That's fine with Python itself, class objects are just fancy dictionaries and you can put almost anything there.
But in case of typing, the approach is additive - whatever is not declared, we know nothing about.
A python model class system for schema can only go as far, and some corners have to be cut.
Json value can be of only one type at a time* (string, integer, float, object or array) so each of them can be handled separately and then, in case of anyOf and oneOf, put in an typing.Union.
allOf cannot combine different types.
Scalar types
For primitive types only the type can be hinted (str and format types like uuid, int, float).
Arrays
Only list or Sequence can be hinted. Its generic argument should follow the same rules as any other schema object
Objects
I was thinking of generating classes that include members of all its elements, using Union where names clash.
Same for allOf, anyOf, oneOf.
If a property is only defined under not:, it should be skipped from the model
schemas like
are redundant - if a property has one type, it cannot have another.
Schemas like
can be implemented as typing.Union. anyOf degrades to oneOf
Beta Was this translation helpful? Give feedback.
All reactions