How to validate one key based on another in Colander Python?

128 views Asked by At

I am using Colander for validation, and would like to validate one key based on another.

e.g: I would like to validate field value based on entity value:

class ValidationSchema(Validator.MappingSchema):
    entity = Validator.SchemaNode(Validator.String())
    field = Validator.SchemaNode(Validator.String(), missing=Validator.drop)

try:
    values = {"entity":"a", "field":"field1"}
    parameters = ValidationSchema().deserialize()
except Validator.Invalid as e:
    errors = e.asdict()
    return

If entity is a then field should be field1
If entity is b then field should be field2
If entity is not present then do not validate field key

How can this be implemented?

0

There are 0 answers