famo-data-validator


Namefamo-data-validator JSON
Version 0.1.6 PyPI version JSON
download
home_pagehttps://github.com/famo-codes/data_validator
SummaryPython data validation library with type hints and schema support
upload_time2025-10-17 19:31:52
maintainerNone
docs_urlNone
authorFamo
requires_python>=3.10
licenseNone
keywords data validation serializer schema python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install famo-data-validator
```

Or install the latest version from GitHub:

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

---

## Examples

### 1. Single Object Validation

```python
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:**

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

---

### 2. Multiple Objects Validation

```python
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:**

```python
[
    {"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

```python
import json

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

**Sample Output:**

```python
{
    "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

```python
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:**

```python
{"price": 50}
```

---

### 5. Handling Validation Errors

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

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

**Sample Output:**

```python
{
    "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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/famo-codes/data_validator",
    "name": "famo-data-validator",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "data validation serializer schema python",
    "author": "Famo",
    "author_email": "CoreByMostafa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6b/95/da587b14caa026390e0bc91eff810a7f5bf8c13fd11990cdcb5a24eff2f3/famo_data_validator-0.1.6.tar.gz",
    "platform": null,
    "description": "# Famo Data Validator\r\n\r\n**Famo Data Validator** is a Python library for validating dictionaries and lists of dictionaries using a schema-based approach.\r\nIt supports default values, required fields, custom validation, and schema generation.\r\n\r\n---\r\n\r\n## Features\r\n\r\n* Validate **single objects** or **multiple objects** (`many=True`)\r\n* Built-in field types: `CharField`, `IntegerField`, `EmailField`, `ListField`\r\n* Required fields and default values\r\n* Custom validation support\r\n* Generate schema dynamically for documentation\r\n* Fully typed and IDE-friendly\r\n\r\n---\r\n\r\n## Installation\r\n\r\n```bash\r\npip install famo-data-validator\r\n```\r\n\r\nOr install the latest version from GitHub:\r\n\r\n```bash\r\npip install git+https://github.com/famo-codes/data-validator.git\r\n```\r\n\r\n---\r\n\r\n## Examples\r\n\r\n### 1. Single Object Validation\r\n\r\n```python\r\nfrom data_validator.serializer import Serializer\r\nfrom data_validator.fields import CharField, IntegerField, EmailField, ListField\r\n\r\nclass UserSerializer(Serializer):\r\n    name = CharField(min_length=3, max_length=50, required=True)\r\n    age = IntegerField(min_value=0, max_value=120, default=18)\r\n    email = EmailField(required=True)\r\n    tags = ListField(item_field=CharField(), min_length=1, default=[])\r\n    status = CharField(choices=[\"active\", \"inactive\"], default=\"active\")\r\n\r\nuser_data = {\"name\": \"Alice\", \"email\": \"alice@example.com\", \"tags\": [\"python\"]}\r\nserializer = UserSerializer(data=user_data)\r\nserializer.is_valid(raise_exception=True)\r\nprint(serializer.validated_data)\r\n```\r\n\r\n**Output:**\r\n\r\n```python\r\n{\r\n    \"name\": \"Alice\",\r\n    \"age\": 18,\r\n    \"email\": \"alice@example.com\",\r\n    \"tags\": [\"python\"],\r\n    \"status\": \"active\"\r\n}\r\n```\r\n\r\n---\r\n\r\n### 2. Multiple Objects Validation\r\n\r\n```python\r\nusers = [\r\n    {\"name\": \"Alice\", \"email\": \"alice@example.com\", \"tags\": [\"python\"]},\r\n    {\"name\": \"Bob\", \"email\": \"bob@example.com\", \"tags\": [\"backend\"]}\r\n]\r\n\r\nserializer_many = UserSerializer(data=users, many=True)\r\nserializer_many.is_valid(raise_exception=True)\r\nprint(serializer_many.validated_data)\r\n```\r\n\r\n**Output:**\r\n\r\n```python\r\n[\r\n    {\"name\": \"Alice\", \"age\": 18, \"email\": \"alice@example.com\", \"tags\": [\"python\"], \"status\": \"active\"},\r\n    {\"name\": \"Bob\", \"age\": 18, \"email\": \"bob@example.com\", \"tags\": [\"backend\"], \"status\": \"active\"}\r\n]\r\n```\r\n\r\n---\r\n\r\n### 3. Generate Schema for Documentation\r\n\r\n```python\r\nimport json\r\n\r\nschema = UserSerializer().get_schema()\r\nprint(json.dumps(schema, indent=4))\r\n```\r\n\r\n**Sample Output:**\r\n\r\n```python\r\n{\r\n    \"name\": {\"type\": \"CharField\", \"required\": True, \"default\": None, \"min_length\": 3, \"max_length\": 50},\r\n    \"age\": {\"type\": \"IntegerField\", \"required\": True, \"default\": 18, \"min_value\": 0, \"max_value\": 120},\r\n    \"email\": {\"type\": \"EmailField\", \"required\": True, \"default\": None},\r\n    \"tags\": {\"type\": \"ListField\", \"required\": True, \"default\": [], \"item_field\": \"<CharField instance>\", \"min_length\": 1},\r\n    \"status\": {\"type\": \"CharField\", \"required\": True, \"default\": \"active\", \"choices\": [\"active\", \"inactive\"]}\r\n}\r\n```\r\n\r\n---\r\n\r\n### 4. Custom Validator Example\r\n\r\n```python\r\nfrom data_validator.fields import IntegerField, ValidationError\r\n\r\nclass PositiveIntegerField(IntegerField):\r\n    def validate(self, value, field_name=None):\r\n        value = super().validate(value, field_name)\r\n        if value <= 0:\r\n            raise ValidationError({field_name: \"Value must be positive\"})\r\n        return value\r\n\r\nclass ProductSerializer(Serializer):\r\n    price = PositiveIntegerField(required=True)\r\n\r\nproduct_data = {\"price\": 50}\r\nserializer = ProductSerializer(data=product_data)\r\nserializer.is_valid(raise_exception=True)\r\nprint(serializer.validated_data)\r\n```\r\n\r\n**Output:**\r\n\r\n```python\r\n{\"price\": 50}\r\n```\r\n\r\n---\r\n\r\n### 5. Handling Validation Errors\r\n\r\n```python\r\ninvalid_data = {\"name\": \"A\", \"email\": \"invalid_email\"}\r\nserializer = UserSerializer(data=invalid_data)\r\n\r\nif not serializer.is_valid():\r\n    print(serializer.errors)\r\n```\r\n\r\n**Sample Output:**\r\n\r\n```python\r\n{\r\n    \"name\": \"Minimum length is 3\",\r\n    \"email\": \"Invalid email address\"\r\n}\r\n```\r\n\r\n---\r\n\r\n## License\r\n\r\nMIT License\r\n\r\nCopyright (c) 2025 Famo\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\nSOFTWARE.\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python data validation library with type hints and schema support",
    "version": "0.1.6",
    "project_urls": {
        "Homepage": "https://github.com/famo-codes/data_validator"
    },
    "split_keywords": [
        "data",
        "validation",
        "serializer",
        "schema",
        "python"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c6c07a92a09bed4c731840b3ecc1998eeca412b8b8574a79808374559251f627",
                "md5": "0368bcc62897b1f91852e9ce34586bc3",
                "sha256": "c4fc20224679722afaf99adac605655e9d6a2eea7b8dc352b97a37e0ac657593"
            },
            "downloads": -1,
            "filename": "famo_data_validator-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0368bcc62897b1f91852e9ce34586bc3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 9930,
            "upload_time": "2025-10-17T19:31:51",
            "upload_time_iso_8601": "2025-10-17T19:31:51.752655Z",
            "url": "https://files.pythonhosted.org/packages/c6/c0/7a92a09bed4c731840b3ecc1998eeca412b8b8574a79808374559251f627/famo_data_validator-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "6b95da587b14caa026390e0bc91eff810a7f5bf8c13fd11990cdcb5a24eff2f3",
                "md5": "899e8d7d6518318ce5f040fa8fbb8b53",
                "sha256": "ca74870b254baeb291d9afc515a355d7f64ff4dd2a8706af45df6a3f00fb5457"
            },
            "downloads": -1,
            "filename": "famo_data_validator-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "899e8d7d6518318ce5f040fa8fbb8b53",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 9910,
            "upload_time": "2025-10-17T19:31:52",
            "upload_time_iso_8601": "2025-10-17T19:31:52.661664Z",
            "url": "https://files.pythonhosted.org/packages/6b/95/da587b14caa026390e0bc91eff810a7f5bf8c13fd11990cdcb5a24eff2f3/famo_data_validator-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-17 19:31:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "famo-codes",
    "github_project": "data_validator",
    "github_not_found": true,
    "lcname": "famo-data-validator"
}
        
Elapsed time: 2.30157s