-
Notifications
You must be signed in to change notification settings - Fork 89
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
Command line argument for custom validators #202
Open
jhe-iqbis
wants to merge
6
commits into
23andMe:master
Choose a base branch
from
jhe-iqbis:custom_validator_cliarg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a65ad39
implemented command line argument for loading custom validators
jhe-iqbis fb56134
Made package executable by adding `__main__.py` used by `python -m ya…
jhe-iqbis cbf46f0
Added different examples in `examples/yamalevalidators.py` for the ne…
jhe-iqbis 577d8ea
Improved command line argument for loading custom validators.
jhe-iqbis 4056bba
Created test for the new `-i` command line argument.
jhe-iqbis 09b7479
Added error handling for `-i` file not found.
jhe-iqbis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import math | ||
import re | ||
from yamale.validators.base import Validator | ||
from yamale.validators import constraints as con | ||
from yamale import util | ||
|
||
|
||
class MultipleOf(con.Constraint): | ||
fail = '%s is not a multiple of %s' | ||
|
||
def __init__(self, value_type, kwargs): | ||
self.keywords = {'mul': value_type, 'prec': float} | ||
super(MultipleOf, self).__init__(value_type, kwargs) | ||
|
||
def _is_valid(self, value): | ||
return value % self.mul <= self.prec | ||
|
||
def _fail(self, value): | ||
return self.fail % (value, self.min) | ||
|
||
|
||
class PowerOf(con.Constraint): | ||
fail = '%s is not a power of %s' | ||
|
||
def __init__(self, value_type, kwargs): | ||
self.keywords = {'pow': value_type, 'prec': float} | ||
super(PowerOf, self).__init__(value_type, kwargs) | ||
|
||
def _is_valid(self, value): | ||
return abs(math.log(value, self.pow) % 1 <= self.prec | ||
|
||
def _fail(self, value): | ||
return self.fail % (value, self.min) | ||
|
||
|
||
class ExtendedNumber(Validator): | ||
"""Extended number/float validator""" | ||
value_type = float | ||
tag = 'xnum' | ||
constraints = [con.Min, con.Max, MultipleOf, PowerOf] | ||
|
||
def _is_valid(self, value): | ||
return isinstance(value, (int, float)) and not isinstance(value, bool) | ||
|
||
# OR: yamale.validators.validators.Number.constraints.extend([MultipleOf, PowerOf]) | ||
|
||
|
||
class ExtendedInteger(Validator): | ||
"""Extended integer validator""" | ||
value_type = int | ||
tag = 'xint' | ||
constraints = [con.Min, con.Max, MultipleOf, PowerOf] | ||
|
||
def _is_valid(self, value): | ||
return isinstance(value, int) and not isinstance(value, bool) | ||
|
||
# OR: yamale.validators.validators.Integer.constraints.extend([MultipleOf, PowerOf]) | ||
|
||
|
||
class NumberRegex(Validator): | ||
"""Regular expression validator with number support""" | ||
tag = 'num_regex' | ||
_regex_flags = {'ignore_case': re.I, 'multiline': re.M, 'dotall': re.S} | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.regex_name = kwargs.pop('name', None) | ||
|
||
flags = 0 | ||
for k, v in util.get_iter(self._regex_flags): | ||
flags |= v if kwargs.pop(k, False) else 0 | ||
|
||
self.regexes = [re.compile(arg, flags) | ||
for arg in args if util.isstr(arg)] | ||
super(NumberRegex, self).__init__(*args, **kwargs) | ||
|
||
def _is_valid(self, value): | ||
return any(r.match(str(value)) for r in self.regexes) | ||
|
||
def get_name(self): | ||
return self.regex_name or self.tag + " match" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Validate yaml files and check them against their schemas. Designed to be used outside of Vagrant. | ||
|
||
Just install Yamale: | ||
pip install yamale | ||
And run with: | ||
yamale | ||
OR | ||
python -m yamale | ||
""" | ||
from .command_line import main | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
map: map(oneof('hello', 'bye'), int()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from yamale.validators.base import Validator | ||
|
||
|
||
class OneOf(Validator): | ||
"""One of given values validator""" | ||
tag = 'oneof' | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(OneOf, self).__init__(*args, **kwargs) | ||
self.values = args | ||
|
||
def _is_valid(self, value): | ||
return value in self.values | ||
|
||
def fail(self, value): | ||
return '\'%s\' is not in %s' % (value, self.values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this step allows users of the yamale CLI to load arbitrary python code, the README.md warning may need to be broadened.