Skip to content

ypankovych/composition-based-validators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Composition based validators

This is a minimal implementation of infinitely nested validators.

class Sum(BaseValidator):

    def __init__(self, value):
        self.sum = value

    def validate(self, instance, value, field_name):
        return sum(value) == self.sum


class Foo:
    name = List(Int() | List(List(Int(min_value=0)) & Sum(10), min_size=5)) | Int(max_value=10)

    def __init__(self, name):
        self.name = name


foo = Foo(
    [
        [
            [1, 1, 1, 1, 6],
            [0, 0, 0, 0, 10],
            [1, 2, 3, 1, 1, 2],
            [5, 1, 1, 1, 1, 1],
            [2, 3, 4, 1, 0]
        ]
    ]
)

Here's validation tree of above validator:

image

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages