Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation of ResponseModel? #85

Open
ScottSturdivant opened this issue Jan 12, 2024 · 1 comment
Open

Validation of ResponseModel? #85

ScottSturdivant opened this issue Jan 12, 2024 · 1 comment

Comments

@ScottSturdivant
Copy link

ScottSturdivant commented Jan 12, 2024

Is it possible to validate that ResponseModel is what's returned? Hoping I've missed something obvious in the docs: I see query, body, and form, along with the ability to generate response_many.

Extending the sample app.py a tiny bit:

from typing import Optional
from flask import Flask, request
from pydantic import BaseModel

from flask_pydantic import validate

app = Flask("flask_pydantic_app")

class QueryModel(BaseModel):
  age: int

class ResponseModel(BaseModel):
  id: int
  age: int
  name: str
  nickname: Optional[str] = None

# Example 1: query parameters only
@app.route("/", methods=["GET"])
@validate()
def get(query: QueryModel) -> ResponseModel:
  age = query.age
  if age > 10:
      return {'asdf': False}. # This should NOT be a valid response
  return ResponseModel(
    age=age,
    id=0, name="abc", nickname="123"
    )

Now querying and getting a response that doesn't match the expected ResponseModel:

❯ curl 'http://localhost:5000/?age=1'
{"id":0,"age":1,"name":"abc","nickname":"123"}
❯ curl 'http://localhost:5000/?age=100'
{"asdf":false}

Was hoping that there was a way to ensure that an invalid response schema couldn't be returned. Any pointers?

@bauerji
Copy link
Owner

bauerji commented Jan 17, 2024

Hey, you would expect the error to be raised on runtime? I think mypy should catch this issue. Do you use some kind of static type checker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants