diff --git a/docs/README.md b/docs/README.md index 308e0f3..ef708a7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,8 +7,10 @@ django-extra-field-validation [![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) [![Total alerts](https://img.shields.io/lgtm/alerts/g/tj-django/django-extra-field-validation.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/tj-django/django-extra-field-validation/alerts/) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/tj-django/django-extra-field-validation.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/tj-django/django-extra-field-validation/context:python) -Introduction ------------- + +## Table of Contents + +## 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. @@ -16,22 +18,15 @@ This can also be extended by defining table check constraints if needed but curr will only be handled at the model level. -Installation ------------- +## Installation ```shell script pip install django-extra-field-validation ``` -Usage ------ -This provides model level validation which includes: - - - [Required field validation](#require-a-single-field) - - [Optional field validation](#optionally-required-fields) - - [Conditional field validation](#conditional-required-fields) +## Usage -### Require a single field +### Require all fields ```py @@ -44,10 +39,7 @@ class TestModel(FieldValidationMixin, models.Model): 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. - ] - + REQUIRED_FIELDS = ['amount'] # Always requires an amount to create the instance. ``` Example @@ -57,16 +49,16 @@ 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')) +In [3]: TestModel.objects.create(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'])} +ValueError: {'amount': ValidationError([u'Please provide a value for: "amount".'])} ``` -### Require all fields +### Require at least one field ```py @@ -79,7 +71,10 @@ class TestModel(FieldValidationMixin, models.Model): 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. + REQUIRED_TOGGLE_FIELDS = [ + ['amount', 'fixed_price', 'percentage'], # Require only one of the following fields. + ] + ``` Example @@ -89,16 +84,16 @@ In [1]: from decimal import Decimal In [2]: from demo.models import TestModel -In [3]: TestModel.objects.create(fixed_price=Decimal('3.00')) +In [3]: TestModel.objects.create(amount=Decimal('2.50'), fixed_price=Decimal('3.00')) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) ... -ValueError: {'amount': ValidationError([u'Please provide a value for: "amount".'])} +ValueError: {'fixed_price': ValidationError([u'Please provide only one of: Amount, Fixed price, Percentage'])} ``` -### Optionally required fields +### Optionally require at least one field in a collection ```py @@ -137,7 +132,7 @@ ValueError: {'percentage': ValidationError([u'Please provide only one of: Fixed ``` -### Conditional required fields +### Conditional require fields ```py @@ -182,7 +177,7 @@ ValueError: {u'percentage': ValidationError([u'Please provide a value for: "perc ``` -### Conditional required optional fields +### Conditional require optional fields ```py @@ -232,8 +227,7 @@ ValueError: {'__all__': ValidationError([u'Please provide only one of the follow ``` -Model Attributes ----------------- +## Model Attributes This is done using model attributes below. @@ -269,8 +263,7 @@ CONDITIONAL_REQUIRED_EMPTY_FIELDS = [] ``` -License -------- +## License django-extra-field-validation is distributed under the terms of both @@ -279,8 +272,7 @@ django-extra-field-validation is distributed under the terms of both 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. +## 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.