# pylidator
pylidator is a validation framework for Python projects.
Many business systems have complex validation rules. This library provides a method of organizing those rules for
convenience and testability. A `validator` method is written for each rule (or group of rules), which simply returns a
list of errors if any are found.
## Validators
A validator method checks the validity of one or a closely-related group of
assertions about a piece of data. They all look basically like this:
```python
import pylidator
@pylidator.validator(of="child")
def child_is_valid(child):
messages = []
if child['age'] >= 18:
messages.append({"age": "Child is too old."}
if child['type'] != 'human':
messages.append({"type": "Only humans allowed."}
return messages
```
(Alternately, you can return just a dict of `{field: message}` items.)
## Validating Something
Once you have authored some `@pylidator.validator` methods as above, you can use them! Try this:
```python
import pylidator
objs = {
'name': "Mrs. Teacher's Class",
'children': [
{'name': "Joe", 'age': 15, 'type': 'human'},
{'name': "Sarah", 'age': 19, 'type': 'human'},
]
}
# Define a provider
def _provide_child(obj):
for i, c in enumerate(obj['children']):
yield c, {"description": "Child {}".format(i)}
providers = {"child": _provide_something} # "child" matches the `of` argument of the `@pylidator.validator`.
ret = pylidator.validate(objs, {pylidator.ERROR: [some_values_are_valid]}, providers=providers)
```
`child_is_valid` will be invoked once per child, and any that return something truthy will show as an ERROR.
## Function Reference
`@pylidator.validator` decorates any method that will be passed to `pylidator.validate`, and takes several optional parameters:
```
@pylidator.validator(of, requires=None, affects=None)
`of` specifies what provider the validator should use. The `validate` call needs an item in `providers`
that matches `of`.
`requires` (optional) can add additional context items, such as the current time or other services that can supply
data or settings to the validator. The requirement is fulfilled by passing `extra_context` to the `validate`
call, containing any items that are used in a `requires`.
`affects` (optional) is simply passed through to results. It can be used as guidance for UI/error reporting for
helping to resolve any resultant errors.
```
```
pylidator.validate(
obj, validators=None, providers=None, extra_context=None, field_name_mapper=None,
validation_type=None)
`obj` is the top-level object requiring validation.
`validators` is a dict of {level: list of `@pylidator.validator` objects}
`providers` is a dict of {of: func that takes obj and returns an iterable of some subobjects}
`extra_context` is a dict of other data that can be injected into `@pylidator.validator` with `requires`.
`field_name_mapper` is a string->string func that converts field names given in returned errors into verbose names.
`validation_type` is added as documentation into the error object.
`logging` If set to False, disables logging of validation results.
`why` String added to logging to identify the logpoint.
```
Raw data
{
"_id": null,
"home_page": "https://github.com/mathandpencil/pylidator",
"name": "pylidator",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "validation validate",
"author": "Scott Stafford",
"author_email": "scott.stafford+pylidator@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/fa/bc/a6b996dc0609b9c5abb4a1d8401e482df1c29c185d7f8e7c0e9a11649b99/pylidator-1.1.6.tar.gz",
"platform": null,
"description": "# pylidator\npylidator is a validation framework for Python projects.\n\nMany business systems have complex validation rules. This library provides a method of organizing those rules for\nconvenience and testability. A `validator` method is written for each rule (or group of rules), which simply returns a\nlist of errors if any are found.\n\n## Validators\n\nA validator method checks the validity of one or a closely-related group of\nassertions about a piece of data. They all look basically like this:\n\n```python\nimport pylidator\n\n@pylidator.validator(of=\"child\")\ndef child_is_valid(child):\n messages = []\n\n if child['age'] >= 18:\n messages.append({\"age\": \"Child is too old.\"}\n\n if child['type'] != 'human':\n messages.append({\"type\": \"Only humans allowed.\"}\n\n return messages\n```\n\n(Alternately, you can return just a dict of `{field: message}` items.)\n\n## Validating Something\n\nOnce you have authored some `@pylidator.validator` methods as above, you can use them! Try this:\n\n```python\nimport pylidator\n\nobjs = {\n 'name': \"Mrs. Teacher's Class\",\n 'children': [\n {'name': \"Joe\", 'age': 15, 'type': 'human'},\n {'name': \"Sarah\", 'age': 19, 'type': 'human'},\n ]\n}\n\n# Define a provider\ndef _provide_child(obj):\n for i, c in enumerate(obj['children']):\n yield c, {\"description\": \"Child {}\".format(i)}\n\nproviders = {\"child\": _provide_something} # \"child\" matches the `of` argument of the `@pylidator.validator`.\nret = pylidator.validate(objs, {pylidator.ERROR: [some_values_are_valid]}, providers=providers)\n```\n\n`child_is_valid` will be invoked once per child, and any that return something truthy will show as an ERROR.\n\n## Function Reference\n\n`@pylidator.validator` decorates any method that will be passed to `pylidator.validate`, and takes several optional parameters:\n\n```\n@pylidator.validator(of, requires=None, affects=None)\n\n`of` specifies what provider the validator should use. The `validate` call needs an item in `providers`\n that matches `of`.\n`requires` (optional) can add additional context items, such as the current time or other services that can supply\n data or settings to the validator. The requirement is fulfilled by passing `extra_context` to the `validate`\n call, containing any items that are used in a `requires`.\n`affects` (optional) is simply passed through to results. It can be used as guidance for UI/error reporting for\n helping to resolve any resultant errors.\n```\n\n```\npylidator.validate(\n obj, validators=None, providers=None, extra_context=None, field_name_mapper=None, \n validation_type=None)\n\n`obj` is the top-level object requiring validation.\n`validators` is a dict of {level: list of `@pylidator.validator` objects}\n`providers` is a dict of {of: func that takes obj and returns an iterable of some subobjects}\n`extra_context` is a dict of other data that can be injected into `@pylidator.validator` with `requires`.\n`field_name_mapper` is a string->string func that converts field names given in returned errors into verbose names.\n`validation_type` is added as documentation into the error object.\n`logging` If set to False, disables logging of validation results.\n`why` String added to logging to identify the logpoint.\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A practical system for organizing validation rules.",
"version": "1.1.6",
"project_urls": {
"Homepage": "https://github.com/mathandpencil/pylidator"
},
"split_keywords": [
"validation",
"validate"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5d0d8617df7ef1b1c6b80637f366a0344f67c2a49f57e85c8690da9bcaad3024",
"md5": "c783b869df74ed1b65b92478efbb11fe",
"sha256": "633b61f754e51f8a54c74ff2f183da0524d1fcf030d7f828fb99a02bdb6b8da5"
},
"downloads": -1,
"filename": "pylidator-1.1.6-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "c783b869df74ed1b65b92478efbb11fe",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 13034,
"upload_time": "2024-11-01T15:34:28",
"upload_time_iso_8601": "2024-11-01T15:34:28.833246Z",
"url": "https://files.pythonhosted.org/packages/5d/0d/8617df7ef1b1c6b80637f366a0344f67c2a49f57e85c8690da9bcaad3024/pylidator-1.1.6-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fabca6b996dc0609b9c5abb4a1d8401e482df1c29c185d7f8e7c0e9a11649b99",
"md5": "6521266bc791639940f9dee0b02a6a33",
"sha256": "f0a0504d340f34e55b8617d632ee6184565001aa7693b7acf7dc99f09ec18640"
},
"downloads": -1,
"filename": "pylidator-1.1.6.tar.gz",
"has_sig": false,
"md5_digest": "6521266bc791639940f9dee0b02a6a33",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12586,
"upload_time": "2024-11-01T15:34:30",
"upload_time_iso_8601": "2024-11-01T15:34:30.153245Z",
"url": "https://files.pythonhosted.org/packages/fa/bc/a6b996dc0609b9c5abb4a1d8401e482df1c29c185d7f8e7c0e9a11649b99/pylidator-1.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-01 15:34:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mathandpencil",
"github_project": "pylidator",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "pylidator"
}