Skip to main content

LIVR validator.

Project description

https://travis-ci.org/asholok/python-validator-livr.svg

README

NAME

LIVR.Validator - Lightweight validator supporting Language Independent Validation Rules Specification (LIVR)

Python 2 and 3 compatible

SYNOPSIS

Common usage:

from LIVR import Validator
Validator.Validator.set_default_auto_trim(True)

validator = Validator.Validator({
    'name':      'required',
    'email':     [ 'required', 'email' ],
    'gender':    { 'one_of' : ['male', 'female'] },
    'phone':     { 'max_length' : 10 },
    'password':  [ 'required', {'min_length' : 10} ],
    'password2': { 'equal_to_field' : 'password' }
})

valid_data = validator.validate(user_data)

if valid_data:
    save_user_data(valid_data)
else:
    some_error_hendler(validator.get_errors())

You can use filters separately or can combine them with validation:

validator = Validator.Validator({
    'email': [ 'required', 'trim', 'email', 'to_lc' ]
})

Feel free to register your own rules:

validator = Validator.Validator({
    'password': ['required', 'strong_password']
})

class StrongPassword(object):
    def __init__(self, *args):
        pass

    def __call__(self, value, unuse, unuse):
        value == None or value == '':
            return

        if len(value) < 6:
            return 'WEAK_PASSWORD'

validator.registerRules({ 'strong_password': StrongPassword})

Also you can use aliases for some case, but you must ensure that in aliase dict present required kyes ‘rules’ and ‘name’:

validator = Validator.Validator({
    'password': ['required', 'strong_password']
})

validator.register_aliased_rule({
    'name': 'strong_password',
    'rules': {'min_length' : 9},
    'error': 'WEAK_PASSWORD'
})

DESCRIPTION

See http://livr-spec.org for detailed documentation and list of supported rules.

Features:

  • Rules are declarative and language independent

  • Any number of rules for each field

  • Return together errors for all fields

  • Excludes all fields that do not have validation rules described

  • Has possibility to validatate complex hierarchical structures

  • Easy to describe and undersand rules

  • Returns understandable error codes(not error messages)

  • Easy to add own rules

  • Rules are be able to change results output (“trim”, “nested_object”, for example)

  • Multipurpose (user input validation, configs validation, contracts programming etc)

INSTALL

Install LIVR from PyPI using PIP:

sudo pip install LIVR

CLASS METHODS

Validator.Validator(livr, is_auto_trim)

Contructor creates validator objects. livr - validations rules. Rules description is available here - https://github.com/koorchik/LIVR

is_auto_trim - asks validator to trim all values before validation. Output will be also trimmed.

if is_auto_trim is undefined(or None) than default_auto_trim value will be used.

Validator.Validator.registerDefaultRules({“rule_name”: rule_builder })

rule_builder - is a function reference which will be called for building single rule validator.

class MyRule(object):
    def __init__(self, *args):
        rule_builders = args[0]
        # rule_builders - are rules from original validator
        # to allow you create new validator with all supported rules
        # validator = Validator(livr)
        # validator.register_rules(rule_builders)
        # validator.prepare()

    def __call__(self, value, all_values, output_array):
        if not_valid:
            return "SOME_ERROR_CODE"
        else:
            # some usefull code

Validator.Validator.register_default_rules( {"my_rule": MyRule} )

Then you can use “my_rule” for validation:

{
    'name1': 'my_rule', # Call without parameters
    'name2': { 'my_rule': arg1 }, # Call with one parameter.
    'name3': { 'my_rule': [arg1] }, # Call with one parameter.
    'name4': { 'my_rule': [ arg1, arg2, arg3 ] } # Call with many parameters.
}

Here is “max_number” implemenation:

class MaxNumber(object):
def __init__(self, *args):
    self._max_number = float(args[1])

def __call__(self, number, unuse, unuse_):
    # We do not validate empty fields. We have "required" rule for this purpose
    if number == None or number == '':
        return

    #return error message
    if float(number) > self._max_number:
        return 'TOO_HIGH'

Validator.Validator.register_default_rules({ "max_number": MaxNumber })

All rules for the validator are equal. It does not distinguish “required”, “list_of_different_objects” and “trim” rules. So, you can extend validator with any rules you like.

Validator.Validator.get_default_rules()

returns object containing all default rule_builders for the validator. You can register new rule or update existing one with “register_rules” method.

Validator.Validator.set_default_auto_trim(is_auto_trim)

Enables or disables automatic trim for input data. If is on then every new validator instance will have auto trim option enabled

OBJECT METHODS

validator.validate(input)

Validates user input. On success returns valid_data (contains only data that has described validation rules). On error return false.

valida_data = validator.validate(input)

if valida_data:
    #use valida_data
else:
    errors = validator.get_errors()

validator.get_errors()

Returns errors object.

{
     "field1": "ERROR_CODE",
     "field2": "ERROR_CODE",
     ...
 }

For example:

{
    "country":  "NOT_ALLOWED_VALUE",
    "zip":      "NOT_POSITIVE_INTEGER",
    "street":   "REQUIRED",
    "building": "NOT_POSITIVE_INTEGER"
}

validator.register_rules({“rule_name”: rule_builder})

rule_builder - is a function reference which will be called for building single rule validator.

See “Validator.Validator.register_default_rules” for rules examples.

validator.get_rules()

returns object containing all rule_builders for the validator. You can register new rule or update existing one with “register_rules” method.

validator.register_aliased_rule(alias)

alias - is a composite validation rule.

See “Validator.Validator.register_aliased_rule” for rules examples.

AUTHOR

koorchik (Viktor Turskyi), asholok (Ihor Kolosha)

BUGS

Please report any bugs or feature requests to Github https://github.com/asholok/python-validator-livr

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

LIVR-2.0.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

LIVR-2.0.1-py2.py3-none-any.whl (20.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file LIVR-2.0.1.tar.gz.

File metadata

  • Download URL: LIVR-2.0.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for LIVR-2.0.1.tar.gz
Algorithm Hash digest
SHA256 ef5f04955fae1064fc9df3a5bd37229ea9efc470b37c89237dec58bdaa37feaf
MD5 8c328a967d6425f23b1f10ef80fb16bf
BLAKE2b-256 cebfa1f2d38e4207688c1334834a876884d140e3257a4a16d2ddf540eded8afd

See more details on using hashes here.

File details

Details for the file LIVR-2.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for LIVR-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bf1b2f93585332702b427d4998dde0604c01673404685ef6dd4f2dc7a9c54868
MD5 c128baecb3382d0d76f08240d3fe6394
BLAKE2b-256 eec74d38dcffeb1422520d951aca1da4cb2775a791d314f8ca34ac8f8d4ad54c

See more details on using hashes here.

Supported by

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