Skip to content

Make YAML validator more elegant #51

@ajjackson

Description

@ajjackson

It seems like there should be a smarter way of doing this...

    def is_valid(loc, typ):
        if (trial := toolz.dicttoolz.get_in(loc.split("/"), file, default=None)) is None:
            raise ValidationError(f"{loc} key is missing.")
        if not isinstance(trial, typ):
            raise ValidationError(f"{loc} should be {typ_.__name__} is {type(trial).__name__}")

    required = {
        'name': str,
        'version': dict,
        ('default_version', 'version/default_version'): str,
    }
    for valid, typ in required.items:
        if isinstance(valid, tuple):
           for tmp in valid:
               with suppress(ValidationError):
                   is_valid(tmp, typ)
                   break
           else:
               raise ValidationError(...)             
        else:
            is_valid(valid, typ)

Or similar?

Originally posted by @oerc0122 in #19 (comment)

Various more elegant alternatives were considered. My current leaning is towards json-schema.

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