A simple validation library
Project description
RATIFY
website: ratify
documentation: documentation
Congratulations on choosing Ratify! Ratify is a Python library that allows you to validate data against a schema. Whether you are a pro or a beginner, Ratify is designed to be simple and easy to use. This guide will walk you through the installation process and help you get started with Ratify..
make
The make method is used to validate data against a schema. The make method takes two arguments, data and rule. The data argument is the data that you want to validate, and the rule argument is the schema that you want to validate the data against.
The make method returns a dictionary with the validated data. If the data is invalid, the make method raises a ValidationError exception.
from ratify.ratify import Ratify
validator = Ratify()
try:
data = {
"name": "John Doe",
"email": "john@ratify.com",
"age": 25
}
rule = {
"name": ['required'],
"email": ['required', 'email'],
"age": ['required', 'integer', 'max:30']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
alpha
alphaThe alpha rule is used to check if a field contains only alphabetic characters. If the field contains any non-alphabetic characters, the alpha rule raises a ValidationError exception.
The alpha rule does not take any arguments.
validator = Ratify()
try:
data = {
"name": "John Doe",
}
rule = {
"name": ['alpha'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
alpha_num
alpha_numThe alpha_num rule is used to check if a field contains only alphabetic and numeric characters. If the field contains any non-alphabetic or non-numeric characters, the alpha_num rule raises a ValidationError exception.
The alpha_num rule does not take any arguments.
validator = Ratify()
try:
data = {
"username": "JohnDoe657",
}
rule = {
"username": ['alpha_num'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
after
after:dateThe after rule is used to check if a date is after a specified date. If the date is not after the specified date, the after rule raises a ValidationError exception.
The after rule takes one argument, date, which is the date that the field should be after. The date argument should be in the format YYYY-MM-DD.
validator = Ratify()
try:
data = {
"start": "2024-10-20",
}
rule = {
"start": ['after:2024-11-01'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
after_or_equal
after_or_equal:dateThe after_or_equal rule is used to check if a date is after or equal to a specified date. If the date is not after or equal to the specified date, the after_or_equal rule raises a ValidationError exception.
The after_or_equal rule takes one argument, date, which is the date that the field should be after or equal to. The date argument should be in the format YYYY-MM-DD.
validator = Ratify()
try:
data = {
"start_date": "2024-10-20",
}
rule = {
"start_date": ['after_or_equal:2024-11-01'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
before
before:dateThe before rule is used to check if a date is before a specified date. If the date is not before the specified date, the before rule raises a ValidationError exception.
The before rule takes one argument, date, which is the date that the field should be before. The date argument should be in the format YYYY-MM-DD.
validator = Ratify()
try:
data = {
"deadline": "2024-10-20",
}
rule = {
"deadline": ['after:2024-11-01'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
before_or_equal
before_or_equal:dateThe before_or_equal rule is used to check if a date is before or equal to a specified date. If the date is not before or equal to the specified date, the before_or_equal rule raises a ValidationError exception.
The before_or_equal rule takes one argument, date, which is the date that the field should be before or equal to. The date argument should be in the format YYYY-MM-DD.
validator = Ratify()
try:
data = {
"deadline": "2024-10-20",
}
rule = {
"deadline": ['before_or_equal:2024-11-01'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
boolean
booleanThe boolean rule is used to check if a field is a boolean value. If the field is not a boolean value, the boolean rule raises a ValidationError exception.
The boolean rule does not take any arguments.
validator = Ratify()
try:
data = {
"is_available": True
}
rule = {
"is_available": ['boolean'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
confirm_password
confirm_password:password_fieldThe confirm_password rule is used to check if a field matches another field. If the field does not match the other field, the confirm_password rule raises a ValidationError exception.
The confirm_password rule takes one argument, password_field, which is the field that the field should match.
validator = Ratify()
try:
data = {
"enter_password": "1234Password ",
"confirm_password": "1234Password "
}
rule = {
"confirm_password": ['confirm_password:enter_password']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
contains
contains:fooThe contains rule is used to check if a value exists in a list. If the list does not contain the specified value, the contains rule raises a ValidationError exception.
The contains rule takes one argument, value, which is the value that the list should contain.
validator = Ratify()
try:
data = {
"ages": [1, 2, 3, 4, 5]
}
rule = {
"ages": ['contains:3']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
date
dateThe date rule is used to check if a field is a valid date. If the field is not a valid date, the date rule raises a ValidationError exception.
The date rule does not take any arguments.
validator = Ratify()
try:
data = {
"start_date": "2024-10-20"
}
rule = {
"start_date": ['date']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
date_format
date_format:formatThe date_format rule is used to check if a field is a valid date with a specified format. If the field is not a valid date with the specified format, the date_format rule raises a ValidationError exception.
The date_format rule takes one argument, format, which is the format that the date should be in. The format argument should be a valid date format string.
validator = Ratify()
try:
data = {
"start_date": "10-20-2024"
}
rule = {
"start_date": ['date_format:%m-%d-%Y']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
datetime
datetimeThe datetime rule is used to check if a field is a valid datetime. If the field is not a valid datetime, the datetime rule raises a ValidationError exception.
The datetime rule does not take any arguments.
validator = Ratify()
try:
data = {
"start_date": "2024-10-20 12:30:05"
}
rule = {
"start_date": ['datetime']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
dict
dictThe dict rule is used to check if a field is a valid dictionary. If the field is not a valid dictionary, the dict rule raises a ValidationError exception.
The dict rule takes no argument
validator = Ratify()
try:
data = {
"inventory": {"name": "John Doe", "age": 23, "country": "Nigeria"}
}
rule = {
"inventory": ['dict']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
The email rule is used to check if a field is a valid email address. If the field is not a valid email address, the email rule raises a ValidationError exception.
The email rule does not take any arguments.
validator = Ratify()
try:
data = {
"email_address": "john@ratify.com",
}
rule = {
"email_address": ['email'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
ends_with
ends_with:fooThe ends_with rule is used to check if a field ends with a specified value. If the field does not end with the specified value, the ends_with rule raises a ValidationError exception.
The ends_with rule takes one argument, foo, which is the value that the field should end with.
validator = Ratify()
try:
data = {
"name": "Jones",
}
rule = {
"name": ['ends_with:es'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
file
file:extensionsThe file rule checks if the file is a valid file and if the file has the specified extensions. If the file is not valid or does not have the specified extensions, the file rule raises a ValidationError exception.
The file rule takes one argument, extensions, which is a comma-separated list of file extensions that the file should have.
note: The file rule only works with the Flask file object
validator = Ratify()
try:
data = {
"cv": "document.pdf",
}
rule = {
"cv": ['file:pdf,txt,doc'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
float
floatThe float rule is used to check if a field is a valid float. If the field is not a valid float, the float rule raises a ValidationError exception.
The float rule does not take any arguments.
validator = Ratify()
try:
data = {
"price": 24.32,
}
rule = {
"price": ['float']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
greater_than
greater_than:another_fieldThe greater_than rule is used to check if a field is greater than another field. If the field is not greater than the specified field, the greater_than rule raises a ValidationError exception.
The greater_than rule takes one argument, another_field, which is the referenced field.
validator = Ratify()
try:
data = {
"age_limit": 20,
"age": 24,
}
rule = {
"age": ['greater_than:age_limit'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
greater_than_or_equal
greater_than_or_equal:another_fieldThe greater_than_or_equal rule is used to check if a field is greater than or equal to another field. If the field is not greater than or equal to the specified field, the greater_than_or_equal rule raises a ValidationError exception.
The greater_than_or_equal rule takes one argument, another_field, which is the referenced field.
validator = Ratify()
try:
data = {
"age_limit": 20,
"age": 20,
}
rule = {
"age": ['greater_than:age_limit'],
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
in
in:foo,bar...The in rule is used to check if a field is in a list of comma separated values. If the field is not in the list of values, the in rule raises a ValidationError exception.
The in rule takes one argument, "foo, bar", which is a comma-separated list of values that the field should be in.
validator = Ratify()
try:
data = {
"age": 20
}
rule = {
"age": ['in:20,21,23,25']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
integer
integerThe integer rule checks if a field contains a valid integer
The integer rule takes no argument
validator = Ratify()
try:
data = {
"age": 20
}
rule = {
"age": ['integer']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
less_than
less_than:another_fieldThe less_than rule checks if the field under validation is less than the value in another field
The less_than rule takes one argument, another_field, which is the referenced field.
validator = Ratify()
try:
data = {
"wallet_balance": 20000,
"price": 5000
}
rule = {
"price": ['less_than:wallet_balance']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
less_than_or_equal
less_than_or_equal:another_fieldThe less_than_or_equal rule checks if the field under validation is less than or equals a value in another field
The less_than_or_equal rule takes one argument, another_field, which is the referenced field.
validator = Ratify()
try:
data = {
"wallet_balance": 20000,
"price": 5000
}
rule = {
"price": ['less_than_or_equal:wallet_balance']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
list
listThe list rule verifies if a value is a valid list
The list rule takes no argument.
validator = Ratify()
try:
data = {
"students": ['Joy', 'Joel', 'Kenn', 'Kay']
}
rule = {
"students": ['list']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
lowercase
lowercaseThe lowercase rule checks if all the characters in a value are all lowercases.
The lowercase rule does not take any argument
validator = Ratify()
try:
data = {
"model": "tedds"
}
rule = {
"model": ['lowercase']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
max
max:maximum_valueThe max rule is used to check if a field's value is less than or equal to a specified minimum value. If the field's value is less than the specified minimum value, the min rule raises a ValidationError exception.
The max rule takes one argument, maximum_value, which is the maximum value that the field should have.
validator = Ratify()
try:
data = {
"password": "1234Point"
}
rule = {
"password": ['max:12']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
mimes
mimes:foo,bar....The mimes rule is used to check if a file has a specified MIME type. If the file does not have the specified MIME type, the mimes rule raises a ValidationError exception.
The mimes rule takes comma seperated arguments, which are the MIME types that the file should have.
validator = Ratify()
try:
data = {
"photo": "selfie.jpg"
}
rule = {
"photo": ['mimes:jpg,png']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
min
min:minimum_valueThe min rule is used to check if a field's value is greater than or equal to a specified minimum value. If the field's value is less than the specified minimum value, the min rule raises a ValidationError exception.
The min rule takes one argument, minimum_value, which is the minimum value that the field should have.
validator = Ratify()
try:
data = {
"password": "1234Point"
}
rule = {
"password": ['min:6']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
not_in
not_in:foo,bar...The not_in rule is used to check if a field is not in a list of comma-separated values. If the field is in the list of values, the not_in rule raises a ValidationError exception.
The not_in rule takes one argument, "foo, bar", which is a comma-separated list of values that the field should not be in.
validator = Ratify()
try:
data = {
"weekday": "monday"
}
rule = {
"weekday": ['not_in:sunday,saturday']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
required
requiredThe required rule is used to check if a field is present in the data. If the field is not present, the required rule raises a ValidationError exception.
The required rule does not take any arguments.
validator = Ratify()
try:
data = {
"name": "John Doe",
"email": "john@ratify.com",
"age": 25
}
rule = {
"name": ['required'],
"email": ['required'],
"age": ['required']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
required_if
required_if:another_field,foo..The required_if rule makes a field required when a specified field contains a specified value
The required_if rule takes a comma seperated argument which starts with the referenced field as the first value, then the specified values
validator = Ratify()
try:
data = {
"name": "John Doe",
"country": "Canada",
"zip_code": "100 WYM",
"email": "john@ratify.com",
"age": 25
}
rule = {
"zip_code": ['required_if:country,Canada,USA']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
same
same:another_fieldThe same rule is used to check if a field matches another field. If the field does not match the other field, the same rule raises a ValidationError exception.
The same rule takes one argument, another_field, which is the field that the field should match.
validator = Ratify()
try:
data = {
"email": "john@ratify.com",
"confirm_email": "john@ratify.com"
}
rule = {
"confirm_email: ['same:email']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
size
size:valueThe size rule is used to check if a field's value is equal to a specified size. If the field's value is not equal to the specified size, the size rule raises a ValidationError exception.
This validates integers, strings and lists. when used on integers, the field under validation must be equal to the specified value, for strings it validates the string's length, for lists it validates the list length.
The size rule takes one argument, value, which is the specified value
validator = Ratify()
try:
data = {
"age": 20,
"key": "Everyday",
"names": ["Rahman", "Kola", "Chike"]
}
rule = {
"age": ['size:20'],
"key": ['size:8'],
"names": ['size:3']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
starts_with
starts_with:valueThe starts_with rule checks if a field starts with a specified value. If the field does not start with the specified value, the starts_with rule raises a ValidationError exception.
The starts_with rule takes one argument, value, which is the value that the field should start with.
validator = Ratify()
try:
data = {
"name": "Everyday"
}
rule = {
"name": ['starts_with:Ev']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
string
stringThe string rule checks if a field is a valid string. If the field is not a valid string, the string rule raises a ValidationError exception.
The string rule takes no argument
validator = Ratify()
try:
data = {
"name": "Everyday19"
}
rule = {
"name": ['string']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
uppercase
uppercaseThe uppercase rule checks if all the characters in a value are all uppercases.
The uppercase rule does not take any argument
validator = Ratify()
try:
data = {
"model": "TEDDS"
}
rule = {
"model": ['uppercase']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
url
urlThe url rule checks if a field is a valid URL. If the field is not a valid URL, the url rule raises a ValidationError exception.
The url rule does not take any argument
validator = Ratify()
try:
data = {
"website": "https://ted.com"
}
rule = {
"webiste": ['url']
}
validated = validator.make(data, rule)
except ValidationError as e:
print(e)
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
Built Distribution
File details
Details for the file ratify-0.4.0.tar.gz
.
File metadata
- Download URL: ratify-0.4.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b53cedb2b28591b5d9a4ad3f1c4210a8777255bff99e89e43deb5459beb073d9 |
|
MD5 | 933becc5abf466b1898dd3c586880a31 |
|
BLAKE2b-256 | 8e612dfb6f165de2bb0915f00ec9fb600b75e50f11eaebea507d22306d813437 |
File details
Details for the file ratify-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: ratify-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f808b20b5a08c23f1173cdace540a514089b0e55b5c94c25161c5219f77ef7d |
|
MD5 | e4fcc54979e3c0e19f4e32dbdfa3530a |
|
BLAKE2b-256 | 421079228ac454f887274b154a9505f6882fa3fd1282e5c9888243b5eaa5ca07 |