Skip to content

Mockup of quick expression validator syntax #346

Open
@bsamuel-ui

Description

@bsamuel-ui

Here's a quick mockup of the idea.

The idea is to provide a special token VAL that constructs validators using magic methods so you can write:

@attr.s
class Test:
    positive = attr.ib(validator=[VAL.isa(int, float), VAL > 0.0])
    nonempty = attr.ib(validator=[VAL.len() >= 1])

And, of course, it can report the expression in the exception it raises.

Things to support in a real version:

  • __getitem__ to check keys
  • __getattr__ to allow property and method checks
  • the other operators, especially in and such
  • __call__ validates in the mockup, but attr.ib should tell the object to convert itself to a validator

I'd like to see what the community thinks before putting together a proper PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ThinkingNeeds more braining.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions