All Projects → Attumm → Maat

Attumm / Maat

Licence: MIT license
Validation and transformation library powered by deductive ascending parser. Made to be extended for any kind of project.

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to Maat

Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+2529.63%)
Mutual labels:  validation, json-schema, validator
openui5-validator
A library to validate OpenUI5 fields
Stars: ✭ 17 (-37.04%)
Mutual labels:  validation, json-schema, validator
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (+359.26%)
Mutual labels:  serialization, validation, validator
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (+148.15%)
Mutual labels:  validation, json-schema, validator
Swagger Cli
Swagger 2.0 and OpenAPI 3.0 command-line tool
Stars: ✭ 321 (+1088.89%)
Mutual labels:  validation, json-schema, validator
Validr
A simple, fast, extensible python library for data validation.
Stars: ✭ 205 (+659.26%)
Mutual labels:  validation, json-schema, validator
Typical
Typical: Fast, simple, & correct data-validation using Python 3 typing.
Stars: ✭ 111 (+311.11%)
Mutual labels:  serialization, validation
Marshmallow Jsonapi
JSON API 1.0 (https://jsonapi.org/) formatting with marshmallow
Stars: ✭ 203 (+651.85%)
Mutual labels:  serialization, validation
Json Schema Validator
A fast Java JSON schema validator that supports draft V4, V6, V7 and V2019-09
Stars: ✭ 292 (+981.48%)
Mutual labels:  fast, json-schema
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (+2188.89%)
Mutual labels:  fast, serialization
Fast Xml Parser
Validate XML, Parse XML to JS/JSON and vise versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
Stars: ✭ 1,021 (+3681.48%)
Mutual labels:  fast, validator
Borer
Efficient CBOR and JSON (de)serialization in Scala
Stars: ✭ 131 (+385.19%)
Mutual labels:  fast, serialization
Fhir.js
Node.JS library for serializing/deserializing FHIR resources between JS/JSON and XML using various node.js XML libraries
Stars: ✭ 61 (+125.93%)
Mutual labels:  serialization, validation
Schematics
Project documentation: https://schematics.readthedocs.io/en/latest/
Stars: ✭ 2,461 (+9014.81%)
Mutual labels:  serialization, validation
Strictyaml
Type-safe YAML parser and validator.
Stars: ✭ 836 (+2996.3%)
Mutual labels:  serialization, validation
Marshmallow
A lightweight library for converting complex objects to and from simple Python datatypes.
Stars: ✭ 5,857 (+21592.59%)
Mutual labels:  serialization, validation
finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-33.33%)
Mutual labels:  json-schema, validator
another-json-schema
Another JSON Schema validator, simple & flexible & intuitive.
Stars: ✭ 48 (+77.78%)
Mutual labels:  json-schema, validator
node-input-validator
Validation library for node.js
Stars: ✭ 74 (+174.07%)
Mutual labels:  validation, validator
validation
Aplus Framework Validation Library
Stars: ✭ 99 (+266.67%)
Mutual labels:  validation, validator

Maat

Build Status Coverage Status Downloads

Maat is an easily extensible transformation and validation library for Python. Built for corner cases and speed.

The project is named after the ancient Egyptian god Maat. Her scale was used to weigh the heart as described in the book of the dead.

Since Maats scale is magical, it not only validates values, it can transform them too.

Maat does dictionary-to-dictionary validation and transformation. From those two dictionaries a new dictionary is created. Each value of the dictionary to be validated is passed through their validation and transformation functions.

Maat doesn't use other depenencies.

Examples

This validates that input name is of type str and is either "John Doe" or "Jane Doe". Throws Invalid exception when validation fails. Maat has a fail fast policy.

    >>> from maat import validate
    >>> user = {"name": "John Doe"}
    >>> user_validation = {"name": {"type": "str", "choices": ["John Doe", "Jane Doe"]}}
    >>> validate(user, user_validation)
    {"name": "John Doe"}
    
    >>> validate({"name": "peter pan"}, user_validation)
    Traceback (most recent call last):
    maat.validation.Invalid: key: "name" contains invalid item "peter pan": not in valid choices ["John Doe", "Jane Doe"]
    
    >>> validate({"name": 42}, user_validation)
    Traceback (most recent call last)
    maat.validation.Invalid: key: "name" contains invalid item "42" with type "int": not of type string
    
    >>>  validate({"user": "John Doe"}, user_validation)
    Traceback (most recent call last)
    maat.validation.Invalid: invalid keys: user :expected keys: name
    
    >>> validate({"name": "Jane Doe"}, user_validation)
    {"name": "Jane Doe"}

    >>> import maat
    >>> @maat.protected(user_validation)
        def create_user(name):
            return "success"

    >>> create_user("peter pan")
    Traceback (most recent call last):
    maat.maat.Invalid: key: "name" contains invalid item "peter pan": not in valid choices ["John Doe", "Jane Doe"]

    >>> create_user("John Doe")
    "success"

Starting Point Example

validation = {
    "int   ": {"type": "int", "cast": True, "min_amount": 1, "max_amount": 150},
    "float ": {"type": "float", "cast": True, "min_amount": 1, "max_amount": 150},
    "list  ": {"type": "list", "min_amount": 1, "max_amount": 5, "skip_failed": True},
    "dict  ": {"type": "dict", "min_amount": 1, "max_amount": 2, "key_regex": r"(\w+)"},
    "string": {"type": "str", "cast": True, "min_length": 1,
        "max_length": 12, "regex": r"(\w+ )(\w+)", "choices": ["John Doe", "Jane Doe"]
    }
}

Field Control

Each field could be nullable, optional, default; they can be added to any field. For lists it's possible to skip failed items with skip_failed.

>>> input_dic = {"str   ": None}
>>> validation = {
	"int   ": {"type": "int", "min_amount": 1, "default": 42},
	"float ": {"type": "float", "optional": True},
	"str   ": {"type": "str", "nullable": True},
}
>>> validate(input_dic, validation)
{
    "int   ": 42,
    "str   ": None
}

Nesting

Nested data structures, nested fields are treated the same as upper levels. It's possible to nest thousand of levels, it can be increased by upping recursion level of python. Nesting is done without any performance hit.

>>> input_dic = {
    "foo": {
	"foo_bar": "John Doe Street",
	"foo_baz": 123,
    }
}
>>> validation = {
    "foo": {"type": "dict", "key_regex": r"\w+", "nested": {
	"foo_bar": {"type": "str", "min_length": 5, "max_length": 99},
	"foo_baz": {"type": "int", "min_amount": 1},
	}
    }
}

Nesting of Dicts

>>> input = {
    'foobar': [
	{'name': 'John Doe', 'points': 22},
	{'name': 'Jane Doe', 'points': 23},
	{'name': 'willo wanka', 'points': 42},
    ]
}
>>> validation = {
    'foobar': {'type': 'list_dicts',  'nested': {
	    'name': {'type': 'str'},
	    'points': {'type': 'int'},
	}
    }
}

Extending Maat with custom validation

>>> from maat import types


>>> def datetime_parse(val, key, formats="%Y-%m-%dT%H:%M:%S.%f", *args, **kwargs):
    """ uses to parse iso format 'formats': '%Y-%m-%dT%H:%M:%S.%f'"""
    try:
        return datetime.strptime(val, formats)
    except Exception as e:
        raise Invalid(f'key: "{key}" contains invalid item')

>>> types['custom_datetime'] = datetime_parse

>>> input = {
    "created": "2022-01-28T15:01:46.0000",
}


>>> validation = {
    "created": {
        "type": "custom_datetime",
    }   
}

>>> validate(input, validation)
{'created': datetime.datetime(2022, 1, 28, 15, 1, 46)}

Installation

pip install maat

License

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

Note

This project is being used in production by multiple companies.

Benchmark

Benchmark open-sourced from Pydantic

Package Version Relative Performance Mean validation time
maat 3.0.4 15.8μs
attrs + cattrs 21.2.0 2.4x slower 37.6μs
pydantic 1.8.2 2.5x slower 39.7μs
voluptuous 0.12.1 6.2x slower 98.6μs
marshmallow 3.13.0 7.2x slower 114.1μs
trafaret 2.1.0 7.5x slower 118.5μs
schematics 2.1.1 26.6x slower 420.9μs
django-rest-framework 3.12.4 30.4x slower 482.2μs
cerberus 1.3.4 55.6x slower 880.2μs
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].