Skip to main content

Python data validation library with type hints and schema support

Project description

Famo Data Validator

Famo Data Validator is a Python library for validating dictionaries and lists of dictionaries using a schema-based approach. It supports default values, required fields, custom validation, and schema generation.


Features

  • Validate single objects or multiple objects (many=True)
  • Built-in field types: CharField, IntegerField, EmailField, ListField
  • Required fields and default values
  • Custom validation support
  • Generate schema dynamically for documentation
  • Fully typed and IDE-friendly

Installation

pip install famo-data-validator

Or install the latest version from GitHub:

pip install git+https://github.com/famo-codes/data-validator.git

Examples

1. Single Object Validation

from data_validator.serializer import Serializer
from data_validator.fields import CharField, IntegerField, EmailField, ListField

class UserSerializer(Serializer):
    name = CharField(min_length=3, max_length=50, required=True)
    age = IntegerField(min_value=0, max_value=120, default=18)
    email = EmailField(required=True)
    tags = ListField(item_field=CharField(), min_length=1, default=[])
    status = CharField(choices=["active", "inactive"], default="active")

user_data = {"name": "Alice", "email": "alice@example.com", "tags": ["python"]}
serializer = UserSerializer(data=user_data)
serializer.is_valid(raise_exception=True)
print(serializer.validated_data)

Output:

{
    "name": "Alice",
    "age": 18,
    "email": "alice@example.com",
    "tags": ["python"],
    "status": "active"
}

2. Multiple Objects Validation

users = [
    {"name": "Alice", "email": "alice@example.com", "tags": ["python"]},
    {"name": "Bob", "email": "bob@example.com", "tags": ["backend"]}
]

serializer_many = UserSerializer(data=users, many=True)
serializer_many.is_valid(raise_exception=True)
print(serializer_many.validated_data)

Output:

[
    {"name": "Alice", "age": 18, "email": "alice@example.com", "tags": ["python"], "status": "active"},
    {"name": "Bob", "age": 18, "email": "bob@example.com", "tags": ["backend"], "status": "active"}
]

3. Generate Schema for Documentation

import json

schema = UserSerializer().get_schema()
print(json.dumps(schema, indent=4))

Sample Output:

{
    "name": {"type": "CharField", "required": True, "default": None, "min_length": 3, "max_length": 50},
    "age": {"type": "IntegerField", "required": True, "default": 18, "min_value": 0, "max_value": 120},
    "email": {"type": "EmailField", "required": True, "default": None},
    "tags": {"type": "ListField", "required": True, "default": [], "item_field": "<CharField instance>", "min_length": 1},
    "status": {"type": "CharField", "required": True, "default": "active", "choices": ["active", "inactive"]}
}

4. Custom Validator Example

from data_validator.fields import IntegerField, ValidationError

class PositiveIntegerField(IntegerField):
    def validate(self, value, field_name=None):
        value = super().validate(value, field_name)
        if value <= 0:
            raise ValidationError({field_name: "Value must be positive"})
        return value

class ProductSerializer(Serializer):
    price = PositiveIntegerField(required=True)

product_data = {"price": 50}
serializer = ProductSerializer(data=product_data)
serializer.is_valid(raise_exception=True)
print(serializer.validated_data)

Output:

{"price": 50}

5. Handling Validation Errors

invalid_data = {"name": "A", "email": "invalid_email"}
serializer = UserSerializer(data=invalid_data)

if not serializer.is_valid():
    print(serializer.errors)

Sample Output:

{
    "name": "Minimum length is 3",
    "email": "Invalid email address"
}

License

MIT License

Copyright (c) 2025 Famo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

famo_data_validator-0.1.6.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

famo_data_validator-0.1.6-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file famo_data_validator-0.1.6.tar.gz.

File metadata

  • Download URL: famo_data_validator-0.1.6.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for famo_data_validator-0.1.6.tar.gz
Algorithm Hash digest
SHA256 ca74870b254baeb291d9afc515a355d7f64ff4dd2a8706af45df6a3f00fb5457
MD5 899e8d7d6518318ce5f040fa8fbb8b53
BLAKE2b-256 6b95da587b14caa026390e0bc91eff810a7f5bf8c13fd11990cdcb5a24eff2f3

See more details on using hashes here.

File details

Details for the file famo_data_validator-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for famo_data_validator-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c4fc20224679722afaf99adac605655e9d6a2eea7b8dc352b97a37e0ac657593
MD5 0368bcc62897b1f91852e9ce34586bc3
BLAKE2b-256 c6c07a92a09bed4c731840b3ecc1998eeca412b8b8574a79808374559251f627

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