Yamale
Yamale copied to clipboard
Pass multiple values to a Custom Validator
Sometimes it would be handy to be able to have a custom validator which is able to validate a value in dependency of another value. Imagine some attributes are mutally exclusive or if you set one attribute another attribute have to be set. So it would help if validators are able to access the complete data structure. What do you think?
Hi, thanks for your interest in Yamale.
Since you're writing your own custom validator, you must also be using the API to run Yamale. That means you can do something like this today:
import yamale
import datetime
from yamale.validators import DefaultValidators, Validator
class Date(Validator):
""" Custom Date validator """
tag = 'date'
def _is_valid(self, value):
print(data)
return isinstance(value, datetime.date)
data = yamale.make_data('./187.yaml')
Date.data = data
validators = DefaultValidators.copy()
validators[Date.tag] = Date
schema = yamale.make_schema('./187.schema', validators=validators)
yamale.validate(schema, data)
If you would like something a little more built-in, I think that feature could fit well. We do accept pull requests if you'd like to submit this as a new feature.
Thanks a lot for your hints. I'll give this approach a try. 🙂