Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/romain-li/django-validator
Django validator with decorators.
https://github.com/romain-li/django-validator
Last synced: 3 months ago
JSON representation
Django validator with decorators.
- Host: GitHub
- URL: https://github.com/romain-li/django-validator
- Owner: romain-li
- License: mit
- Created: 2016-03-21T03:01:47.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-02-27T18:36:17.000Z (over 4 years ago)
- Last Synced: 2024-07-11T19:05:45.712Z (4 months ago)
- Language: Python
- Size: 43 KB
- Stars: 15
- Watchers: 3
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-validator ![Travis-CI](https://travis-ci.org/romain-li/django-validator.svg)
A django-rest-framework tools for validate and extract param from request.
And also support normal django view functions.## Install:
```bash
pip install django_validator
```## Usage:
### Use default decorators and validators
```python
@GET('check_phone_type', type='int', validators='required | in: 1,2,3')
@POST('phone', validators='required | regex: \d{11}')
@POST('token', validators='required')
def post(request, check_phone_type, phone, token):
pass
```### Custom validator
```python
class PhoneNumberValidator(BaseValidator):
def is_valid(self, value, params):
return TrueValidatorRegistry.register('phone_number_validator', PhoneNumberValidator)
@POST('phone', validators='required | regex: \d{11} | phone_number_validator')# or
@POST('phone', validators='required | regex: \d{11}', validator_classes=[PhoneNumberValidator()])
```## Decorators
- GET
- POST
- POST_OR_GET
- FILE
- HEADER
- URI## Params for decorator
- name: The key of param in the request.
- related_name: The key of param to pass throw the function. Default is equals to name.
- verbose_name: The key for display in the validation error message. Default is equals to name.
- default: Default value for the param. Default is `None`.
- type: Auto check and convert the param type. Default is string.
- many: Convert the param to a list if set to `True`.
- separator: If set many to `True`, use this value to split the param.
- validators: String format validator, like `required | max: 1`.## Default types
- str, string
- int, integer
- float
- bool, boolean## Default validators
- required
- required_with
- required_without
- required_if
- max
- min
- between
- regex
- integer
- numeric
- in
- not_in
- ext_in
- ext_not_in## Run tests
scripts/test.sh## TODO List
- [ ] Be compatible with django framework, not django-rest-framework.