Top Related Projects
CONTRIBUTIONS ONLY: Voluptuous, despite the name, is a Python data validation library.
Schema validation just got Pythonic
Python Data Structures for Humans™.
A lightweight library for converting complex objects to and from simple Python datatypes.
Lightweight, extensible data validation library for Python
Quick Overview
Validators is a Python library that provides a collection of string validation utilities. It offers a wide range of validation functions for common data types such as email addresses, URLs, IP addresses, and more. The library aims to simplify the process of data validation in Python applications.
Pros
- Comprehensive set of validation functions for various data types
- Easy to use with simple function calls
- Well-documented with clear examples
- Actively maintained and regularly updated
Cons
- Some validations may be too strict or not flexible enough for certain use cases
- Limited customization options for validation rules
- Dependency on third-party libraries for some validations
- May not cover all possible edge cases for complex data types
Code Examples
- Validating an email address:
import validators
email = "user@example.com"
is_valid = validators.email(email)
print(f"Is {email} valid? {is_valid}")
- Checking if a string is a valid URL:
import validators
url = "https://www.example.com"
is_valid = validators.url(url)
print(f"Is {url} a valid URL? {is_valid}")
- Validating an IPv4 address:
import validators
ip = "192.168.0.1"
is_valid = validators.ipv4(ip)
print(f"Is {ip} a valid IPv4 address? {is_valid}")
Getting Started
To get started with Validators, follow these steps:
-
Install the library using pip:
pip install validators
-
Import the validators module in your Python script:
import validators
-
Use the validation functions as needed:
email = "user@example.com" if validators.email(email): print("Valid email address") else: print("Invalid email address")
That's it! You can now use the various validation functions provided by the Validators library in your Python projects.
Competitor Comparisons
CONTRIBUTIONS ONLY: Voluptuous, despite the name, is a Python data validation library.
Pros of Voluptuous
- More flexible and powerful schema definition
- Supports complex data structures and nested validation
- Allows custom validation functions and error messages
Cons of Voluptuous
- Steeper learning curve due to more complex API
- Less intuitive for simple validation tasks
- Requires more code for basic validations
Code Comparison
Validators:
from validators import email
email("example@example.com") # Returns True
email("invalid_email") # Returns False
Voluptuous:
from voluptuous import Schema, Email
schema = Schema(Email())
schema("example@example.com") # Passes validation
schema("invalid_email") # Raises Invalid exception
Summary
Validators is simpler and more straightforward for basic validation tasks, making it easier to use for beginners or small projects. It provides a collection of ready-to-use validation functions for common data types.
Voluptuous offers more advanced features and flexibility, allowing for complex schema definitions and custom validation logic. It's better suited for larger projects or when dealing with nested data structures that require intricate validation rules.
The choice between the two depends on the project's complexity and specific validation requirements. Validators is ideal for quick, simple validations, while Voluptuous shines in scenarios requiring more sophisticated validation logic.
Schema validation just got Pythonic
Pros of Schema
- More flexible and powerful validation capabilities, allowing for complex data structures
- Supports custom validation functions and error messages
- Can be used for both data validation and object serialization
Cons of Schema
- Steeper learning curve due to its more complex API
- Less focused on specific validation types (e.g., email, URL) compared to Validators
- Requires more code to set up simple validations
Code Comparison
Schema:
from schema import Schema, And, Use, Optional
schema = Schema({
'name': And(str, len),
'age': And(Use(int), lambda n: 18 <= n <= 99),
Optional('email'): str
})
Validators:
import validators
name = "John Doe"
age = 25
email = "john@example.com"
validators.length(name, min=1)
validators.between(age, min=18, max=99)
validators.email(email)
Summary
Schema offers more flexibility and power for complex validations but requires more setup. Validators provides simpler, more focused validation for common data types with less code. Choose Schema for complex data structures and custom validations, and Validators for quick, straightforward validations of common types.
Python Data Structures for Humans™.
Pros of Schematics
- More comprehensive data modeling and validation framework
- Supports complex data structures and nested schemas
- Provides type coercion and data conversion capabilities
Cons of Schematics
- Steeper learning curve due to more complex API
- Heavier and potentially slower for simple validation tasks
- Less frequently updated compared to Validators
Code Comparison
Validators:
from validators import email
email("example@example.com") # Returns True
email("invalid_email") # Returns False
Schematics:
from schematics import Model, types
class User(Model):
email = types.EmailType(required=True)
user = User({"email": "example@example.com"})
user.validate() # Passes validation
Summary
Validators is a lightweight library focused on simple data validation, while Schematics offers a more robust solution for complex data modeling and validation. Validators is easier to use for basic tasks, but Schematics provides more flexibility and power for handling complex data structures and transformations. Choose based on your project's complexity and requirements.
A lightweight library for converting complex objects to and from simple Python datatypes.
Pros of marshmallow
- More comprehensive data serialization and deserialization capabilities
- Supports complex object schemas and nested structures
- Integrates well with various web frameworks and ORMs
Cons of marshmallow
- Steeper learning curve due to more complex API
- Potentially overkill for simple validation tasks
- Slower performance for basic validations compared to validators
Code comparison
marshmallow:
from marshmallow import Schema, fields
class UserSchema(Schema):
name = fields.Str(required=True)
email = fields.Email()
age = fields.Int(validate=lambda n: 18 <= n <= 99)
schema = UserSchema()
result = schema.load({"name": "John", "email": "john@example.com", "age": 30})
validators:
from validators import email, between
name = "John"
email_address = "john@example.com"
age = 30
is_valid = email(email_address) and between(age, min=18, max=99)
marshmallow offers a more structured approach to data validation and serialization, while validators provides simpler, function-based validations. marshmallow is better suited for complex data structures and API development, whereas validators is more lightweight and easier to use for basic validation tasks.
Lightweight, extensible data validation library for Python
Pros of Cerberus
- More comprehensive data validation framework, allowing complex schema definitions
- Supports custom validation rules and coercion of data types
- Provides detailed error messages and validation contexts
Cons of Cerberus
- Steeper learning curve due to its more complex API
- Heavier and potentially slower for simple validation tasks
- Requires more setup and configuration for basic use cases
Code Comparison
Validators:
from validators import email
email("example@example.com") # Returns True
email("invalid_email") # Returns False
Cerberus:
from cerberus import Validator
schema = {'email': {'type': 'string', 'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'}}
v = Validator(schema)
v.validate({'email': 'example@example.com'}) # Returns True
v.validate({'email': 'invalid_email'}) # Returns False
Validators offers a simpler, more straightforward approach for basic validation tasks, while Cerberus provides a more powerful and flexible framework for complex data validation scenarios. Validators is easier to use for quick, single-field validations, whereas Cerberus excels in validating entire documents or complex data structures with interdependent fields.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
validators - Python Data Validation for Humansâ¢
Python has all kinds of data validation tools, but every one of them seems to require defining a schema or form. I wanted to create a simple validation library where validating a simple value does not require defining a form or a schema.
pip install validators
Then,
>>> import validators
>>>
>>> validators.email('someone@example.com')
True
Resources
Python 3.8 reaches EOL in October 2024.
Top Related Projects
CONTRIBUTIONS ONLY: Voluptuous, despite the name, is a Python data validation library.
Schema validation just got Pythonic
Python Data Structures for Humans™.
A lightweight library for converting complex objects to and from simple Python datatypes.
Lightweight, extensible data validation library for Python
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot