Conditional Schema
See original GitHub issueSuppose I want to support an abstract payload that uses another field to drive nested validations:
TEST_TASK = {
"folder": {
"required": True
}
}
BUILD_TASK = {
"environment": {
"allowed": ["staging", "product"]
}
}
ABSTRACT_TASK = {
'type': {
'allowed': ['test', 'build']
},
'data': {
'type': 'dict',
'required': True,
'anyof': [
{'schema': TEST_TASK},
{'schema': BUILD_TASK}
]
}
}
>>> validator = cerberus.Validator(schemas.ABSTRACT_TASK)
>>> validator.validate({
"type": "build",
"data": {
"environment": "bad"
}
})
>>> pp validator.errors
{'data': {'anyof': 'no definitions validated',
'definition 0': {'environment': 'unknown field',
'folder': 'required field'},
'definition 1': {'environment': 'unallowed value bad'}}}
Is there a way to conditionally use definition 1
when the parent has a type of build
? For example, if the ABSTRACT_TASK is modified so that anyof
is replaced with the BUILD_TASK schema:
>>> pp validator.errors
{'data': {'environment': 'unallowed value bad'}}
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Applying Subschemas Conditionally - JSON Schema
The dependentSchemas keyword conditionally applies a subschema when a given property is present. This schema is applied in the same way allOf applies ......
Read more >Applying subschemas conditionally | Opis JSON Schema
This is a conditional structure containing three keywords: if , then and else . Every keyword value must be a valid JSON schema...
Read more >jsonSchema attribute conditionally required - Stack Overflow
The dependentSchemas keyword is a conditional way to apply a schema. Foreach property in dependentSchemas , if the property is present in ...
Read more >How to conditionally validate based on the values of external ...
Essentially, I need conditional validation (if-then-else) where the if schema can refer to either some sort of external variable or a field in...
Read more >Conditional display and validation of properties - Sitefinity ...
Conditional display and validation of properties. This article contains. Overview · Procedure · JSON schema · Overview. Each type can have properties assigned ......
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
what about such constructs?
or?