Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass multiple values to a Custom Validator #187

Open
franzs opened this issue Jan 12, 2022 · 2 comments
Open

Pass multiple values to a Custom Validator #187

franzs opened this issue Jan 12, 2022 · 2 comments

Comments

@franzs
Copy link

franzs commented Jan 12, 2022

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?

@mildebrandt
Copy link

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.

@franzs
Copy link
Author

franzs commented Jan 13, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants