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

  • SanticModel

  • MethodType Field

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.5.tar.gz (7.1 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.5-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: santic-validation-0.0.5.tar.gz
  • Upload date:
  • Size: 7.1 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.5.tar.gz
Algorithm Hash digest
SHA256 4a516fe50b5141c6adbc1eeb3ee3055d15a19f62ae9b6d8dc05ba9f234b96899
MD5 426ef4622fb4d218007776e1b24b49e9
BLAKE2b-256 37926c88fdbab2983d4078c902c841827ecfc757251047d29055a1c29973aa6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for santic_validation-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 429cb86b246f24405b8da94de1f28801e3eee4944fb18fd5656515b5b831decf
MD5 6ccc3a55607c9a4e266bd0ec61f5ceb1
BLAKE2b-256 0cd697e55445f63c7d9ba6838a915d71cd5f381afcecc66ba3261564abcb6cca

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