Skip to content

daiquiri.core.schema.validators

OneOf(choices, **kwargs)

Single enum type validator

Parameters:

Name Type Description Default
choices list

List of choices

required
Source code in daiquiri/core/schema/validators.py
def OneOf(choices, **kwargs):
    """Single enum type validator

    Args:
        choices (list): List of choices
    """
    metadata = kwargs.pop("metadata", {})
    metadata["enum"] = choices

    return fields.Str(metadata=metadata, validate=validate.OneOf(choices), **kwargs)

RequireEmpty(**kwargs)

A RequireEmpty field

Requires that this particular field is empty by combining NoneValidator and EmptyField

Source code in daiquiri/core/schema/validators.py
def RequireEmpty(**kwargs):
    """A RequireEmpty field

    Requires that this particular field is empty by combining NoneValidator and EmptyField
    """
    return EmptyField(validate=NoneValidator(), allow_none=True, **kwargs)

ValidatedRegexp(pattern, **kwargs)

A regexp validated fields.Str

Parameters:

Name Type Description Default
pattern

the pattern to use from the above dictionary

required

Kwargs: passed to the field

Source code in daiquiri/core/schema/validators.py
def ValidatedRegexp(pattern, **kwargs):
    """A regexp validated fields.Str

    Args:
        pattern: the pattern to use from the above dictionary
    Kwargs:
        passed to the `field`
    """

    expressions = {
        "word": "^[A-z0-9]+$",
        "word-dash": "^[A-z0-9-]+$",
        "word-dash-space": r"^[\w\s-]+$",
        "word-special": r"^(\w|\s|-|%|\(|\)|,|\.)+$",
        "saving": "^({(sample|component|subsampletype|subsampleid)}|[A-z0-9/-])+$",
        "smiles": r"^([^J][A-Za-z0-9@+\-\[\]\(\)\\\/%=#$]+)$",
        "path": "^/[A-z0-9-/]+$",
    }

    if pattern in expressions:
        validates = kwargs.pop("validate", None)
        if validates and not isinstance(validates, list):
            validates = [validates]

        metadata = kwargs.pop("metadata", {})
        metadata["pattern"] = expressions[pattern]

        return fields.Str(
            validate=[
                validate.Regexp(
                    expressions[pattern],
                    flags=0,
                    error="Input {input} does not match {regex}",
                )
            ]
            + (validates if validates else []),
            metadata=metadata,
            **kwargs
        )
    else:
        raise Exception("Cant find regular expression for {pat}".format(pat=pattern))