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)

Route syntax

Relative references

You can reference another field in a route by prefixing its field name by a dot, such as x here:

class Where(Router):
    x: Route = "foo.0.etc"
    b1: Route = ".x.0.bar"
    b2: Route = ".x.1.bar"

The prefix .x is substituted for foo.0.etc (the value of the Route for the field x).

This is equivalent to the following routes without references to the x field:

class Where(Router):
    x: Route = "foo.0.etc"
    b1: Route = "foo.0.etc.0.bar"
    b2: Route = "foo.0.etc.1.bar"

Use this to keep your subpaths readable.

The identity route

Sometimes when you're exploring nested data you want a reminder (or easy access to) the entire data at a given route. This is available at the . route (the route string made up of a single dot). This is known as the 'identity' route.

class Where(Router):
    full: Route = "."

class What(Routed[R]):
    full: dict

This will just give you the entire input, in this case as a dict under the field named full.

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-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

fieldrouter-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fieldrouter-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 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-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bbdfee4d6d1a99ef397e85f414943e0031cac81d2b33b2b559d00ad53f7be9c3
MD5 61825073e4ea4cd2c12d2f84fb030a08
BLAKE2b-256 17b6ebe39765fcfc710f3ea555f5183f16348511c24939cc2ed565aef473a611

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fieldrouter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a21dba1a4276c527ca8ec1993a881702c4d5d7ae5b5cdba09ae81055b1fbba12
MD5 2ab3d263a3692990e8c2df42cf6326b7
BLAKE2b-256 8f79a6f239a648d6255c0e722ee6e7def400a2428a417dc1828469a923f8ce06

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