# django-extra-field-validation
![PyPI](https://img.shields.io/pypi/v/django-extra-field-validation) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-extra-field-validation) ![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-extra-field-validation) [![Downloads](https://pepy.tech/badge/django-extra-field-validation)](https://pepy.tech/project/django-extra-field-validation)
[![CI Test](https://github.com/tj-django/django-extra-field-validation/actions/workflows/test.yml/badge.svg)](https://github.com/tj-django/django-extra-field-validation/actions/workflows/test.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6973bc063f1142afb66d897261d8f8f5)](https://www.codacy.com/gh/tj-django/django-extra-field-validation/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=tj-django/django-extra-field-validation\&utm_campaign=Badge_Grade) [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/6973bc063f1142afb66d897261d8f8f5)](https://www.codacy.com/gh/tj-django/django-extra-field-validation/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=tj-django/django-extra-field-validation\&utm_campaign=Badge_Coverage)
## Table of Contents
* [Background](#background)
* [Installation](#installation)
* [Usage](#usage)
* [Require all fields](#require-all-fields)
* [Require at least one field in a collection](#require-at-least-one-field-in-a-collection)
* [Optionally require at least one field in a collection](#optionally-require-at-least-one-field-in-a-collection)
* [Conditionally require all fields](#conditionally-require-all-fields)
* [Conditionally require at least one field in a collection](#conditionally-require-at-least-one-field-in-a-collection)
* [Model Attributes](#model-attributes)
* [License](#license)
* [TODO's](#todos)
## Background
This package aims to provide tools needed to define custom field validation logic which can be used independently or with
django forms, test cases, API implementation or any model operation that requires saving data to the database.
This can also be extended by defining check constraints if needed but currently validation
will only be handled at the model level.
## Installation
```shell script
pip install django-extra-field-validation
```
## Usage
### Require all fields
```py
from django.db import models
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
REQUIRED_FIELDS = ['amount'] # Always requires an amount to create the instance.
```
Example
```python
In [1]: from decimal import Decimal
In [2]: from demo.models import TestModel
In [3]: TestModel.objects.create(fixed_price=Decimal('3.00'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'amount': ValidationError([u'Please provide a value for: "amount".'])}
```
### Require at least one field in a collection
```py
from django.db import models
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
REQUIRED_TOGGLE_FIELDS = [
['amount', 'fixed_price', 'percentage'], # Require only one of the following fields.
]
```
Example
```python
In [1]: from decimal import Decimal
In [2]: from demo.models import TestModel
In [3]: TestModel.objects.create(amount=Decimal('2.50'), fixed_price=Decimal('3.00'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'fixed_price': ValidationError([u'Please provide only one of: Amount, Fixed price, Percentage'])}
```
### Optionally require at least one field in a collection
```py
from django.db import models
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
OPTIONAL_TOGGLE_FIELDS = [
['fixed_price', 'percentage'] # Optionally validates that only fixed price/percentage are provided when present.
]
```
Example
```python
In [1]: from decimal import Decimal
In [2]: from demo.models import TestModel
In [3]: first_obj = TestModel.objects.create(amount=Decimal('2.0'))
In [4]: second_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'))
In [5]: third_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'), percentage=Decimal('10.0'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'percentage': ValidationError([u'Please provide only one of: Fixed price, Percentage'])}
```
### Conditionally require all fields
```py
from django.db import models
from django.conf import settings
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
CONDITIONAL_REQUIRED_FIELDS = [
(
lambda instance: instance.user.is_active, ['amount', 'percentage'],
),
]
```
Example
```python
In [1]: from decimal import Decimal
in [2]: from django.contrib.auth import get_user_model
In [3]: from demo.models import TestModel
In [4]: user = get_user_model().objects.create(username='test', is_active=True)
In [5]: first_obj = TestModel.objects.create(user=user, amount=Decimal('2.0'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {u'percentage': ValidationError([u'Please provide a value for: "percentage"'])}
```
### Conditionally require at least one field in a collection
```py
from django.db import models
from django.conf import settings
from extra_validator import FieldValidationMixin
class TestModel(FieldValidationMixin, models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)
CONDITIONAL_REQUIRED_TOGGLE_FIELDS = [
(
lambda instance: instance.user.is_active, ['fixed_price', 'percentage', 'amount'],
),
]
```
Example
```python
In [1]: from decimal import Decimal
in [2]: from django.contrib.auth import get_user_model
In [3]: from demo.models import TestModel
In [4]: user = get_user_model().objects.create(username='test', is_active=True)
In [5]: first_obj = TestModel.objects.create(user=user)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'__all__': ValidationError([u'Please provide a valid value for any of the following fields: Fixed price, Percentage, Amount'])}
In [6]: second_obj = TestModel.objects.create(user=user, amount=Decimal('2'), fixed_price=Decimal('2'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: {'__all__': ValidationError([u'Please provide only one of the following fields: Fixed price, Percentage, Amount'])}
```
## Model Attributes
This is done using model attributes below.
```py
# A list of required fields
REQUIRED_FIELDS = []
# A list of fields with at most one required.
REQUIRED_TOGGLE_FIELDS = []
# A list of field with at least one required.
REQUIRED_MIN_FIELDS = []
# Optional list of fields with at most one required.
OPTIONAL_TOGGLE_FIELDS = []
# Conditional field required list of tuples the condition a boolean or a callable.
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Both 'first_name' or 'last_name'
# If condition is True ensure that all fields are set
CONDITIONAL_REQUIRED_FIELDS = []
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Either 'first_name' or 'last_name'
# If condition is True ensure that at most one field is set
CONDITIONAL_REQUIRED_TOGGLE_FIELDS = []
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : At least 'first_name' or 'last_name' provided or both
# If condition is True ensure that at least one field is set
CONDITIONAL_REQUIRED_MIN_FIELDS = []
# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Both 'first_name' and 'last_name' isn't provided
# If condition is True ensure none of the fields are provided
CONDITIONAL_REQUIRED_EMPTY_FIELDS = []
```
## License
django-extra-field-validation is distributed under the terms of both
* [MIT License](https://choosealicense.com/licenses/mit)
* [Apache License, Version 2.0](https://choosealicense.com/licenses/apache-2.0)
at your option.
## TODO's
* \[ ] Support `CONDITIONAL_NON_REQUIRED_TOGGLE_FIELDS`
* \[ ] Support `CONDITIONAL_NON_REQUIRED_FIELDS`
* \[ ] Move to support class and function based validators that use the instance object this should enable cross field model validation.
Raw data
{
"_id": null,
"home_page": "https://github.com/tj-django/django-extra-field-validation.git",
"name": "django-extra-field-validation",
"maintainer": "Tonye Jack",
"docs_url": null,
"requires_python": ">=2.6",
"maintainer_email": "jtonye@ymail.com",
"keywords": "django,model validation,django models,django object validation,field validation,conditional validation,cross field validation,django validation,django validators,django custom validation",
"author": "Tonye Jack",
"author_email": "jtonye@ymail.com",
"download_url": "https://files.pythonhosted.org/packages/0e/d3/97b16a728579da3de1e047be9aed2f294a66b47974c1645618ecb9e07492/django-extra-field-validation-1.2.3.tar.gz",
"platform": null,
"description": "# django-extra-field-validation\n\n![PyPI](https://img.shields.io/pypi/v/django-extra-field-validation) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-extra-field-validation) ![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-extra-field-validation) [![Downloads](https://pepy.tech/badge/django-extra-field-validation)](https://pepy.tech/project/django-extra-field-validation)\n\n[![CI Test](https://github.com/tj-django/django-extra-field-validation/actions/workflows/test.yml/badge.svg)](https://github.com/tj-django/django-extra-field-validation/actions/workflows/test.yml)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6973bc063f1142afb66d897261d8f8f5)](https://www.codacy.com/gh/tj-django/django-extra-field-validation/dashboard?utm_source=github.com\\&utm_medium=referral\\&utm_content=tj-django/django-extra-field-validation\\&utm_campaign=Badge_Grade) [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/6973bc063f1142afb66d897261d8f8f5)](https://www.codacy.com/gh/tj-django/django-extra-field-validation/dashboard?utm_source=github.com\\&utm_medium=referral\\&utm_content=tj-django/django-extra-field-validation\\&utm_campaign=Badge_Coverage)\n\n## Table of Contents\n\n* [Background](#background)\n* [Installation](#installation)\n* [Usage](#usage)\n * [Require all fields](#require-all-fields)\n * [Require at least one field in a collection](#require-at-least-one-field-in-a-collection)\n * [Optionally require at least one field in a collection](#optionally-require-at-least-one-field-in-a-collection)\n * [Conditionally require all fields](#conditionally-require-all-fields)\n * [Conditionally require at least one field in a collection](#conditionally-require-at-least-one-field-in-a-collection)\n* [Model Attributes](#model-attributes)\n* [License](#license)\n* [TODO's](#todos)\n\n## Background\n\nThis package aims to provide tools needed to define custom field validation logic which can be used independently or with\ndjango forms, test cases, API implementation or any model operation that requires saving data to the database.\n\nThis can also be extended by defining check constraints if needed but currently validation\nwill only be handled at the model level.\n\n## Installation\n\n```shell script\npip install django-extra-field-validation\n```\n\n## Usage\n\n### Require all fields\n\n```py\n\nfrom django.db import models\nfrom extra_validator import FieldValidationMixin\n\n\nclass TestModel(FieldValidationMixin, models.Model):\n amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)\n fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)\n percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)\n\n REQUIRED_FIELDS = ['amount'] # Always requires an amount to create the instance.\n```\n\nExample\n\n```python\nIn [1]: from decimal import Decimal\n\nIn [2]: from demo.models import TestModel\n\nIn [3]: TestModel.objects.create(fixed_price=Decimal('3.00'))\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n...\n\nValueError: {'amount': ValidationError([u'Please provide a value for: \"amount\".'])}\n\n```\n\n### Require at least one field in a collection\n\n```py\n\nfrom django.db import models\nfrom extra_validator import FieldValidationMixin\n\n\nclass TestModel(FieldValidationMixin, models.Model):\n amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)\n fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)\n percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)\n\n REQUIRED_TOGGLE_FIELDS = [\n ['amount', 'fixed_price', 'percentage'], # Require only one of the following fields.\n ]\n\n```\n\nExample\n\n```python\nIn [1]: from decimal import Decimal\n\nIn [2]: from demo.models import TestModel\n\nIn [3]: TestModel.objects.create(amount=Decimal('2.50'), fixed_price=Decimal('3.00'))\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n...\n\nValueError: {'fixed_price': ValidationError([u'Please provide only one of: Amount, Fixed price, Percentage'])}\n\n```\n\n### Optionally require at least one field in a collection\n\n```py\n\nfrom django.db import models\nfrom extra_validator import FieldValidationMixin\n\n\nclass TestModel(FieldValidationMixin, models.Model):\n amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)\n fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)\n percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)\n\n OPTIONAL_TOGGLE_FIELDS = [\n ['fixed_price', 'percentage'] # Optionally validates that only fixed price/percentage are provided when present.\n ]\n\n```\n\nExample\n\n```python\nIn [1]: from decimal import Decimal\n\nIn [2]: from demo.models import TestModel\n\nIn [3]: first_obj = TestModel.objects.create(amount=Decimal('2.0'))\n\nIn [4]: second_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'))\n\nIn [5]: third_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'), percentage=Decimal('10.0'))\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n...\n\nValueError: {'percentage': ValidationError([u'Please provide only one of: Fixed price, Percentage'])}\n\n```\n\n### Conditionally require all fields\n\n```py\n\nfrom django.db import models\nfrom django.conf import settings\nfrom extra_validator import FieldValidationMixin\n\n\nclass TestModel(FieldValidationMixin, models.Model):\n user = models.ForeignKey(settings.AUTH_USER_MODEL)\n\n amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)\n fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)\n percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)\n\n CONDITIONAL_REQUIRED_FIELDS = [\n (\n lambda instance: instance.user.is_active, ['amount', 'percentage'],\n ),\n ]\n\n```\n\nExample\n\n```python\nIn [1]: from decimal import Decimal\n\nin [2]: from django.contrib.auth import get_user_model\n\nIn [3]: from demo.models import TestModel\n\nIn [4]: user = get_user_model().objects.create(username='test', is_active=True)\n\nIn [5]: first_obj = TestModel.objects.create(user=user, amount=Decimal('2.0'))\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n...\n\nValueError: {u'percentage': ValidationError([u'Please provide a value for: \"percentage\"'])}\n\n```\n\n### Conditionally require at least one field in a collection\n\n```py\n\nfrom django.db import models\nfrom django.conf import settings\nfrom extra_validator import FieldValidationMixin\n\n\nclass TestModel(FieldValidationMixin, models.Model):\n user = models.ForeignKey(settings.AUTH_USER_MODEL)\n\n amount = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)\n fixed_price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)\n percentage = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True)\n\n CONDITIONAL_REQUIRED_TOGGLE_FIELDS = [\n (\n lambda instance: instance.user.is_active, ['fixed_price', 'percentage', 'amount'],\n ),\n ]\n```\n\nExample\n\n```python\nIn [1]: from decimal import Decimal\n\nin [2]: from django.contrib.auth import get_user_model\n\nIn [3]: from demo.models import TestModel\n\nIn [4]: user = get_user_model().objects.create(username='test', is_active=True)\n\nIn [5]: first_obj = TestModel.objects.create(user=user)\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n...\n\nValueError: {'__all__': ValidationError([u'Please provide a valid value for any of the following fields: Fixed price, Percentage, Amount'])}\n\nIn [6]: second_obj = TestModel.objects.create(user=user, amount=Decimal('2'), fixed_price=Decimal('2'))\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n...\n\nValueError: {'__all__': ValidationError([u'Please provide only one of the following fields: Fixed price, Percentage, Amount'])}\n```\n\n## Model Attributes\n\nThis is done using model attributes below.\n\n```py\n# A list of required fields\nREQUIRED_FIELDS = []\n\n# A list of fields with at most one required.\nREQUIRED_TOGGLE_FIELDS = []\n\n# A list of field with at least one required.\nREQUIRED_MIN_FIELDS = []\n\n# Optional list of fields with at most one required.\nOPTIONAL_TOGGLE_FIELDS = []\n\n# Conditional field required list of tuples the condition a boolean or a callable.\n# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Both 'first_name' or 'last_name'\n# If condition is True ensure that all fields are set\nCONDITIONAL_REQUIRED_FIELDS = []\n\n# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Either 'first_name' or 'last_name'\n# If condition is True ensure that at most one field is set\nCONDITIONAL_REQUIRED_TOGGLE_FIELDS = []\n\n# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : At least 'first_name' or 'last_name' provided or both\n# If condition is True ensure that at least one field is set\nCONDITIONAL_REQUIRED_MIN_FIELDS = []\n\n# [(lambda user: user.is_admin, ['first_name', 'last_name'])] : Both 'first_name' and 'last_name' isn't provided\n# If condition is True ensure none of the fields are provided\nCONDITIONAL_REQUIRED_EMPTY_FIELDS = []\n\n```\n\n## License\n\ndjango-extra-field-validation is distributed under the terms of both\n\n* [MIT License](https://choosealicense.com/licenses/mit)\n* [Apache License, Version 2.0](https://choosealicense.com/licenses/apache-2.0)\n\nat your option.\n\n## TODO's\n\n* \\[ ] Support `CONDITIONAL_NON_REQUIRED_TOGGLE_FIELDS`\n* \\[ ] Support `CONDITIONAL_NON_REQUIRED_FIELDS`\n* \\[ ] Move to support class and function based validators that use the instance object this should enable cross field model validation.\n",
"bugtrack_url": null,
"license": "MIT/Apache-2.0",
"summary": "Extra django field validation.",
"version": "1.2.3",
"split_keywords": [
"django",
"model validation",
"django models",
"django object validation",
"field validation",
"conditional validation",
"cross field validation",
"django validation",
"django validators",
"django custom validation"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4f0432af1303b196027c9071fc7817f4a2f2b3c460465be6b2c8e06cc8107953",
"md5": "57e910ae61f406dc54811c0ad7098b25",
"sha256": "e6bc1838b51456f2e4120e550357e3732812fce833d8b5329a953833ca1406fe"
},
"downloads": -1,
"filename": "django_extra_field_validation-1.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "57e910ae61f406dc54811c0ad7098b25",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=2.6",
"size": 11899,
"upload_time": "2023-01-14T08:08:43",
"upload_time_iso_8601": "2023-01-14T08:08:43.802507Z",
"url": "https://files.pythonhosted.org/packages/4f/04/32af1303b196027c9071fc7817f4a2f2b3c460465be6b2c8e06cc8107953/django_extra_field_validation-1.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0ed397b16a728579da3de1e047be9aed2f294a66b47974c1645618ecb9e07492",
"md5": "cb77587cbdd4381308c35311b4298b28",
"sha256": "7e682290b33082be665b08b427e2ab0c6b729569f480a52e785e473e716c8458"
},
"downloads": -1,
"filename": "django-extra-field-validation-1.2.3.tar.gz",
"has_sig": false,
"md5_digest": "cb77587cbdd4381308c35311b4298b28",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=2.6",
"size": 11095,
"upload_time": "2023-01-14T08:08:45",
"upload_time_iso_8601": "2023-01-14T08:08:45.945312Z",
"url": "https://files.pythonhosted.org/packages/0e/d3/97b16a728579da3de1e047be9aed2f294a66b47974c1645618ecb9e07492/django-extra-field-validation-1.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-14 08:08:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "tj-django",
"github_project": "django-extra-field-validation.git",
"lcname": "django-extra-field-validation"
}