Django Easy Validation |pypi version|
Django Easy Validation is a simple Django library that allows validation for forms via ajax in a simple and elegant way.
Installation
Install project with pip
pip install django-easy-validation
Usages
from django_easy_validation import Validator
from django.http import HttpResponse, JsonResponse
def store(request):
errors = Validator.validate(request, {
"name": "required|unique:rating_metrices|max:100|min:6",
"score": "required|numeric"
})
if errors:
return JsonResponse(errors, status=422, safe=False)
return JsonResponse('success', safe=False)
By default validator generated messages will be thrown, but messsages can be customized too. For example if you want to show custom messages based on validation attributes you can define it as follows
from django_easy_validation import Validator
from django.http import HttpResponse, JsonResponse
def store(request):
errors = Validator.validate(request, {
"name": "required|unique:rating_metrices|max:100|min:6",
"score": "required|numeric"
},{
'name.required': 'Name field cannot be empty',
'score.required': 'Score should be assigned to each name',
})
if errors:
return JsonResponse(errors, status=422, safe=False)
return JsonResponse('success', safe=False)
Advance Usages
Instead of writing rules inside the validator validate method you can import it from other files to make the code for elegant. For example: Create a user_rules.py file under rules directory and place all your rules and messages there.
user_rules.py
class UserRules:
valid_rules = {
'name': 'required|min:6|max:20',
'score': 'required|numeric',
}
messages = {
'required.name': 'Name field cannot be empty',
'required.score': 'You must add score for each name',
}
Now you can import user_rules.py in your views and use it as below
from django_easy_validation import Validator
from rules.user_rules import UserRules
def store(request):
metrices = json.loads(request.body)
errors = Validator.validate(request, UserRules.valid_rules, UserRules.messages)
if errors and is_ajax:
return JsonResponse(errors, status=422, safe=False)
Available Validation Rules
required
unique (unique:users)
max_character (min:10)
min_character (max:100)
email
numeric
boolean
string
uppercase
is_array
alphanumeric
lowercase
between
size
Build Locally
Clone the project
git clone https://github.com/sandysh/django-easy-validation.git
Go to the project directory
cd django-easy-validation
Build
python setup.py sdist
Publish
twine upload dist/*
Authors
Metadata
Release files for django-easy-validation 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-easy-validation-1.0.0.tar.gz | 4.2 kB | Without provenance | Details for django-easy-validation-1.0.0.tar.gz |
Release files / django-easy-validation-1.0.0.tar.gz
| Download URL | django-easy-validation-1.0.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2778acf964da04807aaa53e293a47437f94ff0541d9618c0b45e07d288d0a383
|
|
BLAKE2b-256 checksum How to use checksums |
3a57441b9c8a6324e0498ce760e049692645c1ffdae14a44a038cbe4854fb811
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Security for django-easy-validation 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.