Skip to main content

Laravel style input validation for Python.

Project description

Spotlight

Laravel style input validation for Python.

Table of Contents

Installation

Spotlight can be installed via pip:

pip install spotlight

Dependencies

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)

Nested validation:

rules = {
    "token": "required|string",
    "person": {
        "first_name": "required|string|max:255",
        "last_name": "required|string|max:255",
        "email": "required|email",
        "password": "required|min:8|max:255"
    }
}

input_ = {
    "token": "test-token",
    "person": {
        "first_name": "John",
        "last_name": "Doe",
        "email": "john.doe@example.com",
        "password": "test1234"
    }
}

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_list
  • valid_dict
  • valid_alpha_num
  • valid_alpha_num_space

Available Rules

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

required_without

The field under validation must be present and not empty when the other specified field is not present.

required_without:other

required_with

The field under validation must be present and not empty if the other specified field is present.

required_with:other

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

not_with

The field under validation can't be present if the other specified field is present.

not_with:other

filled

The field under validation must not be empty when it is present.

filled

email

The field under validation must be a valid email address.

email

url

The field under validation must be a valid URL.

url

ip

The field under validation must be an IP address.

url

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

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

in

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

in:value,other,...

alpha_num

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

alpha_num

alpha_num_space

The field under validation may have alpha-numeric characters, as well as spaces.

alpha_num_space

string

The field under validation must be a string.

string

integer

The field under validation must be an integer.

string

boolean

The field under validation must be a boolean.

boolean

list

The field under validation must be a list.

list

dict

The field under validation must be a dict.

dict

json

The field under validation must be a valid JSON string.

json

uuid4

The field under validation must be a valid uuid (version 4).

uuid4

accepted

The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.

accepted

starts_with

The field under validation must start with one of the given values.

starts_with:value,other,...

Advanced Usage

Overwriting Messages

Docs coming soon...

Custom Rules

Docs coming soon...

Plugins

Spotlight SQLAlchemy

To use database rules such as unique and exists checkout the Spotlight SQLAlchemy plugin.

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

spotlight-0.2.3.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

spotlight-0.2.3-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file spotlight-0.2.3.tar.gz.

File metadata

  • Download URL: spotlight-0.2.3.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for spotlight-0.2.3.tar.gz
Algorithm Hash digest
SHA256 755b93e658f722c21ebf96f730bfaa9b28918a34391ea66b921535f158ef51d1
MD5 c3fef71bf208f6f6499e17a8e8b0a5aa
BLAKE2b-256 253d4c5e84967a335046667254a2c935ccd161a802fae21bc46e1d545673ee1e

See more details on using hashes here.

File details

Details for the file spotlight-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: spotlight-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 29.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

File hashes

Hashes for spotlight-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 37e81ad50dd6060509f47a793f3d5c4e9c01b8495bd996eaa9b4a0060445d198
MD5 12d62ce7e49c12288d03931f04f7a22a
BLAKE2b-256 2a406118f54a3b361877c3114d9060631fcc3b45871c1976df6d0763cd8b4d81

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