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

Command line argument for custom validators #202

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions examples/yamalevalidators.py
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"
16 changes: 16 additions & 0 deletions yamale/__main__.py
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()
16 changes: 14 additions & 2 deletions yamale/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

Just install Yamale:
pip install yamale
And run with:
yamale
OR
python -m yamale
"""

import argparse
Expand Down Expand Up @@ -98,8 +102,14 @@ def _validate_dir(root, schema_name, cpus, parser, strict):
raise ValueError('\n----\n'.join(set(error_messages)))


def _router(root, schema_name, cpus, parser, strict=True):
def _router(root, schema_name, cpus, parser, strict=True, include=None):
root = os.path.abspath(root)
if include is not None:
if not os.path.isfile(include):
raise ValueError("Python include file '{}' not found.".format(include))
from importlib.machinery import SourceFileLoader
SourceFileLoader(os.path.basename(include), include).load_module()
Copy link
Contributor

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.

yamale.validators.validators.update_default_validators()
if os.path.isfile(root):
_validate_single(root, schema_name, parser, strict)
else:
Expand All @@ -117,11 +127,13 @@ def main():
help='number of CPUs to use. Default is 4.')
parser.add_argument('-p', '--parser', default='pyyaml',
help='YAML library to load files. Choices are "ruamel" or "pyyaml" (default).')
parser.add_argument('-i', '--include', default=None,
help='File path of Python library to load for custom validators.')
parser.add_argument('--no-strict', action='store_true',
help='Disable strict mode, unexpected elements in the data will be accepted.')
args = parser.parse_args()
try:
_router(args.path, args.schema, args.cpu_num, args.parser, not args.no_strict)
_router(args.path, args.schema, args.cpu_num, args.parser, not args.no_strict, args.include)
except (SyntaxError, NameError, TypeError, ValueError) as e:
print('Validation failed!\n%s' % str(e))
exit(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
map: map(oneof('hello', 'bye'), int())
18 changes: 18 additions & 0 deletions yamale/tests/command_line_fixtures/yamalevalidators.py
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)
13 changes: 10 additions & 3 deletions yamale/tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,28 @@ def test_good_yaml(parser):
command_line._router(
'yamale/tests/command_line_fixtures/yamls/good.yaml',
'schema.yaml', 1, parser)


@pytest.mark.parametrize('parser', parsers)
def test_good_relative_yaml(parser):
command_line._router(
'yamale/tests/command_line_fixtures/yamls/good.yaml',
'../schema_dir/external.yaml', 1, parser)


@pytest.mark.parametrize('parser', parsers)
def test_external_glob_schema(parser):
command_line._router(
'yamale/tests/command_line_fixtures/yamls/good.yaml',
os.path.join(dir_path, 'command_line_fixtures/schema_dir/ex*.yaml'), 1, parser)



@pytest.mark.parametrize('parser', parsers)
def test_include(parser):
command_line._router(
'yamale/tests/command_line_fixtures/yamls/good.yaml',
'schema_needs_include.yaml', 1, parser, include='yamale/tests/command_line_fixtures/yamalevalidators.py')


def test_empty_schema_file():
with pytest.raises(ValueError, match='is an empty file!'):
Expand Down
10 changes: 6 additions & 4 deletions yamale/validators/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def __init__(self, *args, **kwargs):

DefaultValidators = {}

for v in util.get_subclasses(Validator):
# Allow validator nodes to contain either tags or actual name
DefaultValidators[v.tag] = v
DefaultValidators[v.__name__] = v
def update_default_validators():
for v in util.get_subclasses(Validator):
# Allow validator nodes to contain either tags or actual name
DefaultValidators[v.tag] = v
DefaultValidators[v.__name__] = v
update_default_validators()