Laravel style input validation for Python.
Project description
Spotlight
Laravel style input validation for Python.
Table of Contents
Dependencies
- python >= 3.6.0
- SQLAlchemy >= 1.3.1 (optional)
- Only needed if you want to use the database dependent rules: unique and exists.
Installation
Spotlight can be installed via pip:
pip install spotlight
Usage
from spotlight.validator import Validator
Simple Input Examples
rules = {
"email": "required|email",
"first_name": "required|string|max:255",
"last_name": "required|string|max:255",
"password": "required|min:8|max:255"
}
input = {
"email": "john.doe@example.com",
"first_name": "John",
"last_name": "Doe",
"password": "test1234"
}
validator = Validator()
errors = validator.validate(input, rules)
Database Rule Examples
rules = {
"id": "exists:user,id",
"email": "unique:user,email"
}
input = {
"id": 1,
"email": "john.doe@example.com"
}
validator = Validator()
errors = validator.validate(input, rules)
Direct Validation
Sometimes there is a need for quick and simple validation, without having to create a rule set. The Validator class exposes several static methods that can be used for direct validation.
For example:
email = "john.doe@example.com"
if validator.valid_email(email):
print("This is a valid email!")
# Or like this:
if Validator.valid_email(email):
print("This is a valid email!")
Available methods:
- valid_email
- valid_url
- valid_ip
- valid_uuid4
- valid_string
- valid_integer
- valid_boolean
- valid_json
- valid_alpha_num
- valid_alpha_num_space
Available Rules
- required
- required_without
- required_with
- required_if
- not_with
- filled
- url
- ip
- min
- max
- in
- alpha_num
- alpha_num_space
- string
- integer
- boolean
- json
- uuid4
- unique (database)
- exists (database)
required
The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:
- The value is None.
- The value is an empty string.
- The value is an empty list.
required
Default message:
The {field} field is required.
required_without
The field under validation must be present and not empty when the other specified field is not present.
required_without:other
Default message:
The {field} field is required if the {other} field is absent.
required_with
The field under validation must be present and not empty if the other specified field is present.
required_with:other
Default message:
The {field} field is required if the {other} field is present.
required_if
The field under validation must be present and not empty if the other specified field equals a certain value.
required_if:other,value
Default message:
The {field} field is required if the {other} field equals {value}.
not_with
The field under validation can't be present if the other specified field is present.
not_with:other
Default message:
The {field} field can't be present when the {other} field is present.
filled
The field under validation must not be empty when it is present.
filled
Default message:
The {field} field must not be empty when it is present.
The field under validation must be a valid email address.
email
Default message:
Invalid email address.
url
The field under validation must be a valid URL.
url
Default message:
Invalid URL.
ip
The field under validation must be an IP address.
url
Default message:
Invalid IP address.
min
The field under validation must be greater than or equal to the given minimum value. For strings, value corresponds to the number of characters. For integers, value corresponds to a given integer value. For a list, value corresponds to the length of the list.
min:value
Default message:
The {field} field has to be at least {min} characters.
max
The field under validation must be less than or equal to the given maximum value. For strings, value corresponds to the number of characters. For integers, value corresponds to a given integer value. For a list, value corresponds to the length of the list.
max:value
Default message:
The {field} field cannot be longer than {max} characters.
unique (database)
The field under validation must be unique in a given database table. The last 4 fields (ignore column, ignore value, where column, where value) are optional.
unique:table,column
unique:table,column,ignoreColumn,ignoreValue
unique:table,column,ignoreColumn,ignoreValue,whereColumn,whereValue
unique:table,column,null,null,whereColumn,whereValue
exists (database)
The field under validation must exist on a given database table. The last 2 fields (where column, where value) are optional.
exists:table,column
exists:table,column,whereColumn,whereValue
Advanced Usage
Overwriting Messages
Docs coming soon...
Custom Rules
Docs coming soon...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for spotlight-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 139867561d457887cdc5ab91aba4845b80b5aaa9fbbc891f6f97283d2f08a9fd |
|
MD5 | a026f854dff6ac90b1b54d8c232701be |
|
BLAKE2b-256 | cf8d4e01fe428e4dc4957c6a70bce4773789286cdc69a8f71da0680318b98faf |