Yamale icon indicating copy to clipboard operation
Yamale copied to clipboard

Pass multiple values to a Custom Validator

Open franzs opened this issue 3 years ago • 2 comments

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?

franzs avatar Jan 12 '22 16:01 franzs

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.

mildebrandt avatar Jan 12 '22 18:01 mildebrandt

Thanks a lot for your hints. I'll give this approach a try. 🙂

franzs avatar Jan 13 '22 11:01 franzs