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.4.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.4-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: santic-validation-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 b01841cbe0b65c92eac6745dbab765507c27ac6f7099431c546dff0673a2487b
MD5 140f53583258e1def7847e67862292d7
BLAKE2b-256 3887b0819a1a287f6482c28ed0a6db7f4af807f0e4af1a29a2b1d8b78d688fdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for santic_validation-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc0982991fa661f3c5248d5c5d7bb42b61d6eac31b48d38cef3e1f0f2f691de3
MD5 94db7f6d67927c53434c13387924c714
BLAKE2b-256 0423dfb5fa67390754f672f6a558d9934f46d00a69e0d8f6c3372678fc032dd3

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