Skip to main content

A regex validator for pydantic, using python regex.

Project description

A regex parser for Pydantic, using pythons regex validator.

Since pydantic V2, pydantics regex validator has some limitations. It cannot do look arounds. There is some documenation on how to get around this. This package simplifies things for developers

We provide the class, Regex, which can be used.

Usage in Pydantic

To use simply do:

from pydantic_python_regex_validator import Regex
from pydantic import BaseModel
from typing import Annotated

class MyModel(BaseModel):
    my_field: Annotated[str, Regex(r'^foo')]

This will then have field, my_field, which will have the regex constraint ^foo

Usage in FastAPI

This can also be used with fastapi.

from pydantic_python_regex_validator import RegexQuery, RegexBody
from fastapi import FastAPI

app = FastAPI()

@app.get("/test")
async def get_endpoint(my_param: RegexQuery("^foo")):
    return my_param

@app.post("/test")
async def post_endpoint(my_param: RegexBody("^foo")):
    return my_param

This then has an endpoint at /test which takes a param, my_param, which has the regex constraint ^foo

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

pydantic_python_regex_validator-0.3.4.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

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