django-easy-validation


Namedjango-easy-validation JSON
Version 0.1 PyPI version JSON
download
home_pagehttps://github.com/sandysh/django-easy-validation
SummaryEasy Validation for Django based projects especially for Ajax based request.
upload_time2024-03-29 12:38:37
maintainerNone
docs_urlNone
authorSandesh Satyal
requires_pythonNone
licenseMIT
keywords django-easy-validation validation form-validation form ajax ajax-validation easy-validation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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

```bash
  pip install django-easy-validation
```
    
## Usages

```python
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

```python
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
```python

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

```python

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

```javascript
required
min     (min:10)
max     (max:100)
unique  (unique:users)
email
numeric
boolean
string
uppercase

```
## Build Locally

Clone the project

```bash
  git clone https://github.com/sandysh/django-easy-validation.git
```

Go to the project directory

```bash
  cd django-easy-validation
```

Build

```bash
  python setup.py sdist
```

Publish

```bash
  twine upload dist/*
```


## Authors

- [@sandysh](https://www.github.com/sandysh)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sandysh/django-easy-validation",
    "name": "django-easy-validation",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "django-easy-validation validation form-validation form ajax ajax-validation easy-validation",
    "author": "Sandesh Satyal",
    "author_email": "sandeshsatyal@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/5b/7e/ee71578fa983e1ea1b3d8e8a52b2fa2aff2bdd707cc74b563f29028a4b6d/django-easy-validation-0.1.tar.gz",
    "platform": null,
    "description": "Django Easy Validation |pypi version|\r\n---------------------------------------\r\n\r\n\r\n\r\nDjango Easy Validation is a simple Django library that allows validation for forms via ajax in a simple and elegant way.\r\n\r\n\r\n\r\n## Installation\r\n\r\nInstall project with pip\r\n\r\n```bash\r\n  pip install django-easy-validation\r\n```\r\n    \r\n## Usages\r\n\r\n```python\r\nfrom django_easy_validation import Validator\r\nfrom django.http import HttpResponse, JsonResponse\r\n\r\n\r\n    def store(request):\r\n       errors = Validator.validate(request, {\r\n           \"name\": \"required|unique:rating_metrices|max:100|min:6\",\r\n           \"score\": \"required|numeric\"\r\n       })\r\n       if errors:\r\n           return JsonResponse(errors, status=422, safe=False)\r\n\r\n    return JsonResponse('success', safe=False)\r\n\r\n```\r\nBy 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\r\n\r\n```python\r\nfrom django_easy_validation import Validator\r\nfrom django.http import HttpResponse, JsonResponse\r\n\r\n\r\n    def store(request):\r\n       errors = Validator.validate(request, {\r\n           \"name\": \"required|unique:rating_metrices|max:100|min:6\",\r\n           \"score\": \"required|numeric\"\r\n       },{\r\n         'name.required': 'Name field cannot be empty',\r\n         'score.required': 'Score should be assigned to each name',\r\n      })\r\n       if errors:\r\n           return JsonResponse(errors, status=422, safe=False)\r\n\r\n    return JsonResponse('success', safe=False)\r\n```\r\n\r\n## Advance Usages\r\n\r\nInstead 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.\r\n\r\nuser_rules.py\r\n```python\r\n\r\nclass UserRules:\r\n\r\n    valid_rules = {\r\n        'name': 'required|min:6|max:20',\r\n        'score': 'required|numeric',\r\n    }\r\n\r\n    messages = {\r\n        'required.name': 'Name field cannot be empty',\r\n        'required.score': 'You must add score for each name',\r\n    }\r\n\r\n\r\n```\r\nNow you can import user_rules.py in your views and use it as below\r\n\r\n```python\r\n\r\nfrom django_easy_validation import Validator\r\nfrom rules.user_rules import UserRules\r\n\r\n\r\ndef store(request):\r\n    metrices = json.loads(request.body)\r\n    errors = Validator.validate(request, UserRules.valid_rules, UserRules.messages)\r\n    if errors and is_ajax:\r\n        return JsonResponse(errors, status=422, safe=False)\r\n\r\n```\r\n## Available Validation Rules\r\n\r\n```javascript\r\nrequired\r\nmin     (min:10)\r\nmax     (max:100)\r\nunique  (unique:users)\r\nemail\r\nnumeric\r\nboolean\r\nstring\r\nuppercase\r\n\r\n```\r\n## Build Locally\r\n\r\nClone the project\r\n\r\n```bash\r\n  git clone https://github.com/sandysh/django-easy-validation.git\r\n```\r\n\r\nGo to the project directory\r\n\r\n```bash\r\n  cd django-easy-validation\r\n```\r\n\r\nBuild\r\n\r\n```bash\r\n  python setup.py sdist\r\n```\r\n\r\nPublish\r\n\r\n```bash\r\n  twine upload dist/*\r\n```\r\n\r\n\r\n## Authors\r\n\r\n- [@sandysh](https://www.github.com/sandysh)\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Easy Validation for Django based projects especially for Ajax based request.",
    "version": "0.1",
    "project_urls": {
        "Homepage": "https://github.com/sandysh/django-easy-validation"
    },
    "split_keywords": [
        "django-easy-validation",
        "validation",
        "form-validation",
        "form",
        "ajax",
        "ajax-validation",
        "easy-validation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b7eee71578fa983e1ea1b3d8e8a52b2fa2aff2bdd707cc74b563f29028a4b6d",
                "md5": "989c1c8c09f48a23f19841ea8b452574",
                "sha256": "7b9e71e0ff7ca59299703d72fe92288dd4a0d5d9fc08b7adaf11f029ded97584"
            },
            "downloads": -1,
            "filename": "django-easy-validation-0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "989c1c8c09f48a23f19841ea8b452574",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4559,
            "upload_time": "2024-03-29T12:38:37",
            "upload_time_iso_8601": "2024-03-29T12:38:37.731290Z",
            "url": "https://files.pythonhosted.org/packages/5b/7e/ee71578fa983e1ea1b3d8e8a52b2fa2aff2bdd707cc74b563f29028a4b6d/django-easy-validation-0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-29 12:38:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sandysh",
    "github_project": "django-easy-validation",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "django-easy-validation"
}
        
Elapsed time: 4.19628s