Open sarangsbabu367 opened 3 years ago
@sarangsbabu367 Thank you for your suggestion. I feel it's a good idea. I will implement it when I finish other issues.
@koxudaxi May i open a pr for this ?
yaml
, like currently there is an option for customTypePath
can we use a similar name like customValidator
?yaml
format(minimalize) ?Person:
type: object
customValidator:
type: object
properties:
path: a.b.c.person_root_validator
args:
pre: True
properties:
name:
type: string
customValidator:
type: object
properties:
path: a.b.c.name_must_contain_space
args:
pre: True
always: True
In the above example, we can support an option for root-level
validator. And option to give a validator name can be removed since there wont be much usecase(validator method name will be same as given by client)
Isn't it possible to just not "override" the "@validator" code whenever we generate the pydantic objects again?
Just adding "@validator" logic to the spec would not provide the flexibility that some examples might need. ie: in this case, we would want to validate if a given field matches a set of regexs.
openapi: 3.0.2
info:
title: Example 🐶
version: 1.0.0
paths: {}
components:
schemas:
example.Job:
title: Job
type: object
properties:
schedule:
title: Schedule
type: string
description: Frequency of cronjob. (@once to run 1 time, None to not run automatically)
example: 0 1 * * 1
# example.py
(...)
# added manually ----->
def compile_match_cron_expression(expression: str):
cron_re = re.compile(
r"(@(once|annually|yearly|monthly|weekly|daily|hourly|reboot))|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})"
)
return cron_re.match(expression) or None
# <---------------------
class Job(BaseModel):
schedule: Optional[str] = Field(
None,
description='Frequency of cronjob. (@once to run 1 time, None to not run automatically)',
example='0 1 * * 1',
title='Schedule',
)
# added manually ----->
@validator("schedule")
def schedule_is_cron(cls, v):
if v:
assert compile_match_cron_expression(v), "Value provided is not a valid cron expression."
return v
# <---------------------
This could also solve this problem. Is this something that datamodel-codegen
allows today?
Is your feature request related to a problem? Please describe.
pydantic
supports the provision to add a validator decorator for fields. Is there anyway to represent this injsonschema
and data-model-generator will generate this. This can be treated as acustom-path
where decorator code will be present in the given path and during the model generation this method needs to be attached to the model(Not sure about this).Describe the solution you'd like
jsonschema
.class UserModel(BaseModel): name: str
Describe alternatives you've considered