Skip to main content

Laravel like data validation library for python language

Project description

LaraValidation

Release Travis CI Build Status Coverage Report

Original forked repo: https://github.com/walid-mashal/Laravel-Validation

LaraValidation is a python module containing logic for implementing Laravel style Data Validation using python language. This code can be used with html forms to validate their input fields and can also be used with any kind of other data as long as the data is in the form of a python dictionary.

For Example we can have some data in the form of a python dictionary as the following:

data = { 
    'name':'2222222',   
    'phone':'2',  
    'birthday':'1991/03/04',    
    'email':'info@example.com',    
    'host':'172.30.30.231',  
    'website':'https://www.example.com', 
    'nationality':'afghan',
    'active':'1',  
}

that needs to be validated.

So we can write Rules to validate the data in the following way

rules = { 
    'name':'required|size:20',   
    'phone':'phone', 
    'birthday':'date|date_format:%Y/%m/%d', 
    'email':'required|email',
    'host':'ip', 
    'website':'website', 
    'nationality':'in:afghan,pakistani,irani',
    'active':'boolean'
}  

Description:

The name field is a required field and only allows a maximum value of 20 characters.

The Phone field is a telephone number field and a valid phone number should be entered for this field.

The birthday field is a date field and the value entered should be a valid date, the date_format allows you to specify a date format that the field should be filled with.

The email is also required and a valid email should be entered into that field

Host must be a valid ip address e.g. 196.3.3.8

Website must be a valid website address e.g. http://www.google.com

Nationality can be one of the three provided strings: afghan,pakistani,irani

Active must be one of the following: 1,0,"1","0","false","true",False,True

Now we can write the following code to validate the data:

from validation import Validation

validator = Validation()
errors = validator.validate(data,rules)

Now the errors contain a list of error messages that is the result of the validation.

validate() also takes an optional custom_messages argument which will set user messages to be used for validation, or instead of passing the custom_messages argument you can actually set the messages of your own choice using the cutom_error_messages.json file.

We can call the is_valid() method to validate the data and will return either True or False

from validation import Validation

validator = Validation()
isvalid = validator.is_valid(data,rules)

errors = validator.errors

After validate() or is_valid() there will be an instance variable with the name errors set on the object that holds the list of validation error messages.

Validation Rules

The following is a list of the validation rules available

after

The field under validation must be a value after a given date.

alpha

The field under validation must be entirely alphabetic characters.

alpha_num

The field under validation must be entirely alpha-numeric characters.

before:date

The field under validation must be a value preceding the given date.

between:min,max

The field under validation must have a size between the given min and max. Strings and numerics are evaluated in the same fashion as the size rule.

boolean

The field under validation must be able to be cast as a boolean. Accepted input values are true, false, 1, 0, "1" and "0".

confirmed

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

date

The field under validation must be a valid date, the default value for the format is %m/%d/%Y if no date_format rule is applied.

date_format:format

The field under validation must match the format defined in the format, the default format is %m/%d/%Y. This rule should be preceeded by the date rule.

different:field

The given field must be different than the field under validation.

digits:value

The field under validation must be numeric and must have an exact length of value.

email

The field under validation must be formatted as an e-mail address.

integer

The field under validation must have an integer value.

ip

The field under validation must be formatted as an IP address.

max:value

The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.

min:value

The field under validation must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.

not_in:foo,bar,...

The field under validation must not be included in the given list of values.

present

The field under validation must be present in the input data but can be empty.

regex:pattern

The field under validation must match the given regular expression.

required

The field under validation must be present in the input data.

same:field

The given field must match the field under validation.

size:value

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value.

website

The field under validation must have a valid website url as value. for example: https://www.example.com or www.example.com

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

laravalidation-0.4.1.tar.gz (9.2 kB view details)

Uploaded Source

File details

Details for the file laravalidation-0.4.1.tar.gz.

File metadata

  • Download URL: laravalidation-0.4.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for laravalidation-0.4.1.tar.gz
Algorithm Hash digest
SHA256 fe7f2a9f09e1002816b09a6989c2eb0562198e3cfe34395c2498b716b6257de8
MD5 63d05e15f511f48122c63d4cb61cdc05
BLAKE2b-256 9d4e15afe3e2c2e2ae2808b6bee759efbf345b349037ca71825467bf342cf86a

See more details on using hashes here.

Supported by

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