Skip to main content

Sanic pydantic validation

Project description

Build

Tox

Package

PyPI PyPI version Code style black

Tools for help developers working with Sanic and Pydantic.

Features

  • validate_schema

  • async_validate_by_method

Installation

pip install santic-validation

Usage

from typing import Optional
from pydantic import BaseModel, Field
from santic_validation import (
    validate_schema,
    async_validate_by_method,
    SanticModel,
    MethodType,
)


class QueryAddressSchema(BaseModel):
   location: str


def address_number(value: str):
   return f"Number: {value}"


def make_address(value: str):
   return f"Address: {value}"


class AddressSchema(SanticModel):
   number: MethodType[int] = Field(method=address_number)
   address: MethodType[str] = Field(method=make_address)

   @property
   def address_method_params(self):
       query: QueryAddressSchema = self._context.get("query")
       return {"value": f"{self.address} ({query.location})"}

   @property
   def number_method_params(self):
       query: QueryAddressSchema = self._context.get("query")
       return {"value": f"{self.number} ({query.location})"}


class PlanetSchema(BaseModel):
   name: Optional[str]
   category: int


sanic_app = Sanic(name="TestingApp")


def query_category_(category_id):
    return Model.get(id=category_id)


@sanic_app.route("/post", methods=["POST"])
@validate_schema(body=BodySchema, query=QuerySchema)
async def post_handler(request, body, query):

    # validate to make sure category exists
    # if exists, change the body.category value
    async_validate_by_method(
        body,
        {
            "category": {
                "method": query_category,
                "params": {"category_id": body.category},
            }
        },
        exceptions=(DoesNotExists,),
    )

    return Text("hello world")


@sanic_app.route("/post_address", methods=["POST"])
@validate_schema(
   body=AddressSchema,
   query=QueryAddressSchema,
   method_replace_value=True,
)
async def post_address(request, body, query):
    return json(
        {
            "body": body.dict(),
            "query": query.dict(),
        }
    )

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

santic-validation-0.0.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

santic_validation-0.0.3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file santic-validation-0.0.3.tar.gz.

File metadata

  • Download URL: santic-validation-0.0.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.10

File hashes

Hashes for santic-validation-0.0.3.tar.gz
Algorithm Hash digest
SHA256 afff009e1d0ceda0c59d09bf95538444489fad07667625ff17953cb20a18bdb1
MD5 b346cebb48ce55bd46c6bfce7203b1f6
BLAKE2b-256 b031d9d3638a89321288ac03eb847e1b2e47a996c2a6a6673f78695a98a88a68

See more details on using hashes here.

File details

Details for the file santic_validation-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for santic_validation-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3956efd90e1547c2c082355374aa194b8cd1d7ab4b82e1e374789ae19e692489
MD5 3d29cebc79be470e51b75e0aee1945b5
BLAKE2b-256 8571256caa7ad6f913052949e87211620f0a3337a4f0cc8672f14cf17c6b7b1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page