Skip to content

매개변수와 데이터 검증

Nexify를 사용하면 다양한 매개변수에 대한 검증을 추가할 수 있습니다.

문자열 검증: 최소 & 최대 길이

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}

✅ 허용됨: /items/ab, /items/12345678

❌ 허용 안됨: /items/a (너무 짧음), /items/123456789 (너무 김)

위의 예제에서는 item_id가 최소 2글자 이상, 최대 8글자 이하만 허용되도록 설정되어 있습니다.

문자열 검증: 정규 표현식

정규 표현식을 사용하면 특정 형식의 값만 허용하도록 제한할 수 있습니다.

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}

✅ 허용됨: /users/test@example.com, /users/user123@domain.co.kr

❌ 허용 안됨: /users/invalid-email, /users/user@domain

위의 예제에서는 email 매개변수가 이메일 형식인지 검증합니다.

만약 잘못된 email과 함께 요청할 경우, 아래와 같은 422 응답을 확인할 수 있습니다.

{
  "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-.]+$"
      }
    }
  ]
}

숫자 검증: ~보다 큼, 크거나 같음, 작음, 작거나 같음

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)

숫자 검증: 최대 자릿수 제한

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}

✅ 허용됨: /items/123 (3자리)

❌ 허용 안됨: /items/1000000 (7자리)

문서화

Swagger UI with max and min length

최대, 최소 길이는 모두 Swagger UI에 표시됩니다.

Swagger UI with pattern

이뿐만 아니라 다양한 데이터 검증 조건이 자동으로 Swagger UI에 포함됩니다.

다양한 데이터 검증 옵션

  • multiple_of: 특정 값의 배수 제한
  • allow_inf_nan: 무한대 및 NaN 허용
  • decimal_places: 소수점 이하 자릿수 제한