| Name | fieldrouter JSON |
| Version |
1.0.4
JSON |
| download |
| home_page | None |
| Summary | Data model validation for nested data routes |
| upload_time | 2024-08-21 09:36:04 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.10 |
| license | MIT |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# 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
```py
data = {"a": {"aa": {"aaa": [10, 20, 30]}}}
```
You would typically need to write Pydantic models for each level
```py
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):
```py
from fieldrouter import Routing, RoutingModel
class A(RoutingModel):
thirty: Routing(int, "a.aa.aaa.2")
thirty = A.model_validate(data).thirty
```
## Route syntax
### Relative references
You can reference another field in a route by prefixing its field name by a dot, such as `x` here:
```py
class B(RoutingModel):
x: Routing(int, "foo.0.etc")
b1: Routing(int, ".x.0.bar")
b2: Routing(int, ".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:
```py
class B(RoutingModel):
x: Routing(int, "foo.0.etc")
b1: Routing(int, "foo.0.etc.0.bar")
b2: Routing(int, "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.
```py
class I(RoutingModel):
full: Routing(dict, ".")
```
This will just give you the entire input, in this case as a dict field named `full`.
## Generics
> **Note**: deprecated since v1.0
You can also write routing models in a more 'longform' way, using one model for the routes and
another for the types:
```py
from fieldrouter.generic 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):
```py
from fieldrouter.generic 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:
```py
model = What[Where].model_validate(data)
```
Raw data
{
"_id": null,
"home_page": null,
"name": "fieldrouter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Louis Maddox <louismmx@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/1c/94/673712625a1416d7deaff740eec072faab479889b1a8e17f1d6faef3f23b/fieldrouter-1.0.4.tar.gz",
"platform": null,
"description": "# fieldrouter: Data model validation for nested data routes\n\n`fieldrouter` is a Python library that provides helpers for modelling routes in highly nested structured data.\n\nIt should be considered for cases when exhaustively modelling the tree structures involved is\nsurplus to requirements (in other cases you would simply use Pydantic in the regular way),\nor perhaps if you want to specify 'routes' on an existing data model.\n\nFor example to access the number 30 in\n\n```py\ndata = {\"a\": {\"aa\": {\"aaa\": [10, 20, 30]}}}\n```\n\nYou would typically need to write Pydantic models for each level\n\n```py\nclass A(BaseModel):\n a: AA\n\nclass AA(BaseModel):\n aa: AAA\n\nclass AAA(BaseModel):\n aaa: list[int]\n\nthirty = A.model_validate(data).a.aa.aaa[2]\n```\n\nWith `fieldrouter` you would instead specify a 'route' for the subpath on a 'router' model\n(which is just a regular Pydantic model with default argument validation):\n\n```py\nfrom fieldrouter import Routing, RoutingModel\n\nclass A(RoutingModel):\n thirty: Routing(int, \"a.aa.aaa.2\")\n\nthirty = A.model_validate(data).thirty\n```\n\n## Route syntax\n\n### Relative references\n\nYou can reference another field in a route by prefixing its field name by a dot, such as `x` here:\n\n```py\nclass B(RoutingModel):\n x: Routing(int, \"foo.0.etc\")\n b1: Routing(int, \".x.0.bar\")\n b2: Routing(int, \".x.1.bar\")\n```\n\nThe prefix `.x` is substituted for `foo.0.etc` (the value of the Route for the field x).\n\nThis is equivalent to the following routes without references to the `x` field:\n\n```py\nclass B(RoutingModel):\n x: Routing(int, \"foo.0.etc\")\n b1: Routing(int, \"foo.0.etc.0.bar\")\n b2: Routing(int, \"foo.0.etc.1.bar\")\n```\n\nUse this to keep your subpaths readable.\n\n\n### The identity route\n\nSometimes when you're exploring nested data you want a reminder (or easy access to) the entire\ndata at a given route. This is available at the `.` route (the route string made up of a single\ndot). This is known as the 'identity' route.\n\n```py\nclass I(RoutingModel):\n full: Routing(dict, \".\")\n```\n\nThis will just give you the entire input, in this case as a dict field named `full`.\n\n## Generics\n\n> **Note**: deprecated since v1.0\n\nYou can also write routing models in a more 'longform' way, using one model for the routes and\nanother for the types:\n\n```py\nfrom fieldrouter.generic import RouterModel, Route\n\nclass Where(RouterModel):\n thirty: Route = \"a.aa.aaa.2\"\n```\n\nThen you can model the value at that route with a corresponding field on a 'routed' model\n(which is a generic model which takes the router as a type argument):\n\n```py\nfrom fieldrouter.generic import Routed, R\n\nclass What(Routed[R]):\n thirty: int\n```\n\nThen you can use the router class as a generic type argument to the instance of the routee:\n\n```py\nmodel = What[Where].model_validate(data)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Data model validation for nested data routes",
"version": "1.0.4",
"project_urls": {
"Homepage": "https://github.com/lmmx/fieldrouter",
"Repository": "https://github.com/lmmx/fieldrouter.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bc5d2832df00ddf294138ab23331e1ea204365428020e2d8a5d7ba1a94808fb5",
"md5": "9f6d9c5e77c95bd2859f9bc40fff339c",
"sha256": "20d274396d3d0d7394a79a521bf96f0f8e7f17cfa07c7fdfaaefc2723e2ede46"
},
"downloads": -1,
"filename": "fieldrouter-1.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9f6d9c5e77c95bd2859f9bc40fff339c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 6389,
"upload_time": "2024-08-21T09:36:02",
"upload_time_iso_8601": "2024-08-21T09:36:02.127106Z",
"url": "https://files.pythonhosted.org/packages/bc/5d/2832df00ddf294138ab23331e1ea204365428020e2d8a5d7ba1a94808fb5/fieldrouter-1.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1c94673712625a1416d7deaff740eec072faab479889b1a8e17f1d6faef3f23b",
"md5": "e98039cca5b665b95b155dd3094b42b3",
"sha256": "9f70cc84e5c558317682a31b5675b13c66fc83079f939afa55bb628ec0081a16"
},
"downloads": -1,
"filename": "fieldrouter-1.0.4.tar.gz",
"has_sig": false,
"md5_digest": "e98039cca5b665b95b155dd3094b42b3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 5189,
"upload_time": "2024-08-21T09:36:04",
"upload_time_iso_8601": "2024-08-21T09:36:04.978787Z",
"url": "https://files.pythonhosted.org/packages/1c/94/673712625a1416d7deaff740eec072faab479889b1a8e17f1d6faef3f23b/fieldrouter-1.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-21 09:36:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "lmmx",
"github_project": "fieldrouter",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fieldrouter"
}