All Projects → spy16 → Pyschemes

spy16 / Pyschemes

Licence: wtfpl
PySchemes is a library for validating data structures in python

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pyschemes

Jsonschema
An implementation of the JSON Schema specification for Python
Stars: ✭ 3,474 (+851.78%)
Mutual labels:  schema, validation
Schema
📐 Validating data structures against a given Schema.
Stars: ✭ 359 (-1.64%)
Mutual labels:  schema, validation
Joiful
TypeScript Declarative Validation for Joi
Stars: ✭ 177 (-51.51%)
Mutual labels:  schema, validation
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+408.22%)
Mutual labels:  schema, validation
Joi
The most powerful data validation library for JS
Stars: ✭ 17,989 (+4828.49%)
Mutual labels:  schema, validation
Nope Validator
A small, simple and fast JS validator. Like, wow thats fast. 🚀
Stars: ✭ 142 (-61.1%)
Mutual labels:  schema, validation
Schematics
Project documentation: https://schematics.readthedocs.io/en/latest/
Stars: ✭ 2,461 (+574.25%)
Mutual labels:  schema, validation
Rdfunit
An RDF Unit Testing Suite
Stars: ✭ 117 (-67.95%)
Mutual labels:  schema, validation
openapi-schema-validator
OpenAPI schema validator for Python
Stars: ✭ 35 (-90.41%)
Mutual labels:  schema, validation
Truss
Assertions API for Clojure/Script
Stars: ✭ 239 (-34.52%)
Mutual labels:  schema, validation
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-62.74%)
Mutual labels:  schema, validation
Structure
A simple schema/attributes library built on top of modern JavaScript
Stars: ✭ 292 (-20%)
Mutual labels:  schema, validation
Pandasschema
A validation library for Pandas data frames using user-friendly schemas
Stars: ✭ 135 (-63.01%)
Mutual labels:  schema, validation
Schema Utils
Options Validation
Stars: ✭ 162 (-55.62%)
Mutual labels:  schema, validation
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 (-66.03%)
Mutual labels:  schema, validation
Computed Types
🦩 Joi like validations for TypeScript
Stars: ✭ 197 (-46.03%)
Mutual labels:  schema, validation
Vue Rawmodel
RawModel.js plugin for Vue.js v2. Form validation has never been easier!
Stars: ✭ 79 (-78.36%)
Mutual labels:  schema, validation
Postguard
🐛 Statically validate Postgres SQL queries in JS / TS code and derive schemas.
Stars: ✭ 104 (-71.51%)
Mutual labels:  schema, validation
Password Validator
Validates password according to flexible and intuitive specification
Stars: ✭ 224 (-38.63%)
Mutual labels:  schema, validation
fefe
Validate, sanitize and transform values with proper TypeScript types and zero dependencies.
Stars: ✭ 34 (-90.68%)
Mutual labels:  schema, validation

PySchemes

PySchemes is a library for validating data structures in Python. PySchemes is designed to be simple and Pythonic.

https://travis-ci.org/shivylp/pyschemes

Features

  • Simple representation of schema using primitive Python types (Or Complex types as well)
  • Sane Schema Structures
  • Sane errors
  • Power of PySchemes lies in its validators (take a look at tests to understand usage of various validators)
  • Lambda functions or any callable can be easily used as a validator

Examples

# Execute this before executing any of the examples
>>> from pyschemes import Scheme, validators
  1. Simple TypeChecking
>>> Scheme(int).validate(10)
10

>>> Scheme(int).validate(10.3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected type: 'int', got 'float'

>>> from collections import Iterable
>>> Scheme(Iterable).validate([1, 2])
[1, 2]

>>> Scheme(Iterable).validate(("hello", ))
("hello", )

>>> Scheme(Iterable).validate({"a": "b", "c": "d"})
{"a": "b", "c": "d"}

>>> Scheme(Iterable).validate(range(100))
range(0, 100)

>>> Scheme(Iterable).validate(lambda x: x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected type: 'Iterable', got 'function'
  1. Simple Value Validation
>>> Scheme(10).validate(10)
10

>>> Scheme(10).validate(10.3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: expected value '10', got '10.3'
  1. Simple Choices Validation
>>> choices = validators.Any(["choiceA", "choiceB", "choiceC"])

>>> Scheme(choices).validate("choiceA")
'choiceA'

>>> Scheme(choices).validate("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: value did not pass any validation
  1. Validating a List/Iterable Scheme
>>> Scheme([str, 2, int]).validate(["hello", 2, 15])
["hello", 2, 15]

>>> Scheme((str, 2, int)).validate(("hello", 2, 15))
("hello", 2, 15)

>>> Scheme((str, 2, int)).validate(["hello", 2, 15])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected type: 'tuple', got 'list'

>>> Scheme([str, 2, int]).validate(["hello", 3, 130])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: element at index 1 (expected value '2', got '3')

>>> Scheme([str, 2, int]).validate(["hello", 2, 4.5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: element at index 2 (expected type: 'int', got 'float')
  1. Validating a Dictionary/Mapping Scheme
>>> Scheme({str: int}).validate({"a": 1, "b": 2})
{'a': 1, 'b': 2}

>>> Scheme({str: int}).validate({"a": 1, "b": 3.5})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: at key 'b' (expected type: 'int', got 'float')

>>> Scheme({"hello": 10, str: object}).validate({"hello": 10, "world": 12})
{"hello": 10, "world": 12}

>>> Scheme({"required-key": int}).validate({})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: missing key 'required-key'

>>> from pyschemes.validators import Optional
>>> Scheme({"k": Optional(str, "hello")}).validate({})
{'k': 'hello'}

>>> Scheme({"k": Optional(str, "hello")}).validate({"k": "world"})
{'k': 'world'}

>>> Scheme({"k": Optional(int, 10)}).validate({"k": "world"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: at key 'k' (expected type: 'int', got 'str')

>>> Scheme({"only-key": str}).validate({"only-key": "hello", "b": "not-allowed"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: at key 'b' (not allowed)
  1. Lambda/Callable Scheme
>>> Scheme(lambda x: x < 100).validate(10)
10

>>> Scheme(lambda x: x < 100).validate(101)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: validator '<lambda>' failed for value '101'

>>> def CustomValidator(value):
...    if value == "foo" or value in range(100):
...       return value
...    else:
...       return False
...
>>> Scheme(CustomValidator).validate(101)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: validator 'CustomValidator' failed for value '10'

>>> Scheme(CustomValidator).validate(9)
9
>>> Scheme(CustomValidator).validate("foo")
'foo'
  1. Compund Schemes
>>> Scheme({"test": lambda x: x < 100}).validate({"test": 10})
{'test': 10}

>>> Scheme({"test": lambda x: x < 100}).validate({"test": 101})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: at key 'test' (validator '<lambda>' failed for value '101')

>>> Scheme({"test": Scheme({"a": str, "b": int})}).validate({"test": {}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: at key 'test' (missing key 'a')

>>> Scheme({"test": Scheme({"b": int})}).validate({"test": {"b": 1.5}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: at key 'test' (at key 'b' (expected type: 'int', got 'float'))

>>> Scheme({"t": {str: int}}).validate({"t": {"a": 1}})
{'t': {'a': 1}}

>>> Scheme({"t": (str, int)}).validate({"t": ("a", 1)})
{'t': ('a', 1)}

And more to come in tests!

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].