Add custom format of string and custom validation. #426
-
Hello I need format mapping to my class. openapi: 3.0.0
components:
schemas:
Network:
type: object
properties:
portRange:
type: string
format: port-range
example: "55-1000" And I want to get it in the end class Network(BaseModel):
portRange: Optional[PortRange] = Field(
None, example='55-1000'
) Where How can I add my own validations to the schema, so as not to lose the model with validations in case of re-creation? Thanks for the help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Currently, it is not supported. We are talking about custom type in #418
Also, My idea is ... openapi: 3.0.0
components:
schemas:
Network:
type: object
properties:
name:
type: string
customValidator: "padding" ...
{%- for field in fields -%}
{%- if field.field %}
{{ field.name }}: {{ field.type_hint }} = {{ field.field }}
{%- elif field.required or (field.represented_default == 'None' and field.strip_default_none) %}
{{ field.name }}: {{ field.type_hint }}
{%- else %}
{{ field.name }}: {{ field.type_hint }} = {{ field.represented_default }}
{%- endif %}
{%- if field.customValidator == 'format_name' %}
@validator('{{ field.name}}')
def padding_{{ field.name }}(value):
return value.ljust(10, ' ')
{%- endif %}
... |
Beta Was this translation helpful? Give feedback.
@gespispace
Currently, it is not supported.
We are talking about custom type in #418
Also,
validators
is not supported too.But, you can give the code generator a custom template in jinja2.
https://github.com/koxudaxi/datamodel-code-generator/blob/master/datamodel_code_generator/model/template/pydantic/BaseModel.jinja2
My idea is ...
If a special attribute will be supported in Jsonchema then we can check the value in a custom template 🤔