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

Validators defined as class attributes keep previous state around #35

Open
di opened this issue Jan 5, 2017 · 0 comments
Open

Validators defined as class attributes keep previous state around #35

di opened this issue Jan 5, 2017 · 0 comments

Comments

@di
Copy link
Owner

di commented Jan 5, 2017

When extending the Vlad class, validators defined as class attributes will keep state from a previous validation around:

class YourFirstValidator(Vlad):
    validators = {
        'Column A': [
            UniqueValidator()
        ],
        'Column B': [
            SetValidator(['Vampire', 'Not A Vampire'])
        ]
    }
>>> vlad = YourFirstValidator(source=LocalFile('vladiate/examples/vampires.csv'))
>>> vlad.validate()
Validating YourFirstValidator(source=LocalFile('vladiate/examples/vampires.csv'))
Passed! :)
>>> vlad2 = YourFirstValidator(source=LocalFile('vladiate/examples/vampires.csv'))
>>> vlad2.validate()
Validating YourFirstValidator(source=LocalFile('vladiate/examples/vampires.csv'))
Failed :(
  UniqueValidator failed 3 time(s) (100.0%) on field: 'Column A'
    Invalid fields: ['('Count Chocula',)', '('Dracula',)', '('Vlad the Impaler',)']

For now, this can be worked around by making validators an instance variable:

class YourSecondValidator(Vlad):
    def __init__(self, *args, **kwargs):
        self.validators = {
            'Column A': [
                UniqueValidator()
            ],
            'Column B': [
                SetValidator(['Vampire', 'Not A Vampire'])
            ]
        }
        super(YourSecondValidator, self).__init__(*args, **kwargs)
>>> vlad = YourSecondValidator(source=LocalFile('vladiate/examples/vampires.csv'))
>>> vlad.validate()
Validating YourSecondValidator(source=LocalFile('vladiate/examples/vampires.csv'))
Passed! :)
>>> vlad2 = YourSecondValidator(source=LocalFile('vladiate/examples/vampires.csv'))
>>> vlad2.validate()
Validating YourSecondValidator(source=LocalFile('vladiate/examples/vampires.csv'))
Passed! :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant