Home > Back-end >  rapidjson schema how to get the keyword from the error
rapidjson schema how to get the keyword from the error

Time:03-18

I'm making a physical software and we deploy a json solution and I wanted to used json schema. So, when I had a wrong key typical looking a "length" in the schema and the user gives somethings wrong like "length2". I don't know how to get it with rapidjson actually, I obtained these results

Invalid schema: #/properties/TEST
Invalid keyword: required
Invalid document: #/TEST 

But I want an output like "length" key is missing in order to inform the user.

Imy test.json file:

{
    "$schema": "./schema.json", 
    "TEST": {
        "Length2": {
            "Value":20,
            "Unit":"mm"
        }
    } 
}

Edit following Ether comment my schema.json but this doesn't change my output see "Invalid schema"

{
    "type": "object",
    "required": ["TEST"],
    "properties": {
        "TEST": {
            "type": "object",
            "required": ["Length"],
            "properties": {
                    "Length":{
                        "type": "object",
                        "required": ["Value","Unit"],
                        "properties": {
                            "Value": {"type": "number"},
                            "Unit": {"type": "string"}
                    }       
                }         
            }
        }
    }
}

and my cpp code:

#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/schema.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
using namespace rapidjson;

int main()

{
    char readBuffer[65536];
    FILE* fp = fopen("test.json", "r");
    FileReadStream is(fp, readBuffer, sizeof(readBuffer));
    Document d;
    d.ParseStream(is);

   FILE* fp2 = fopen("schema.json", "r");
    FileReadStream fs(fp2, readBuffer, sizeof(readBuffer));
    Document sd;
    sd.ParseStream(fs);

    SchemaDocument schema(sd);
    SchemaValidator validator(schema);
    
    if(!d.Accept(validator))
    {
                rapidjson::StringBuffer sb;
                validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
                printf("Invalid schema: %s\n", sb.GetString());
                printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());
                sb.Clear();
                validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
                printf("Invalid document: %s\n", sb.GetString());
    }
    else
        printf("\nJson file validated with the given schema successfully\n");
        return 0;
} 

CodePudding user response:

At /properties/TEST/properties you should have property names, not a schema. Perhaps you are missing the "Length" property to contain that subschema?

CodePudding user response:

So I found my answer by following the schemavalidator.cpp example in rapidjson folder. I provide here the example in my case : https://github.com/faudard/rapidjson_scheme I use the "CreateErrorMessages" same to the example.

  • Related