Skip to main content

Library for Help Validation Control

Project description

Safe Shield

Safe Shield is a robust Python library designed to validating HTTP requests and input data.

This project is licensed under the AGPLv3 License - see the LICENSE file for details

Table of Contents

Installation

Use the package manager pip to isntall Safe Shield.

pip install safeshield

Usage

User should pass request dictionary and rules dictionary for validation data in the request.

Please see examples below:

from validator import Validator

request = {
  "name": "Wunsun",
  "email": "wunsun58@gmail.com",
  "age": 20,
}

rules = {
  "name": "required",
  "email": "required|email",
  "age": "required|integer|min:18",
}

validated_data = Validator(request, rules).validate() # Validated data returned

Validator().validate() returns either validated data or False

Validation Data

Rule Format

You can define multiple rules for each field as a string, tuple, or list.

  • String Format
rules = {
  "name": "required",
  "email": "required|email",
  "age": "required|integer|min:18",
}
  • List Format
rules = {
  "name": ["required"],
  "email": ["required", "email"],
  "age": ["required","integer", "min:18"],
}
  • Tuple Format
rules = {
  "name": ("required"),
  "email": ("required", "email"),
  "age": ("required","integer", "min:18"),
}

All rules can be invoked either as a class instantiation or via method calls from the Rule class (e.g., Required()) or methods (e.g., Rule.required()).

from validator.rules import Rule, Required, Email

rules = {
  "name": Required(),  # Single rule (no brackets needed)
  "email": [Required(), Email()],  # Initialization Rule Class
  "age": [Rule.required(),Rule.integer(), Rule.min(18)],  # Call rule from Rule Class
}

Nested Rules:

Nested data validation, use dot notation like 'user.name' to access deep fields or wildcards like 'orders.*.id' to validate all array items. The system automatically checks all nested levels and returns clear error messages pointing to exact validation failures."

rules = {
    "user.name": Required(),  # Top-level field
    "user.contact.email": [Required(), Email()],  # Nested field
    "orders.*.id": Required(),  # Wildcard for list items
}

Error Messages

This validator enables users to track validation failures and receive corresponding error messages.

Basic Error Validation

This demonstrates fundamental validation failures for required fields and data types:

from validator import Validator

request = {
  "name": "",  # Empty value
  "email": "wunsun58@gmail.com",
]

rules = {
  "name": "required|string"
}

validator = Validator(request, rules)
validator.validate()

"""
validator.has_errors -> True
validator.errors:
  {
    "name": ["The name field is required.", "The name must be a string"]
  }
"""
  • Output Explanation:
    • validator.has_errors → True (Indicates validation failed)
    • validator.errors → Returns detailed error messages:
      • For the name field:
        • "The name field is required." - Because the value is empty
        • "The name must be a string." - Empty string fails type validation

Nested Error Validation

Validates complex nested structures (objects/arrays) with precise error location:

from validator import Validator

data = {
    "user": {
        "name": "Alice",
        "contact": {"email": "alice@example.com"} # Invalid dns email format
    },
    "orders": [
        {"id": 1, "price": "One hundred"}, # Invalid price type
        {"id": 2, "price": 200}  # Valid entry
    ]
}

rules = {
  "user.name": "required",
  "user.contact.email": "email:dns",  # Requires valid DNS email records
  "orders.*.price": "required|integer", # Wildcard validates all array item
}

validator = Validator(request, rules)
validator.validate()

"""
validator.has_errors -> True
validator.errors:
  {
    "user.contact.email": ["The email must be a valid email with valid DNS records."],
    "orders.0.price": ["The price must be a number"]
  }
"""
  • Output Explanation:
    • validator.has_errors → True
    • validator.errors → Pinpoints exact failures:
      • user.contact.email: Fails DNS validation
      • orders.0.price: First array item fails integer validation
      • Note how array indices (0, 1, etc.) are automatically identified

Custom Error Message

Override default messages with user-friendly alternatives:

messages = {
  "user.contact.email": "This email is invalid"
}

validator = Validator(request, rules, messages)
validator.validate()

"""
validator.has_errors -> True
validator.errors:
  {
    "user.contact.email": ["This email is invalid"],
    "orders.0.price": ["The price must be a number"]
  }
"""

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

safeshield-1.6.9.tar.gz (39.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeshield-1.6.9-py3-none-any.whl (48.1 kB view details)

Uploaded Python 3

File details

Details for the file safeshield-1.6.9.tar.gz.

File metadata

  • Download URL: safeshield-1.6.9.tar.gz
  • Upload date:
  • Size: 39.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.6

File hashes

Hashes for safeshield-1.6.9.tar.gz
Algorithm Hash digest
SHA256 5428d4f8a294a1f3cc7e3fa87804b0f63b9a8b78c71fe9acb0b83563126e6896
MD5 ac2c485f54e0aff89f9fabff9a441c19
BLAKE2b-256 cb5a929fddbc65224ca7834851e15a8d061648c25ed41d4221f3060a4ea07315

See more details on using hashes here.

File details

Details for the file safeshield-1.6.9-py3-none-any.whl.

File metadata

  • Download URL: safeshield-1.6.9-py3-none-any.whl
  • Upload date:
  • Size: 48.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.6

File hashes

Hashes for safeshield-1.6.9-py3-none-any.whl
Algorithm Hash digest
SHA256 960af9d174df3175a8c65bfbc91ffbc9277bf0bfa368934cfb2b7fd62fd697d3
MD5 ed6dcde10b470b05891f6e4653c87710
BLAKE2b-256 54429c5f58dcb04392b9ea3b121133ec469f43f0d09431c6603c98269e4a6608

See more details on using hashes here.

Supported by

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