rapidjson icon indicating copy to clipboard operation
rapidjson copied to clipboard

Schema validation shows only the 1st error

Open SidGandhi17 opened this issue 7 years ago • 3 comments

Hi,

I have a schema and data json. Based on the schema there should be two schema validation errors, but rapidjson is only reporting the first one.

schema json: { "type": "object", "properties": { "intProp": {"type": "integer", "minimum": 0, "maximum": 100}, "strEnumProp": {"type": "string", "enum": ["enum1", "enum2"]} }, "additionalProperties": false }

data json: { "intProp": "a", "strEnumProp": "enum3" }

Here is my code:

#include "rapidjson/document.h" #include "rapidjson/schema.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" #include "rapidjson/filereadstream.h" #include #include #include

using namespace std; using namespace rapidjson;

int main(int argc, char *argv[]) {

//parse schema json
FILE * pFileSchema = fopen (argv[1] , "rb");
char buffer_2[65536];
FileReadStream isSchema(pFileSchema, buffer_2, sizeof(buffer_2));
Document documentSchema;
documentSchema.ParseStream<0, UTF8<> >(isSchema);
fclose(pFileSchema);

//print schema json
StringBuffer buffer2;
rapidjson::Writer<StringBuffer> writer2(buffer2);
documentSchema.Accept(writer2);
//cout << argv[1]  << buffer2.GetString() << endl;
rapidjson::SchemaDocument schemaDocument(documentSchema);

//parse document json
char buffer_3[65536];
FILE * testFilePtr = fopen (argv[2] , "rb");
FileReadStream isTestFile(testFilePtr, buffer_2, sizeof(buffer_2));
Document d2;
d2.ParseStream(isTestFile);
fclose(testFilePtr);

//print document json
StringBuffer buffer1;
rapidjson::Writer<StringBuffer> writer1(buffer1);
d2.Accept(writer1);
//cout << argv[2]<< buffer1.GetString() << endl;

//validate and print errors
SchemaValidator validator(schemaDocument);
bool ret = d2.Accept(validator);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
validator.GetError();
validator.GetError().Accept(writer);
std::string s = sb.GetString();
cout << "return value of validation" << ret << endl;
    printf("Schema validation errors: %s\n", s.c_str());
    printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());



return 0;

}

Output: $ ./a.out schema.json test.json schema.json{"type":"object","properties":{"intProp":{"type":"integer","minimum":0,"maximum":100},"strEnumProp":{"type":"string","enum":["enum1","enum2"]}},"additionalProperties":false} test.json{"intProp":"a","strEnumProp":"enum3"} return value of validation0 Schema validation errors: {"type":{"expected":["integer"],"actual":"string","instanceRef":"#/intProp","schemaRef":"#/properties/intProp"}} Invalid keyword: type

SidGandhi17 avatar May 10 '18 22:05 SidGandhi17

Yes, currently only the first encountered error will be shown.

miloyip avatar May 11 '18 02:05 miloyip

Hey Milo,

Thanks for the quick reply. Is there any plan to add support for this in the near future?

SidGandhi17 avatar May 11 '18 18:05 SidGandhi17

Hi,

Are there any updates for this?

Thanks

ZoraizQ avatar Sep 06 '24 21:09 ZoraizQ