Exemplo n.º 1
0
def _helper_simplify(root_schema, child_schema):
    ret_schema = {}

    ## Refs override all other type definitions
    if is_ref(child_schema):
        try:
            ret_schema = _helper_simplify(root_schema, get_ref(root_schema, child_schema['$ref']))
        except RecursionError:
            raise JSONSchemaError('`$ref` path "{}" is recursive'.format(get_ref(root_schema, child_schema['$ref'])))

    else:

        ret_schema = {'type': get_type(child_schema)}

        if is_object(child_schema):
            properties = {}
            for field, field_json_schema in child_schema.get('properties', {}).items():
                properties[field] = _helper_simplify(root_schema, field_json_schema)

            ret_schema['properties'] = properties

        if is_iterable(child_schema):
            ret_schema['items'] = _helper_simplify(root_schema, child_schema.get('items', {}))

        if 'format' in child_schema:
            ret_schema['format'] = child_schema.get('format')

    if 'default' in child_schema:
        ret_schema['default'] = child_schema.get('default')

    return ret_schema
Exemplo n.º 2
0
def _helper_simplify(root_schema, child_schema):
    # We check this value to make simplify a noop for schemas which have _already_ been simplified
    if isinstance(child_schema, Cachable):
        return child_schema

    ## Refs override all other type definitions
    if _is_ref(child_schema):
        try:
            ret_schema = _helper_simplify(
                root_schema, get_ref(root_schema, child_schema['$ref']))

        except RecursionError:
            raise JSONSchemaError('`$ref` path "{}" is recursive'.format(
                get_ref(root_schema, child_schema['$ref'])))

    elif _is_allof(child_schema):
        ret_schema = _simplify__allof(root_schema, child_schema)

    elif is_anyof(child_schema):
        ret_schema = _simplify__anyof(root_schema, child_schema)

    else:
        ret_schema = _simplify__implicit_anyof(root_schema, child_schema)

    if 'default' in child_schema:
        ret_schema['default'] = child_schema.get('default')

    return Cachable(ret_schema)
Exemplo n.º 3
0
def _get_ref(schema, paths):
    if not paths:
        return schema

    if not paths[0] in schema:
        raise JSONSchemaError('`$ref` "{}" not found in provided JSON Schema'.format(paths[0]))

    return _get_ref(schema[paths[0]], paths[1:])
Exemplo n.º 4
0
def python_type(x):
    """
    Given a value `x`, return its Python Type as a JSONSchema type.
    :param x:
    :return:
    """
    if not type(x) in _PYTHON_TYPE_TO_JSON_SCHEMA:
        raise JSONSchemaError('Unknown type `{}`. Cannot translate to JSONSchema type.'.format(
            str(type(x))
        ))
    return _PYTHON_TYPE_TO_JSON_SCHEMA[type(x)]
Exemplo n.º 5
0
def _type_shorthand(type_s):
    if isinstance(type_s, list):
        shorthand = ''
        for t in sorted(type_s):
            shorthand += _type_shorthand(t)
        return shorthand

    if not type_s in _shorthand_mapping:
        raise JSONSchemaError(
            'Shorthand not available for type {}. Expected one of {}'.format(
                type_s, list(_shorthand_mapping.keys())))

    return _shorthand_mapping[type_s]
Exemplo n.º 6
0
def get_ref(schema, ref):
    """
    Given a JSON Schema dict, and a valid ref (`$ref`), get the JSON Schema from within schema
    :param schema: dict, JSON Schema
    :param ref: string
    :return: dict, JSON Schema
    :raises: Exception
    """

    # Explicitly only allow absolute internally defined $ref's
    if not re.match(r'^#/.*', ref):
        raise JSONSchemaError('Invalid format for `$ref`: "{}"'.format(ref))

    return _get_ref(schema, re.split('/', re.sub(r'^#/', '', ref)))