lale/schema_simplifier.py
def simplify(schema: JsonSchema, floatAny: bool) -> JsonSchema:
    """Tries to simplify a schema into an equivalent but
    more compact/simpler one.  If floatAny if true, then
    the only anyOf in the return value will be at the top level.
    Using this option may cause a combinatorial blowup in the size
    of the schema
    """
    if is_true_schema(schema):
        return STrue
    if is_false_schema(schema):
        return SFalse
    if "enum" in schema:
        # TODO: simplify the schemas by removing anything that does not validate
        # against the rest of the schema
        return schema
    if "allOf" in schema:
        ret = simplifyAll(schema["allOf"], floatAny)
        return ret
    elif "anyOf" in schema:
        ret = simplifyAny(schema["anyOf"], floatAny)
        return ret
    elif "not" in schema:
        return simplifyNot(schema["not"], floatAny)
    elif "type" in schema and schema["type"] == "object" and "properties" in schema:
        schema2 = schema.copy()
        props = {}
        all_objs = [schema2]
        # TODO: how does this interact with required?
        # {k1:s_1, k2:anyOf:[s2s], k3:anyOf:[s3s]}
        # If floatAny is true and any properties have an anyOf in them
        # we need to float it out to the top.  We can then
        # give it to simplifyAll, which does the cross product to lift
        # them out of the list
        for k, v in schema["properties"].items():
            s = simplify(v, floatAny)
            if is_false_schema(s) and "required" in schema and s in schema["required"]:
                logger.info(
                    f"simplify: required key {k} is False, so the entire schema {schema} is False"
                )
                return impossible()

            if (not is_true_schema(s)) and floatAny and "anyOf" in s:
                all_objs.append(
                    {
                        "anyOf": [
                            {"type": "object", "properties": {k: vv}}
                            for vv in s["anyOf"]
                        ]
                    }
                )
                # If we are disallowing additionalProperties, then we can't remove this property entirely

                if not schema.get("additionalProperties", True):
                    props[k] = STrue
            else:
                props[k] = s
        schema2["properties"] = props
        if len(all_objs) == 1:
            return schema2
        else:
            # The termination argument here is somewhat subtle
            s = simplifyAll(all_objs, floatAny)
            return s
    else:
        return schema
Tries to simplify a schema into an equivalent but
    more compact/simpler one.  If floatAny if true, then
    the only anyOf in the return value will be at the top level.
    Using this option may cause a combinatorial blowup in the size
    of the schema
    
Tries to simplify a schema into an equivalent but
more compact/simpler one.  If floatAny if true, then
the only anyOf in the return value will be at the top level.
Using this option may cause a combinatorial blowup in the size
of the schema
Tries to simplify a schema into an equivalent but more compact / simpler one . If floatAny if true then the only anyOf in the return value will be at the top level . Using this option may cause a combinatorial blowup in the size of the schema
def simplify(schema: JsonSchema, floatAny: bool) -> JsonSchema:
    if is_true_schema(schema):
        return STrue
    if is_false_schema(schema):
        return SFalse
    if "enum" in schema:
        return schema
    if "allOf" in schema:
        ret = simplifyAll(schema["allOf"], floatAny)
        return ret
    elif "anyOf" in schema:
        ret = simplifyAny(schema["anyOf"], floatAny)
        return ret
    elif "not" in schema:
        return simplifyNot(schema["not"], floatAny)
    elif "type" in schema and schema["type"] == "object" and "properties" in schema:
        schema2 = schema.copy()
        props = {}
        all_objs = [schema2]
        for k, v in schema["properties"].items():
            s = simplify(v, floatAny)
            if is_false_schema(s) and "required" in schema and s in schema["required"]:
                logger.info(
                    f"simplify: required key {k} is False, so the entire schema {schema} is False"
                )
                return impossible()
            if (not is_true_schema(s)) and floatAny and "anyOf" in s:
                all_objs.append(
                    {
                        "anyOf": [
                            {"type": "object", "properties": {k: vv}}
                            for vv in s["anyOf"]
                        ]
                    }
                )
                if not schema.get("additionalProperties", True):
                    props[k] = STrue
            else:
                props[k] = s
        schema2["properties"] = props
        if len(all_objs) == 1:
            return schema2
        else:
            s = simplifyAll(all_objs, floatAny)
            return s
    else:
        return schema
def simplify ( schema : JsonSchema , floatAny : bool ) -> JsonSchema : if is_true_schema ( schema ) : return STrue if is_false_schema ( schema ) : return SFalse if "enum" in schema : return schema if "allOf" in schema : ret = simplifyAll ( schema [ "allOf" ] , floatAny ) return ret elif "anyOf" in schema : ret = simplifyAny ( schema [ "anyOf" ] , floatAny ) return ret elif "not" in schema : return simplifyNot ( schema [ "not" ] , floatAny ) elif "type" in schema and schema [ "type" ] == "object" and "properties" in schema : schema2 = schema . copy ( ) props = { } all_objs = [ schema2 ] for k , v in schema [ "properties" ] . items ( ) : s = simplify ( v , floatAny ) if is_false_schema ( s ) and "required" in schema and s in schema [ "required" ] : logger . info ( f"simplify: required key {k} is False, so the entire schema {schema} is False" ) return impossible ( ) if ( not is_true_schema ( s ) ) and floatAny and "anyOf" in s : all_objs . append ( { "anyOf" : [ { "type" : "object" , "properties" : { k : vv } } for vv in s [ "anyOf" ] ] } ) if not schema . get ( "additionalProperties" , True ) : props [ k ] = STrue else : props [ k ] = s schema2 [ "properties" ] = props if len ( all_objs ) == 1 : return schema2 else : s = simplifyAll ( all_objs , floatAny ) return s else : return schema
Tries to simplify a schema into an equivalent but
more compact/simpler one.
Tries to simplify a schema into an equivalent but more compact / simpler one .
"""Tries to simplify a schema into an equivalent but
    more compact/simpler one.  If floatAny if true, then
    the only anyOf in the return value will be at the top level.
    Using this option may cause a combinatorial blowup in the size
    of the schema
    """ # TODO: simplify the schemas by removing anything that does not validate # against the rest of the schema # TODO: how does this interact with required? # {k1:s_1, k2:anyOf:[s2s], k3:anyOf:[s3s]} # If floatAny is true and any properties have an anyOf in them # we need to float it out to the top.  We can then # give it to simplifyAll, which does the cross product to lift # them out of the list # If we are disallowing additionalProperties, then we can't remove this property entirely # The termination argument here is somewhat subtle
schema
JsonSchema
floatAny
bool