Hi, Rucha.
I decided i dislike the overall design of the struct json_schema.
Seems better to do it with C++ classes using virtual methods.
So instead of structures we'd have classes as
json_schema_const
json_schema_enum
json_scnema_string
json_schema_number
etc.
they all inherited from the
class json_schema_item
{
virtual parse()
virtual check()
}
Parsing the schema we can behave like this:
json_parse_schema()
checks all the keys on the current level
looking for 'const' 'enum' and 'typ[e' keywords only.
If it gets 'const' or 'enum' it creates the
json_schema_const/enum respectively
For the "type" it creates the appropriate json_schema_xxx and
callst ->parse() with the same JSON object. So the parse would go over it once
again looking for the type-specific keywords.
That way we don't need the hash_list as the destructor of the json_schema_xxx class
would free it's resources.
Minor comments.
You compare keys like strncmp((const char*)curr_key, "const", key_len)
all over the code. But it is not correct.
That way 'cons' or 'con' or 'co' or even 'c' - they all will be considered equal to the 'const'.
Just add the 'key_cmp()' function that would compare key lengths then do the strncmp.
> double val= je->s.cs->strntod((char *) je->value, je->value_len, &end;
You do this even if the value_type is not number. Not very safe. Better to avoid doing
that here at all.
That queriy just crashes the server. Didn't yet found why
SET @schema_array= '{
"typ": "arra",
"prefixItem": [
{ "typ": "objec"},
{ "typ": "objec",
"propertie": {
"number1": {"typ":"numbe"},
"string1": {"typ":"strin"},
"obj1" : {"typ":"objec",
"propertie": {
"key1" : {"type":"string"}
}
}
},
"required":["obj1"]
}
],
"items":false
}';
SELECT JSON_SCHEMA_VALID(@schema_array, '[{"key1":"value1"}, {"number1":1, "string1":"str1", "obj1":{"key1":"val1"}}]');
Feel free to discuss the design details!
Best regards.
HF