schemify

Simple schema validation library
Download

schemify Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Other/Proprietary Li...
  • Price:
  • FREE
  • Publisher Name:
  • Andrey Popp

schemify Tags


schemify Description

schemify is a very simple Python schema validation library.The source code:from webob.exc import HTTPBadRequest__all__ = ('validate', 'opt', 'anything', 'ValidationError')class ValidationError(ValueError): """ Validation error""" def __init__(self, error): self.error = error super(ValueError, self).__init__(error)def validate(schema, data): if schema is anything: return data if isinstance(schema, tuple) and hasattr(schema, '_replace'): result = validate(schema._asdict(), data) return type(schema)(**result) if isinstance(schema, dict): result, errors = {}, {} for k, v in schema.items(): if not k in data: if not isinstance(v, opt): errors = 'missing %s key' % k elif v.default is not _no_default: result = v.default continue if isinstance(v, opt): v = v.type try: result = validate(v, data) except ValidationError as e: errors = e.error if errors: raise ValidationError(errors) return result elif isinstance(schema, list): assert len(schema) == 1, 'invalid schema' return , v) for v in data] elif isinstance(schema, tuple): if not len(schema) == len(data): raise ValidationError('length should be equal to %d' % len(schema)) return tuple(validate(s, v) for s, v in zip(schema, data)) else: try: return schema(data) except ValueError as e: raise ValidationError(str(e))anything = object()_no_default = object()class opt(object): """ Marker for optional elements in container""" def __init__(self, type, default=_no_default): self.type = type self.default = defaultProduct's homepage


schemify Related Software