Skip to content

any() silently ignores literals #269

Description

@sambowry

any() silently ignores literals. Fo example in the below schema 'a1' and 'c3' wil be not validated, only the str() part will have an effect.

# data
data: a1

# schema
data: any( 'a1', str(matches='b[0-9]+'), 'c3' )

In addition, I propose a new validator: notany() to check exclusions

implementation may look like:

# validators.py
class Any(Validator):
    """Any of several types validator"""

    tag = "any"

    def __init__(self, *args, **kwargs):
        self.literals   = []
        self.validators = []
        for val in args:
            if isinstance(val, Validator):
                self.validators.append( val )
            else:
                self.literals.append( val )

        if self.literals:
            self.validators.append( Enum( *self.literals ) )

        super(Any, self).__init__(*args, **kwargs)

    def _is_valid(self, value):
        return True


class NotAny(Validator):
    """No one of several types validator"""

    tag = "notany"
.
    def __init__(self, *args, **kwargs):
        self.validators = [ Any( *args, **kwargs ) ]
        super(NotAny, self).__init__(*args, **kwargs)

    def _is_valid(self, value):
        return True


# schema.py
    def _validate(self, validator, data, path, strict):
    . . .
        elif isinstance(validator, val.NotAny):
            sub_errors = self._validate_any(validator, data, path, strict)
            if not sub_errors:
                errors += [ "%s: %s is matched to %s" % ( str(path) if path and len(path._path)>0 else '<document>', data, str(validator.validators) ) ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions