Skip to main content

A small, extensible python library to deal with web validations !

Project description

simpleValidator
===============

[![PyPI version](https://badge.fury.io/py/simpleValidator.png)](http://badge.fury.io/py/simpleValidator) [![Build Status](https://travis-ci.org/markleent/simpleValidator.png?branch=master)](https://travis-ci.org/markleent/simpleValidator) [![Coverage Status](https://coveralls.io/repos/markleent/simpleValidator/badge.png?branch=master)](https://coveralls.io/r/markleent/simpleValidator?branch=master) [![Downloads](https://pypip.in/d/simpleValidator/badge.png)](https://pypi.python.org/pypi/simpleValidator/) [![License](https://pypip.in/license/simpleValidator/badge.png)](https://pypi.python.org/pypi/simpleValidator/)


A small, extensible python 2 (and 3 compatible !) library to deal with web validations !

simpleValidator (or just Validator actually), comes from the need of having a simple and straightforward validation library in python.

Sure some heavy players already exists, like WTForm, but take it as both a challenge and having somefun :), it is also inspired by the simplicity of [Laravel](https://github.com/laravel/laravel)'s own validator class

The library is standalone, rules are built the python way, in a module and easy to implement, the library is extensible as well !

Where to use it ? in [Flask](https://github.com/mitsuhiko/flask) for example for people who need very simple validations

N.B. : while i have years of experiences in php, i am mostly a newbie in python, this was a good idea to test on, and my very first OSS project as well :)

Installing
----------

1. pip install simpleValidator
2. git clone https://github.com/markleent/simpleValidator.git
a. cd simpleValidator
b. run python setup.py
3. wget https://github.com/markleent/simpleValidator/archive/master.zip (or download through your browser !)
a. unzip file
b. cd simpleValidator-*
c. run python setup.py


Use case:
---------

```python
from simplevalidator import Validator

my_items_to_test = {
'name': 'myusername',
'email': 'myfakeemail@fakedomain.com',
}

my_validation_rules = {
'name': 'required',
'email': 'required|email',
}

v = Validator()
v.make(fields = my_items_to_test, rules = my_validation_rules)

### alternatively from the class constructor :
v = Validator(fields = my_items_to_test, rules = my_validation_rules)

### returns True if the validation failed, False if passed
if v.fails():
# do something
else:
# do something else

### returns a list of error messages
v.errors()

### returns the list of failed validation only (no error message)
v.failed()
```

Custom Validation !
-------------------

simpleValidator is extensible at runtime ! you can add in your own validation rules and messages !

```python
from simplevalidator import Validator

my_items_to_test = {
'name': 'myusername',
'email': 'myfakeemail@fakedomain.com',
}

my_validation_rules = {
'name': 'required|mycustomrule',
'email': 'required|email',
}

my_validation_messages = {
'mycustomrule': '{0} is not equal to 1 !'
}

def mycustomrule(value):
return value == 1

v = Validator()
v.extend({'mycustomrule': mycustomrule})

v.make(fields = my_items_to_test, rules = my_validation_rules, messages = my_validation_messages)

print(v.fails())
### outputs True

print(v.errors())
### outputs ['name is not equal to 1 !']

```

i18n
----

simpleValidator supports i18n, through gettext, for now a list of 3 languages are available : English (en), French (fr), Japanese (ja)

```python
from simplevalidator import Validator, i18n

i18n.switch_language('fr')

fields = {'username': ''}
rules = {'username': 'required'}

v = Validator(fields = fields, rules = rules)

print(v.errors())
## > ['username est requis']



i18n.switch_language('ja')

v = Validator(fields = fields, rules = rules)

print(v.errors())
## > ['usernameは必ず指定してください。']
```


Rules List
==========

* required, field to validate must contain a value
* email, field must be a valid email
* alpha, field must contain alphabetical characters only
* alpha_num, field must contain alphabetical characters and/or numbers
* alpha_dash, field must contain alphabetical characters, numbers, dashes and underscores
* numeric, field must contain a numerical value
* integer, field must be an integer only
* posinteger, field must be a positive integer only
* min, depending on field:
- string, size must be at least of min value (ex min = 5, "mystring" is valid)
- numerical, value must be at least higher or equal to min value (ex min = 22, 39 is valid)
* max, depending on field:
- string, size must at most be of max value (ex max = 10, "hello world" is NOT valid)
- numerical, value must be lower or equal than max value (ext max = 10, 12 is NOT valid)
* between, depending on value:
- string, size must be between the boundary values (ex between = (5,10), "hello !" is valid)
- numerical, value must be between the 2 values
* ip4, field must be a valid ipv4 address
* ip6, field must be a valid ipv6 address
* date, field must be a valid date, corresponding to a specific template
* url, field must be a valid url (https, and port are allowed)


To Do
-----

- Add more validation rules with time...
- Add support for file validation (mime type, file size etc...)
- Add support for json validation (through templates)




Is it UnitTested ?
------------------

All tests files where merged for conveniance in one file that can be either run :

1. python setup.py test
2. python tests.py




License
-------

Released under a ([MIT](LICENSE)) license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simpleValidator-0.0.6.0.tar.gz (20.6 kB view details)

Uploaded Source

File details

Details for the file simpleValidator-0.0.6.0.tar.gz.

File metadata

File hashes

Hashes for simpleValidator-0.0.6.0.tar.gz
Algorithm Hash digest
SHA256 d4d7da8716c6535400a8bde91a154547ddcdd91033ef11b029f35c9250c9d78b
MD5 75636ddf267b069a3fb3d106ba94cf1b
BLAKE2b-256 64cea33e4bf463015123c14ded8f2200777231087d35b6cf802a6e74427a63c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page