Pydantic provides Field(json_schema_extra=...)
which allows to do something like:
def forbid_internal_labels(value):
if re.match(r"^reserved-for-internal-use:.*$", value):
raise ValueError(...)
class Foo(BaseModel):
label: Annotated[str, AfterValidator(forbid_internal_labels), Field(json_schema_extra={
"not": {
{
"type": "string",
"pattern": "^reserved-for-internal-use:.*$"
},
}
})]
This would generate:
"type": "string",
"not": {
{
"type": "string",
"pattern": "^reserved-for-internal-use:.*$"
},
}
It doesn't actually add/do any validation, it merely impacts the generated JSON schema.
Similar mechanism is needed for Parameter
, to inject discriminators (e.g. anyOf
, not
) into the OpenAPI spec (although the validation is decoupled from the schema declaration).
The builtin generation uses oneOf
for union types (IIRC Pydantic uses anyOf
for those). But I don't think it's possible to declare something like above.
Workarounds at this point would be:
Operation
class (did not try but I assume it could work)OpenAPIPlugin
?)Both are quite ugly hacks, while something like this should be supported out-of-the-box.
No response
No response
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too