Exemplo n.º 1
0
    def validate(cls, text):
        if not isinstance(text, six.string_types):
            raise ValueError('Text to be evaluated is not typeof string.')

        errors = []

        # Validate the entire text to cover malformed delimiters and blocks.
        try:
            cls._jinja_env.parse(text)
        except jinja2.exceptions.TemplateError as e:
            errors.append(expr_util.format_error(cls._type, text, e))

        # Validate individual inline expressions.
        for expr in cls._regex_parser.findall(text):
            # Skip expression if it has already been validated and erred.
            if list(filter(lambda x: x['expression'] == expr, errors)):
                continue

            try:
                parser = jinja2.parser.Parser(cls._jinja_env.overlay(),
                                              cls.strip_delimiter(expr),
                                              state='variable')

                parser.parse_expression()
            except jinja2.exceptions.TemplateError as e:
                errors.append(expr_util.format_error(cls._type, expr, e))

        return errors
Exemplo n.º 2
0
def validate(statement):
    errors = []

    if isinstance(statement, dict):
        for k, v in six.iteritems(statement):
            errors.extend(validate(k)['errors'])
            errors.extend(validate(v)['errors'])

    elif isinstance(statement, list):
        for item in statement:
            errors.extend(validate(item)['errors'])

    elif isinstance(statement, six.string_types):
        evaluators = [
            evaluator for name, evaluator in six.iteritems(get_evaluators())
            if evaluator.has_expressions(statement)
        ]

        if len(evaluators) == 1:
            errors.extend(evaluators[0].validate(statement))
        elif len(evaluators) > 1:
            message = 'Expression with multiple types is not supported.'
            errors.append(expr_util.format_error(None, statement, message))

    return {'errors': errors}
Exemplo n.º 3
0
        def decorate_ctx_var_error(var):
            error = expr_utils.format_error(
                var['type'], var['expression'],
                'Variable "%s" is referenced before assignment.' % var['name'],
                var['spec_path'], var['schema_path'])

            return error
Exemplo n.º 4
0
    def test_format_error_str(self):
        expected = {
            'type': 'yaql',
            'expression': '$.foo in $.bar',
            'message': 'Unknown error.'
        }

        actual = utils.format_error('yaql', '$.foo in $.bar', 'Unknown error.')

        self.assertEqual(expected, actual)
    def test_format_error(self):
        expected = {
            'type': 'yaql',
            'expression': '$.foo in $.bar',
            'message': 'Unknown error.'
        }

        actual = expr_util.format_error('yaql', '$.foo in $.bar',
                                        Exception('Unknown error.'))

        self.assertEqual(expected, actual)
Exemplo n.º 6
0
    def test_format_error_str(self):
        expected = {
            "type": "yaql",
            "expression": "$.foo in $.bar",
            "message": "Unknown error."
        }

        actual = expr_util.format_error("yaql", "$.foo in $.bar",
                                        "Unknown error.")

        self.assertEqual(expected, actual)
Exemplo n.º 7
0
    def validate(cls, text):
        if not isinstance(text, six.string_types):
            raise ValueError('Text to be evaluated is not typeof string.')

        errors = []

        for expr in cls._regex_parser.findall(text):
            try:
                cls._engine(cls.strip_delimiter(expr))
            except (yaql_exc.YaqlException, ValueError, TypeError) as e:
                errors.append(expr_util.format_error(cls._type, expr, e))

        return errors
Exemplo n.º 8
0
    def test_format_error_with_paths(self):
        expected = {
            'type': 'yaql',
            'expression': '$.foo in $.bar',
            'spec_path': 'path.to.error.in.spec',
            'schema_path': 'path.to.reference.in.schema',
            'message': 'Unknown error.'
        }

        actual = utils.format_error('yaql',
                                    '$.foo in $.bar',
                                    Exception('Unknown error.'),
                                    spec_path='path.to.error.in.spec',
                                    schema_path='path.to.reference.in.schema')

        self.assertEqual(expected, actual)
Exemplo n.º 9
0
    def test_format_error_with_paths(self):
        expected = {
            "type": "yaql",
            "expression": "$.foo in $.bar",
            "spec_path": "path.to.error.in.spec",
            "schema_path": "path.to.reference.in.schema",
            "message": "Unknown error.",
        }

        actual = expr_util.format_error(
            "yaql",
            "$.foo in $.bar",
            Exception("Unknown error."),
            spec_path="path.to.error.in.spec",
            schema_path="path.to.reference.in.schema",
        )

        self.assertEqual(expected, actual)
Exemplo n.º 10
0
        def decorate_ctx_var_error(var, msg):
            error = expr_util.format_error(
                var["type"], var["expression"], msg, var["spec_path"], var["schema_path"]
            )

            return error
Exemplo n.º 11
0
        def decorate_ctx_var_error(var, msg):
            error = expr_util.format_error(var['type'], var['expression'], msg,
                                           var['spec_path'],
                                           var['schema_path'])

            return error