Skip to main content

Data model validation for nested data routes

Project description

fieldrouter: Data model validation for nested data routes

fieldrouter is a Python library that provides helpers for modelling routes in highly nested structured data.

It should be considered for cases when exhaustively modelling the tree structures involved is surplus to requirements (in other cases you would simply use Pydantic in the regular way), or perhaps if you want to specify 'routes' on an existing data model.

For example to access the number 30 in

data = {"a": {"aa": {"aaa": [10, 20, 30]}}}

You would typically need to write Pydantic models for each level

class A(BaseModel):
    a: AA

class AA(BaseModel):
    aa: AAA

class AAA(BaseModel):
    aaa: list[int]

thirty = A.model_validate(data).a.aa.aaa[2]

With fieldrouter you would instead specify a 'route' for the subpath on a 'router' model (which is just a regular Pydantic model with default argument validation):

from fieldrouter import RouterModel, Route

class Where(RouterModel):
    thirty: Route = "a.aa.aaa.2"

Then you can model the value at that route with a corresponding field on a 'routed' model (which is a generic model which takes the router as a type argument):

from fieldrouter import Routed, R

class What(Routed[R]):
    thirty: int

Then you can use the router class as a generic type argument to the instance of the routee:

model = What[Where].model_validate(data)

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

fieldrouter-0.1.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

fieldrouter-0.1.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file fieldrouter-0.1.2.tar.gz.

File metadata

  • Download URL: fieldrouter-0.1.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.3 CPython/3.10.14 Linux/5.15.0-117-generic

File hashes

Hashes for fieldrouter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cd40778f1380e550aa5b95a5d0b12aa2bb72fb4e71e1915d4f17fb0b8dc21e10
MD5 291115c3731c67b67ca91d4913b4e726
BLAKE2b-256 ea043dcfb30a23277f0c8ce1bba6527c897a38dc48608a4115cbd55c9d08ec94

See more details on using hashes here.

File details

Details for the file fieldrouter-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fieldrouter-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.17.3 CPython/3.10.14 Linux/5.15.0-117-generic

File hashes

Hashes for fieldrouter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 42a1cd1034c83fa7dcc0a00f9e5873908abb79190d4651597b60f079ecdbe9a4
MD5 7ea26e10f9066102a87c4b605b16e2d8
BLAKE2b-256 a4e3af30c85891170949f6f939ec4f0b935ddff8eb93b7b9f67e4ea169f82d5d

See more details on using hashes here.

Supported by

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