Pass schema's context to validation functions
See original GitHub issueI am thinking about a way to pass context to validation function.
Currently validation function expect exactly one argument, value of a field. Maybe Field._validate
method can check with inspect.getargspec and if function expect two arguments, send schema’s context as second argument?
I know that context is available in validation methods declared with @validates
decorator, but I want to reuse validation function in different schemas with different fields.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:16 (10 by maintainers)
Top Results From Across the Web
How to use Yup context variables in Formik validation
This article demonstrates the power of custom functions with Yup context variables and Formik validation in React forms.
Read more >Ajv options - Ajv JSON schema validator
Pass validation context to compile and validate keyword functions. If this option is true and you pass some context to the compiled validation...
Read more >Extending Schemas — marshmallow 3.19.0 documentation
You can register schema-level validation functions for a Schema using the marshmallow.validates_schema decorator. By default, schema-level validation errors ...
Read more >Validation Schema | Fonk Doc
What happens if we need to add addional context information to the validator function, for instance passing a custom error message ?
Read more >Validation - VeeValidate
Create a form context with the validation schema useForm({ ... You can pass your schemas to the useForm function using the same validationSchema...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kevgliss You can use the existing
@validate_schema
decorator to validate against the schema context. Thefield_names
parameter of theValidationError
exception allows you to specify which field(s) the error belongs to.I didn’t fully digest this requirement the first time around.
Currently you would have to:
but it would be a lot more convenient if you had access to the context in a field validator:
So
Field._validate()
would need to be able to efficiently identify when a validator has been wrapped in@pass_schema_context
decorator and provide the schema context as an additional argument.I’m not sure it makes sense to use a decorator outside of the schema body in order to identify that a field-level validator expects schema-level data. All the other decorators are used to register a processor with a specific schema class.