kennethreitz / responder

A familiar HTTP Service Framework for Python.
http://responder.kennethreitz.org/
Other
3.59k stars 218 forks source link

Input schema validation support for Pydantic and Marshmellow #490

Closed tabotkevin closed 10 months ago

tabotkevin commented 10 months ago

check examples/schema_validation.py

""" Decorator for parsing and validating input schema. Supports both Pydantic and Marshmallow.

Usage:: import time

from pydantic import BaseModel
import responder

class Item(BaseModel)
    name: str

api = responder.API()

@api.route("/create")
@api.trust(Item)
def create_item(req, resp, *, data):
    @api.background.task
    def process_item(item):
        time.sleep(2)
        print(item)   # e.g {"name": "Monster Hunter"}

    process_item(data)
    resp.media = {"msg": "created"}

"""

Summary by CodeRabbit

kennethreitz commented 10 months ago

This is beautiful.

kennethreitz commented 10 months ago

let's make pydantic a dependency. or is that a bad idea?

tabotkevin commented 10 months ago

Yes,,I added pydantic already as dependency in the setup.py file for installation.

coderabbitai[bot] commented 10 months ago

It seems that the existing content is still valid and meets the requirements. Therefore, I will repeat it verbatim in my response:

Walkthrough

The changes involve integrating schema validation into a Python web service using the Responder framework. Two popular schema libraries, Pydantic and Marshmallow, are now supported for input validation. The update includes modifications to the API class, addition of schema validation logic, and the necessary dependency updates. Tests have been added to ensure the new validation works correctly with both schema types.

Changes

File(s) Change Summary
examples/schema_validation.py Introduced a Python web service with routes for creating books using Pydantic and Marshmallow for schema validation.
responder/api.py, responder/ext/schema/__init__.py Added input schema parsing and validation to the API class; adjusted for compatibility with Pydantic and Marshmallow.
responder/models.py Made a minor syntactical change to the Response class.
setup.py, Pipfile (2 entries) Added "pydantic" to dependencies and updated development dependencies with pytest, pytest-mock, and pydantic.
examples/hello_fortune.py (2 entries) Cosmetic changes to string quotations and response header formatting.
tests/test_responder.py (2 entries) Added tests for Pydantic and Marshmallow schema validation within the API framework.

🐇 In the brisk November air, we code with flair, 🍂
Schema validations we weave, with Pydantic and Marshmallow up our sleeve. 📚
As tests ensure our code's might, we hop into the season's cozy night. 🌙✨


Tips ### Chat with CodeRabbit Bot (`@coderabbitai`) - If you reply to a *review comment* from CodeRabbit, the bot will automatically respond. - To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment - Note: Review comments are made on code diffs or files, not on the PR overview. - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Note: For conversation with the bot, please use the review comments on code diffs or files. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - The JSON schema for the configuration file is available [here](https://coderabbit.ai/integrations/coderabbit-overrides.v2.json). - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json`