Example #1
0
def simplify_schema_and_embed_checkers(s):
    ''' This function assumes the schema s is already canonicalized. 
        So it must be a dict '''
    #
    if s == {} or not definitions.Jkeywords.intersection(s.keys()):
        top = JSONtop()
        # top.update(s)
        return top
    if "not" in s.keys() and s["not"] == {}:
        bot = JSONbot()
        # del s["not"]
        # bot.update(s)
        return bot

    # json.array specific
    if "items" in s:
        if utils.is_dict(s["items"]):
            s["items"] = simplify_schema_and_embed_checkers(s["items"])
        elif utils.is_list(s["items"]):
            s["items"] = [
                simplify_schema_and_embed_checkers(i) for i in s["items"]
            ]

    if "additionalItems" in s and utils.is_dict(s["additionalItems"]):
        s["additionalItems"] = simplify_schema_and_embed_checkers(
            s["additionalItems"])

    # json.object specific
    if "properties" in s:
        s["properties"] = dict([(k, simplify_schema_and_embed_checkers(v))
                                for k, v in s["properties"].items()])

    if "patternProperties" in s:
        s["patternProperties"] = dict([
            (k, simplify_schema_and_embed_checkers(v))
            for k, v in s["patternProperties"].items()
        ])

    if "additionalProperties" in s and utils.is_dict(
            s["additionalProperties"]):
        s["additionalProperties"] = simplify_schema_and_embed_checkers(
            s["additionalProperties"])

    #
    if "type" in s:
        return typeToConstructor.get(s["type"])(s)

    if "not" in s:
        return typeToConstructor.get(s["not"]["type"]).neg(s["not"])

    if "anyOf" in s:
        anyofs = [simplify_schema_and_embed_checkers(i) for i in s["anyOf"]]
        return boolToConstructor.get("anyOf")({"anyOf": anyofs})

    if "allOf" in s:
        allofs = [simplify_schema_and_embed_checkers(i) for i in s["allOf"]]
        return boolToConstructor.get("allOf")({"allOf": allofs})
Example #2
0
def canoncalize_not(d):
    # d: {} has a not schema
    to_be_negated_schema = d["not"]
    if not isinstance(to_be_negated_schema, JSONschema):
        to_be_negated_schema = canoncalize_dict(to_be_negated_schema)

    # not schema is now in canonical form
    t = to_be_negated_schema.type
    if t in definitions.Jtypes:
        anyofs = []
        for t_i in definitions.Jtypes.difference([t]):
            anyofs.append(typeToConstructor.get(t_i)({"type": t_i}))
        anyofs.append(negTypeToConstructor.get(t)(to_be_negated_schema))
        anyofs = list(filter(None, anyofs))
        return boolToConstructor.get("anyOf")({"anyOf": anyofs})

    elif t in definitions.Jconnectors:

        if t == "not":
            return to_be_negated_schema

        if t == "anyOf":
            allofs = []
            for i in to_be_negated_schema["anyOf"]:
                allofs.append(canoncalize_not({"not": i}))
            return boolToConstructor.get("allOf")({"allOf": allofs})

        elif t == "allOf":
            anyofs = []
            for i in to_be_negated_schema["allOf"]:
                anyofs.append(canoncalize_not({"not": i}))
            return boolToConstructor.get("anyOf")({"anyOf": anyofs})

        elif t == "oneOf":
            sys.exit(">>>>>> oneOf is not supported yet. <<<<<<")