JSON_Status json_validate()

in WifiConfigurationViaAppResource/src/parson.c [2038:2101]


JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value)
{
    JSON_Value *temp_schema_value = NULL, *temp_value = NULL;
    JSON_Array *schema_array = NULL, *value_array = NULL;
    JSON_Object *schema_object = NULL, *value_object = NULL;
    JSON_Value_Type schema_type = JSONError, value_type = JSONError;
    const char *key = NULL;
    size_t i = 0, count = 0;
    if (schema == NULL || value == NULL) {
        return JSONFailure;
    }
    schema_type = json_value_get_type(schema);
    value_type = json_value_get_type(value);
    if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */
        return JSONFailure;
    }
    switch (schema_type) {
    case JSONArray:
        schema_array = json_value_get_array(schema);
        value_array = json_value_get_array(value);
        count = json_array_get_count(schema_array);
        if (count == 0) {
            return JSONSuccess; /* Empty array allows all types */
        }
        /* Get first value from array, rest is ignored */
        temp_schema_value = json_array_get_value(schema_array, 0);
        for (i = 0; i < json_array_get_count(value_array); i++) {
            temp_value = json_array_get_value(value_array, i);
            if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
                return JSONFailure;
            }
        }
        return JSONSuccess;
    case JSONObject:
        schema_object = json_value_get_object(schema);
        value_object = json_value_get_object(value);
        count = json_object_get_count(schema_object);
        if (count == 0) {
            return JSONSuccess; /* Empty object allows all objects */
        } else if (json_object_get_count(value_object) < count) {
            return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */
        }
        for (i = 0; i < count; i++) {
            key = json_object_get_name(schema_object, i);
            temp_schema_value = json_object_get_value(schema_object, key);
            temp_value = json_object_get_value(value_object, key);
            if (temp_value == NULL) {
                return JSONFailure;
            }
            if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
                return JSONFailure;
            }
        }
        return JSONSuccess;
    case JSONString:
    case JSONNumber:
    case JSONBoolean:
    case JSONNull:
        return JSONSuccess; /* equality already tested before switch */
    case JSONError:
    default:
        return JSONFailure;
    }
}