jsonmodels package

Submodules

jsonmodels.builders module

Builders to generate in memory representation of model and fields tree.

class jsonmodels.builders.Builder(parent=None, nullable=False)[source]

Bases: object

add_definition(builder)[source]
count_type(type)[source]
get_builder(type)[source]
static maybe_build(value)[source]
register_type(type, builder)[source]
class jsonmodels.builders.EmbeddedBuilder(*args, **kwargs)[source]

Bases: jsonmodels.builders.Builder

add_type_schema(schema)[source]
build()[source]
is_definition
class jsonmodels.builders.ListBuilder(*args, **kwargs)[source]

Bases: jsonmodels.builders.Builder

add_type_schema(schema)[source]
build()[source]
is_definition
class jsonmodels.builders.ObjectBuilder(model_type, *args, **kwargs)[source]

Bases: jsonmodels.builders.Builder

add_field(name, field, schema)[source]
build()[source]
build_definition(add_defintitions=True, nullable=False)[source]
is_definition
is_root
type_name
class jsonmodels.builders.PrimitiveBuilder(*args, **kwargs)[source]

Bases: jsonmodels.builders.Builder

build()[source]
set_type(type)[source]

jsonmodels.collections module

class jsonmodels.collections.ModelCollection(field)[source]

Bases: list

ModelCollection is list which validates stored values.

Validation is made with use of field passed to __init__ at each point, when new value is assigned.

append(value)[source]

jsonmodels.errors module

exception jsonmodels.errors.FieldNotFound[source]

Bases: exceptions.RuntimeError

exception jsonmodels.errors.FieldNotSupported[source]

Bases: exceptions.ValueError

exception jsonmodels.errors.ValidationError[source]

Bases: exceptions.RuntimeError

jsonmodels.fields module

class jsonmodels.fields.BaseField(required=False, nullable=False, help_text=None, validators=None)[source]

Bases: object

Base class for all fields.

static get_default_value()[source]

Get default value for field.

Each field can specify its default.

parse_value(value)[source]

Parse value from primitive to desired format.

Each field can parse value to form it wants it to be (like string or int).

to_struct(value)[source]

Cast value to Python structure.

types = None
validate(value)[source]
validate_for_object(obj)[source]
class jsonmodels.fields.BoolField(required=False, nullable=False, help_text=None, validators=None)[source]

Bases: jsonmodels.fields.BaseField

Bool field.

parse_value(value)[source]

Cast value to bool.

types = (<type 'bool'>,)
class jsonmodels.fields.DateField(str_format=None, *args, **kwargs)[source]

Bases: jsonmodels.fields.StringField

Date field.

default_format = '%Y-%m-%d'
parse_value(value)[source]

Parse string into instance of date.

to_struct(value)[source]

Cast date object to string.

types = (<type 'datetime.date'>,)
class jsonmodels.fields.DateTimeField(str_format=None, *args, **kwargs)[source]

Bases: jsonmodels.fields.StringField

Datetime field.

parse_value(value)[source]

Parse string into instance of datetime.

to_struct(value)[source]

Cast datetime object to string.

types = (<type 'datetime.datetime'>,)
class jsonmodels.fields.EmbeddedField(model_types, *args, **kwargs)[source]

Bases: jsonmodels.fields.BaseField

Field for embedded models.

parse_value(value)[source]

Parse value to proper model type.

to_struct(value)[source]
validate(value)[source]
class jsonmodels.fields.FloatField(required=False, nullable=False, help_text=None, validators=None)[source]

Bases: jsonmodels.fields.BaseField

Float field.

types = (<type 'float'>, <type 'int'>)
class jsonmodels.fields.IntField(required=False, nullable=False, help_text=None, validators=None)[source]

Bases: jsonmodels.fields.BaseField

Integer field.

types = (<type 'int'>,)
class jsonmodels.fields.ListField(items_types=None, *args, **kwargs)[source]

Bases: jsonmodels.fields.BaseField

List field.

get_default_value()[source]
parse_value(values)[source]

Cast value to proper collection.

to_struct(values)[source]
types = (<type 'list'>,)
validate(value)[source]
validate_single_value(item)[source]
class jsonmodels.fields.StringField(required=False, nullable=False, help_text=None, validators=None)[source]

Bases: jsonmodels.fields.BaseField

String field.

types = (<type 'basestring'>,)
class jsonmodels.fields.TimeField(str_format=None, *args, **kwargs)[source]

Bases: jsonmodels.fields.StringField

Time field.

parse_value(value)[source]

Parse string into instance of time.

to_struct(value)[source]

Cast time object to string.

types = (<type 'datetime.time'>,)

jsonmodels.models module

class jsonmodels.models.Base(**kwargs)[source]

Bases: object

Base class for all models.

get_field(field_name)[source]

Get field associated with given attribute.

classmethod iterate_over_fields()[source]

Iterate through fields and values.

populate(**kw)[source]

Populate values to fields. Skip non-existing.

classmethod to_json_schema()[source]

Generate JSON schema for model.

to_struct()[source]

Cast model to Python structure.

validate()[source]

Explicitly validate all the fields.

jsonmodels.parsers module

Parsers to change model structure into different ones.

jsonmodels.parsers.build_json_schema(value, parent_builder=None)[source]
jsonmodels.parsers.build_json_schema_object(cls, parent_builder=None)[source]
jsonmodels.parsers.build_json_schema_primitive(cls, parent_builder)[source]
jsonmodels.parsers.to_json_schema(cls)[source]

Generate JSON schema for given class.

Parameters:cls – Class to be casted.
Return type:dict
jsonmodels.parsers.to_struct(model)[source]

Cast instance of model to python structure.

Parameters:model – Model to be casted.
Return type:dict

jsonmodels.utilities module

class jsonmodels.utilities.PythonRegex(regex, flags)

Bases: tuple

flags

Alias for field number 1

regex

Alias for field number 0

jsonmodels.utilities.compare_schemas(one, two)[source]

Compare two structures that represents JSON schemas.

For comparison you can’t use normal comparison, because in JSON schema lists DO NOT keep order (and Python lists do), so this must be taken into account during comparison.

Note this wont check all configurations, only first one that seems to match, which can lead to wrong results.

Parameters:
  • one – First schema to compare.
  • two – Second schema to compare.
Return type:

bool

jsonmodels.utilities.convert_ecma_regex_to_python(value)[source]

Convert ECMA 262 regex to Python tuple with regex and flags.

If given value is already Python regex it will be returned unchanged.

Parameters:value (string) – ECMA regex.
Returns:2-tuple with regex and flags
Return type:namedtuple
jsonmodels.utilities.convert_python_regex_to_ecma(value, flags=[])[source]

Convert Python regex to ECMA 262 regex.

If given value is already ECMA regex it will be returned unchanged.

Parameters:
  • value (string) – Python regex.
  • flags (list) – List of flags (allowed flags: re.I, re.M)
Returns:

ECMA 262 regex

Return type:

str

jsonmodels.utilities.is_ecma_regex(regex)[source]

Check if given regex is of type ECMA 262 or not.

Return type:bool

jsonmodels.validators module

Predefined validators.

class jsonmodels.validators.Length(minimum_value=None, maximum_value=None)[source]

Bases: object

Validator for length.

modify_schema(field_schema)[source]

Modify field schema.

validate(value)[source]

Validate value.

class jsonmodels.validators.Max(maximum_value, exclusive=False)[source]

Bases: object

Validator for maximum value.

modify_schema(field_schema)[source]

Modify field schema.

validate(value)[source]

Validate value.

class jsonmodels.validators.Min(minimum_value, exclusive=False)[source]

Bases: object

Validator for minimum value.

modify_schema(field_schema)[source]

Modify field schema.

validate(value)[source]

Validate value.

class jsonmodels.validators.Regex(pattern, **flags)[source]

Bases: object

Validator for regular expressions.

FLAGS = {'ignorecase': 2, 'multiline': 8}
modify_schema(field_schema)[source]

Modify field schema.

validate(value)[source]

Validate value.

Module contents