Metadata-Version: 2.1
Name: django-dynamic-model-validation
Version: 0.1.12
Summary: Extra django model validation.
Home-page: https://github.com/jackton1/django-dynamic-model-validation.git
Author: Tonye Jack
Author-email: jtonye@ymail.com
Maintainer: Tonye Jack
Maintainer-email: jtonye@ymail.com
License: MIT/Apache-2.0
Keywords: django,model validation,django models,django object validation,field validation,conditional validation,cross field validation,django validation,django validators,django custom validation
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Natural Language :: English
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Framework :: Django :: 1.11
Classifier: Framework :: Django :: 2.0
Classifier: Framework :: Django :: 2.1
Requires-Python: >=2.6
Description-Content-Type: text/x-rst
Requires-Dist: future (==0.17.1)
Requires-Dist: six (==1.14.0)
Provides-Extra: deploy
Requires-Dist: readme-renderer[md] ; extra == 'deploy'
Requires-Dist: bumpversion (==0.5.3) ; extra == 'deploy'
Provides-Extra: development
Requires-Dist: Django (>=1.11.18) ; extra == 'development'
Requires-Dist: pip-tools ; extra == 'development'
Requires-Dist: check-manifest (==0.37) ; extra == 'development'
Requires-Dist: future (==0.17.1) ; extra == 'development'
Requires-Dist: six (==1.14.0) ; extra == 'development'
Requires-Dist: tox (==3.1.2) ; extra == 'development'
Requires-Dist: pytest-django (==3.4.4) ; extra == 'development'
Requires-Dist: pluggy (>=0.7) ; extra == 'development'
Requires-Dist: mock (==2.0.0) ; extra == 'development'
Requires-Dist: codacy-coverage (==1.3.10) ; extra == 'development'
Requires-Dist: flake8 (==3.4.1) ; extra == 'development'
Requires-Dist: yamllint (==1.10.0) ; extra == 'development'
Requires-Dist: isort (==4.2.15) ; extra == 'development'
Provides-Extra: lint
Requires-Dist: flake8 (==3.4.1) ; extra == 'lint'
Requires-Dist: yamllint (==1.10.0) ; extra == 'lint'
Requires-Dist: isort (==4.2.15) ; extra == 'lint'
Provides-Extra: test
Requires-Dist: tox (==3.1.2) ; extra == 'test'
Requires-Dist: pytest-django (==3.4.4) ; extra == 'test'
Requires-Dist: pluggy (>=0.7) ; extra == 'test'
Requires-Dist: mock (==2.0.0) ; extra == 'test'
Requires-Dist: codacy-coverage (==1.3.10) ; extra == 'test'
Provides-Extra: tox
Requires-Dist: Django (>=1.11.18) ; extra == 'tox'
Requires-Dist: pip-tools ; extra == 'tox'
Requires-Dist: check-manifest (==0.37) ; extra == 'tox'

Dynamic model validation.
=========================

.. image:: https://badge.fury.io/py/django-dynamic-model-validation.svg
    :target: https://badge.fury.io/py/django-dynamic-model-validation
.. image:: https://travis-ci.org/jackton1/django-dynamic-model-validation.svg?branch=master
    :target: https://travis-ci.org/jackton1/django-dynamic-model-validation
.. image:: https://api.codacy.com/project/badge/Coverage/33797e94524e4277b476c051618ad495    
    :target: https://www.codacy.com/app/jackton1/django-dynamic-model-validation?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=jackton1/django-dynamic-model-validation&amp;utm_campaign=Badge_Coverage
.. image:: https://api.codacy.com/project/badge/Grade/33797e94524e4277b476c051618ad495
    :target: https://www.codacy.com/app/jackton1/django-dynamic-model-validation?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=jackton1/django-dynamic-model-validation&amp;utm_campaign=Badge_Grade



.. contents:: **Table of Contents**
    :backlinks: none

Introduction
------------
This package aims to provide tools needed to define custom field validation logic once 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 table check constraints if needed but currently validation
will only be handled at the model level.


Installation
------------

django-dynamic-model-validation is distributed on `PyPI <https://pypi.org>`_ as a universal
wheel and is available on Linux/macOS and Windows and supports
Python 2.7/3.5+ and PyPy.

.. code-block:: bash

    $ pip install django-dynamic-model-validation

.. code-block:: python

   INSTALLED_APPS = [
       ...
       'dynamic_validator',
   ]

Usage
-----
This provides model level validation which includes conditional validation, cross field validation,
required field validation etc.


This is done using model attributes below.

.. code-block:: python

    #  Using a list/iterable: [['a', 'b'], ['c', 'd']] which validates that a field from each item is provided.
    REQUIRED_TOGGLE_FIELDS = []

    # Using a list/iterable validates that all fields are provided.
    REQUIRED_FIELDS = []

    # Optional toggle fields list: [['a', 'b']] which runs the validation only when any of the fields are present.
    OPTIONAL_TOGGLE_FIELDS = []

    # Conditional field validation using a list of tuples the condition which could be boolean or a callable and the list/iterable of fields that are required if the condition evaluates to `True`.
    # [(condition, [fields]), (condition, fields)]

    # Using a callable CONDITIONAL_REQUIRED_FIELDS = [(lambda instance: instance.is_admin, ['a', 'd'])]
    # Using a boolean CONDITIONAL_REQUIRED_TOGGLE_FIELDS = [(True, ['b', 'c']), (True, ['d', f])] 
    # asserts that either 'b' or 'c' is provided and either 'd' or 'f'.
    # (Note: This can also be handled using REQUIRED_FIELDS/REQUIRED_TOGGLE_FIELDS)

    # Validates that all fields are present if the condition is True
    CONDITIONAL_REQUIRED_FIELDS = []
    # Validated at least one not both fields are provided if the condition is True.
    CONDITIONAL_REQUIRED_TOGGLE_FIELDS = []


[Validates] That only one of the listed fields is provided.
***********************************************************

.. code-block:: python

    from django.db import models
    from dynamic_validator import ModelFieldRequiredMixin


    class TestModel(ModelFieldRequiredMixin, 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'],
        ]

.. code-block:: bash

    $ python manage.py shell
    ...
    >>> from decimal import Decimal
    >>> from demo.models import TestModel
    >>> TestModel.objects.create(amount=Decimal('2.50'), fixed_price=Decimal('3.00'))
    ...
    ValueError: {'fixed_price': ValidationError([u'Please provide only one of: Amount, Fixed price, Percentage'])}

[Validates] That a field without a default is required.
*******************************************************

.. code-block:: python

    from django.db import models
    from dynamic_validator import ModelFieldRequiredMixin


    class TestModel(ModelFieldRequiredMixin, 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.

.. code-block:: bash

    $ python manage.py shell
    ...
    >>> from decimal import Decimal
    >>> from demo.models import TestModel
    >>> TestModel.objects.create(fixed_price=Decimal('3.00'))
    ...
    ValueError: {'amount': ValidationError([u'Please provide a value for: "amount".'])}


[Validates] That optional fields should only have one value if provided.
************************************************************************

.. code-block:: python

    from django.db import models
    from dynamic_validator import ModelFieldRequiredMixin


    class TestModel(ModelFieldRequiredMixin, 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.
        ]

.. code-block:: bash

    $ python manage.py shell
    ...
    >>> from decimal import Decimal
    >>> from demo.models import TestModel
    >>> first_obj = TestModel.objects.create(amount=Decimal('2.0'))
    >>> second_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'))
    >>> third_obj = TestModel.objects.create(amount=Decimal('2.0'), fixed_price=Decimal('3.00'), percentage=Decimal('10.0'))
    ...
    ValueError: {'percentage': ValidationError([u'Please provide only one of: Fixed price, Percentage'])}


[Validates] That when a user is active (ie. instance.user.is_active) both fields should be provided.
****************************************************************************************************

.. code-block:: python

    from django.db import models
    from django.conf import settings
    from dynamic_validator import ModelFieldRequiredMixin


    class TestModel(ModelFieldRequiredMixin, 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'],
            ),
        ]

.. code-block:: bash

    $ python manage.py shell
    ...
    >>> from decimal import Decimal
    >>> from django.contrib.auth import get_user_model
    >>> from demo.models import TestModel
    >>> user = get_user_model().objects.create(username='test', is_active=True)
    >>> first_obj = TestModel.objects.create(user=user, amount=Decimal('2.0'))
    ...
    ValueError: {u'percentage': ValidationError([u'Please provide a value for: "percentage"'])}

[Validates] That when a user is active (ie. instance.user.is_active) any of the fields should be provided.
**********************************************************************************************************

.. code-block:: python

    from django.db import models
    from django.conf import settings
    from dynamic_validator import ModelFieldRequiredMixin


    class TestModel(ModelFieldRequiredMixin, 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'],
            ),
        ]

.. code-block:: bash

    $ python manage.py shell
    ...
    >>> from decimal import Decimal
    >>> from django.contrib.auth import get_user_model
    >>> from demo.models import TestModel
    >>> user = get_user_model().objects.create(username='test', is_active=True)
    >>> first_obj = TestModel.objects.create(user=user)
    ...
    ValueError: {'__all__': ValidationError([u'Please provide a valid value for any of the following fields: Fixed price, Percentage, Amount'])}
    ...
    >>>first_obj = TestModel.objects.create(user=user, amount=Decimal('2'), fixed_price=Decimal('2'))
    ...
    ValueError: {'__all__': ValidationError([u'Please provide only one of the following fields: Fixed price, Percentage, Amount'])}
    ...


License
-------

django-dynamic-model-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
- Move to support class and function based validators that use the instance object this should enable cross field model validation.


