Skip to main content

Validation library

Project description

Permissive Path


PyPermissive

tests Python 3.x PyPI License

Validation library in Python, modeled after Pydantic

Example

Inherit from BaseModel and describe required types.
PyPermissive supports validation for primitive types:

class Employee(BaseModel):
    employee_id: int
    name: str
    salary: float
    elected_benefits: bool = False
    
employee = Employee(
    employee_id=1,
    name="Foo Bar",
    salary=123_000.00,
    elected_benefits=True,
)

collections:

class Book(BaseModel):
    characters: dict[str, str]
    chapters: list[str]
    
book = Book(
    characters={"Pelleas": "he", "Melisande": "she"},
    chapters=["Beginning", "Middle", "End"]
)

unions, classes and fields.

Fields are similar to pydantic with one caveat: you need to give value type explicitly:

class User(BaseModel):
    name: Field(type=str, default="Jimmie", frozen=True)
    age: Field(type=int, gt=18, lt=35)
    id: Field(type=UUID, default_factory=uuid4)
    email: Field(type=str, pattern=r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.][a-zA-Z0-9-.]+$")
    nickname: Field(type=str, min_length=6, max_length=12)
    PIN: Field(type=str, field_validator=lambda x: x.isdigit())

You can also use decorators:
@ComputedField (invoke only from instances) and @ComputedClassField (invoke both on class and instance level)

class Thesis:
    BAZZ = ["1", "2", "3"]

    def __init__(self):
        self.fizz = [1, 2, 3, 4, 5]
        self.buzz = [6, 7, 8, 9]

    @ComputedField
    def foo(self):
        return [val for val in itertools.product(self.fizz, self.buzz)]

    @ComputedClassField
    def bar(self):
        return list(itertools.permutations(self.BAZZ))

    

The library supports @validate_call that checks both argument and return types:

@validate_call
def some_func(delimiter: str, count: int, numbers: list[int]) -> str:
    return (delimiter * count).join([str(d) for d in numbers])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypermissive-1.0.0.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

pypermissive-1.0.0-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page