-
-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metaschema tests for patternProperties
#576
Comments
Hello! The need for tests beyond just valid or invalid is a long-standing issue but one that has to be done carefully given we don't have a formal backwards compatibility policy in the suite (essentially meaning we risk breaking lots of users of the suite). We can't simply start adding additional values to We indeed don't have a metaschema test for A PR to add a metaschema test would indeed be welcome for both of these cases, which is something we can express today in the suite - here's an example of how to write one if you care to help:
|
Thanks Julian. I can for my C code add side tests. I noticed I forgot to ask about "ignore non objects". I could be wrong here but in my code, the very first thing I check in my patternProperties validation function is if the JSON object type is an object. If the JSON is not an object the function returns valid (as in "ignore non object JSON"). Is this correct ? I guess the same question goes for the properties validation function as well. For clarity here is my function: int _jdac_check_patternproperties(json_object *jobj, json_object *jschema)
{
//printf("%s\n", __func__);
if (!json_object_is_type(jobj, json_type_object))
return JDAC_ERR_VALID; // ignore non objects
json_object *jprops = json_object_object_get(jschema, "patternProperties");
if (jprops) {
json_object_object_foreach(jprops, jprop_key, jprop_val) {
//printf("key of prop is %s\n", jprop_key);
regex_t regex;
int reti = regcomp(®ex, jprop_key, REG_EXTENDED);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
return JDAC_ERR_SCHEMA_ERROR;
}
json_object_object_foreach(jobj, jobj_key, jobj_val) {
//printf(" key of jobj is %s\n", jobj_key);
reti = regexec(®ex, jobj_key, 0, NULL, 0);
if (reti==0) {
//printf("match: %s\n", jobj_key);
int err = jdac_validate_node(jobj_val, jprop_val);
if (err) {
regfree(®ex);
return err;
}
}
}
regfree(®ex);
}
}
return JDAC_ERR_VALID;
} |
Side tests are probably best for now yeah until we can express these in the suite, which I hope to do soon. It's important to differentiate between when the instance being validated is not an object (where indeed then the spec says you consider it valid as your code seems to correctly) and separately where the value in the schema that Does that help? |
patternProperties
Hii @Julian please check if this test matches the requirements for this issue {
"description" : "metaschemas test for patternProperties",
"schema" : {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"patternProperties":{
"a" : {"$ref" : "https://json-schema.org/draft/2020-12/schema"}
}
},
"tests":[
{
"description" : "non object value",
"data": {"a" : 123},
"valid": false
},
{
"description" : "non object value",
"data": {"a" : ""},
"valid":false
},
{
"description" : "empty object value",
"data" : {"a" : {}},
"valid": true
},
{
"description": "boolean value",
"data" : {"a" : true},
"valid" : true
}
]
} |
Considering we backed some of these out (in #718 via #716 (comment)) it actually probably makes sense to close these I think. Thanks for giving it a shot though. |
Hi. I'm working on a schema validator for C using json-c. It's called jsonc-daccord.
Currently I'm running my test runner on the Test Suite tests/draft2020-12/patternProperties.json,
I noticed that it does not cover invalid regex patterns, and it does not cover "ignore non-object schemas"
The draft https://json-schema.org/draft/2020-12/json-schema-core.html#name-patternproperties says
The value of "patternProperties" MUST be an object.
What if it isn't ? Shall the patternProperties keyword be ignored ?
Each property name of this object SHOULD be a valid regular expression, according to the ECMA-262 regular expression dialect
When it says SHOULD, it means it is recommended.. Here is my problem, I need to test my code for handling of a failed regex since C code can risk of running into segfaults and memory leaks, but since the draft indicates it is recommended, how should the outcome of the test be? If we put a test in the patternProperties.json file, the result cannot be both true and false (or ignore). I wonder if somehow the tests "valid" field should have allowed for "ignore" as well as "true" and "false". A "valid": "ignore" would increase the coverage of the tests. Or perhaps better, to make it backwards compatible, a new field could be added to the json next to "valid", it could says "ignore": true.
for clarity, here is what I suggest:
What do you think ? :)
The text was updated successfully, but these errors were encountered: