def JSONNumericFactory(s):
    '''Factory method handle the case of JSON number with multipleOf being integer.
        In this case, the JSON number becomes a JSON integer.'''
    # WARNING: calling this method with an empty dict like {} will always return
    # a json integer. should always be called with {"type": integer/number}

    # filter our default values which are not compatible with
    # jsonschema validation rules.
    if "minimum" in s and not utils.is_num(s["minimum"]):
        del s["minimum"]
    if "maximum" in s and not utils.is_num(s["maximum"]):
        del s["maximum"]
    if "multipleOf" in s and not utils.is_num(s["multipleOf"]):
        del s["multipleOf"]

    if s.get("type") == "number":
        if utils.is_int_equiv(s.get("multipleOf")):
            s["type"] = "integer"
            if s.get("minimum"):  # -I.inf:
                # s["minimum"] = math.floor(s.get("minimum")) if s.get(
                #     "exclusiveMinimum") else math.ceil(s.get("minimum"))
                s["minimum"] = math.floor(s.get("minimum"))
            if s.get("maximum"):  # I.inf:
                # s["maximum"] = math.ceil(s.get("maximum")) if s.get(
                #     "exclusiveMaximum") else math.floor(s.get("maximum"))
                s["maximum"] = math.floor(s.get("maximum"))
            return JSONTypeInteger(s)
        else:
            return JSONTypeNumber(s)
    else:
        return JSONTypeInteger(s)
示例#2
0
def rewrite_enum(d):
    t = d.get("type")
    enum = d.get("enum")
    ret = None

    if t == "string":
        pattern = "|".join(map(lambda x: "^"+str(x)+"$", enum))
        ret = {"type": "string", "pattern": pattern}

    if t == "integer":
        ret = {"anyOf": []}
        for i in enum:
            ret["anyOf"].append(
                {"type": "integer", "minimum": i, "maximum": i})

    if t == "number":
        ret = {"anyOf": []}
        for i in enum:
            if utils.is_int_equiv(i):
                ret["anyOf"].append(
                    {"type": "integer", "minimum": i, "maximum": i})
            else:
                ret["anyOf"].append(
                    {"type": "number", "minimum": i, "maximum": i})

    if t == "boolean":
        return d

    if t == "null":
        return {"type": "null"}

    if ret:
        return canonicalize_dict(ret)
    else:
        return d
示例#3
0
def rewrite_enum(d):
    t = d.get("type")
    enum = d.get("enum")
    ret = None

    if t == "string":
        pattern = "|".join(map(lambda x: "^" + str(x) + "$", enum))
        ret = {"type": "string", "pattern": pattern}

    if t == "integer":
        ret = {"anyOf": []}
        for i in enum:
            ret["anyOf"].append(
                # {"type": "number", "minimum": i, "maximum": i, "multipleOf": 1}) # check test_numeric/test_join_mulof10
                {
                    "type": "integer",
                    "minimum": i,
                    "maximum": i
                })

    if t == "number":
        ret = {"anyOf": []}
        for i in enum:
            if utils.is_int_equiv(i):
                ret["anyOf"].append({
                    "type": "integer",
                    "minimum": i,
                    "maximum": i
                })
            elif numpy.isnan(i):
                ret["anyOf"].append({"type": "number", "enum": [numpy.NaN]})
            else:
                ret["anyOf"].append({
                    "type": "number",
                    "minimum": i,
                    "maximum": i
                })

    if t == "boolean":
        # booleans are allowed to keep enums,
        # since there are only two values.
        return d

    if t == "null":
        # null schema should be rewritten without enum
        # it is a single value anyways.
        return {"type": "null"}

    if ret:
        ret["enum"] = enum
        return ret
        # return canonicalize_dict(ret)

    # Unsupported cases of rewriting enums
    elif t == 'array' or t == 'object':
        raise UnexpectedCanonicalization(
            msg='Rewriting the following enum is not supported.',
            tau=t,
            schema=d)