Skip to content

Parameters and Validation

You can add validation for both path and query parameters.

String Validation: Minimum & Maximum Length

from typing import Annotated

from nexify import Nexify, Path

app = Nexify()


@app.get("/items/{item_id}")
def read_item(item_id: Annotated[str, Path(min_length=2, max_length=8)]) -> dict:
    return {"item_id": item_id}

✅ Allowed: /items/ab, /items/12345678

❌ Not Allowed: /items/a (too short), /items/123456789 (too long)

In this case, item_id is constrained to a minimum of 2 characters and a maximum of 8 characters.

String Validation: Regular Expressions

You can define a regular expression pattern that the parameter should match:

from typing import Annotated

from nexify import Nexify, Path

app = Nexify()


EMAIL_REGEX = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"


@app.get("/users/{email}")
def get_user(email: Annotated[str, Path(pattern=EMAIL_REGEX)]) -> dict:
    return {"email": email}

✅ Allowed: /users/test@example.com, /users/user123@domain.co.kr

❌ Not Allowed: /users/invalid-email, /users/user@domain

In this case, the email parameter is validated to ensure it follows an email format.

If an invalid email is provided, a 422 response like the following will be returned:

{
  "detail": [
    {
      "type": "string_pattern_mismatch",
      "loc": [
        "path",
        "email"
      ],
      "msg": "String should match pattern '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$'",
      "input": "invalid-email",
      "ctx": {
        "pattern": "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$"
      }
    }
  ]
}

Numeric Validation: Greater Than, Greater Than or Equal, Less Than, Less Than or Equal

from typing import Annotated

from nexify import Nexify, Path

app = Nexify()


@app.get("/items/{item_id}")
def read_item(item_id: Annotated[str, Path(ge=1)]) -> dict:
    return {"item_id": item_id}
  • gt: Greater than
  • ge: Greater than or equal
  • lt: Less than
  • le: Less than or equal

Numeric Validation: Maximum Number of Digits

from typing import Annotated

from nexify import Nexify, Path

app = Nexify()


@app.get("/items/{item_id}")
def read_item(item_id: Annotated[int, Path(max_digits=6)]) -> dict:
    return {"item_id": item_id}

✅ Allowed: /items/123 (3 digits)

❌ Not Allowed: /items/1000000 (7 digits)

문서화

Swagger UI with max and min length

Both maximum and minimum lengths are displayed in Swagger UI (and ReDoc).

Swagger UI with pattern

Additionally, various data validation rules are automatically included in Swagger UI (and ReDoc).

Additional Data Validation Options

  • multiple_of: Value must be a multiple of this.
  • allow_inf_nan: Allow inf, -inf, nan.
  • decimal_places: Maximum number of decimal places allowed for numbers.