Search code examples
rapidjson

How can I validate a subset of a RapidJSON document?


I'm using RapidJSON to parse messages that (roughly) conform to JSON-RPC. Here's an example of one such message:

{
    "method": "increment",
    "params": [ { "count": 42 } ]
}

The content of params depends on the value of method, so... I need to validate against a different schema for each possible value of method. As a step towards this goal, I created a map of schema documents, keyed by the method name:

std::unordered_map<std::string, rapidjson::SchemaDocument> schemas;

My intention was to do something like this (after parsing the received JSON into a RapidJSON document, doc):

if (schemas.find(doc["method"]) != schemas.end()) {
    validate(doc, schemas[doc]);
}

My problem is: I know how to validate a rapidjson::Document, but not a GenericValue instance (which is, I gather, what doc["method"] returns).

How can I validate a fragment or 'sub-document' of a RapidJSON document?

UPDATE/EXPLANATION: Thanks to @wsxedcrfv's answer, I now realize that my statement saying "I know how to validate a rapidjson::Document wasn't entirely accurate. I knew one way of validating a rapidjson::Document. But there's more than one way to do it, apparently. To clean up this question a bit for posterity, here's the validate() function that was missing from my original question:

bool validate(                                                                                                                                                                             
    rj::SchemaDocument const& schema,
    rj::Document *doc,
    std::string const& jsonMsg
)                  
{
    bool valid = false;              

    rj::StringStream ss(jsonMsg.c_str());

    rj::SchemaValidatingReader<
        rj::kParseDefaultFlags,
        rj::StringStream,
        rj::UTF8<>
    > reader(ss, schema);

    doc->Populate(reader);     

    if (!reader.GetParseResult()) {
        if (!reader.IsValid()) {                                  
            rj::StringBuffer sb;                        
            reader.GetInvalidSchemaPointer().StringifyUriFragment(sb);      
            printf("Message does not conform to schema!\n");
            printf("--------------------------------------------------------------------\n");
            printf("Invalid schema: %s\n", sb.GetString());
            printf("Invalid keyword: %s\n", reader.GetInvalidSchemaKeyword());
            sb.Clear();                                  
            reader.GetInvalidDocumentPointer().StringifyUriFragment(sb);    
            printf("Invalid document: %s\n", sb.GetString());
            printf("--------------------------------------------------------------------\n");
        } 

        else {
            printf("Message JSON is not well-formed!\n");
        }
    } 

    else {
        valid = true;
    }        

    return valid;
}

As @wsxedcrfv points out, another option is to create a SchemaValidator instance and pass it to the Accept() method of the (sub-)document:

#include "rapidjson/document.h"
#include <rapidjson/schema.h>
#include <iostream>


namespace rj = rapidjson;

namespace
{

std::string testMsg = R"msg({ "root": { "method": "control", "params": [ { "icc_delta_vol":  5 } ] } })msg";

std::string msgSchema = R"schema(
{
    "type": "object",

    "properties": {
        "method": { "$ref": "#/definitions/method" },
        "params": { "$ref": "#/definitions/paramsList" }
    },

    "required": [ "method", "params" ],

    "additionalProperties": false,

    "definitions": {
        // Omitted in the interest of brevity
        ...
    }
})schema";


} // End anonymous namespace

int main()
{
    rj::Document schemaDoc;

    if (schemaDoc.Parse(::msgSchema.c_str()).HasParseError()) {
        std::cout << "Schema contains invalid JSON, aborting...\n";
        exit(EXIT_FAILURE);
    }

    rj::SchemaDocument schema(schemaDoc);
    rj::SchemaValidator validator(schema);

    rj::Document doc;                                                                                                                                                                                                                     
    doc.Parse(::testMsg.c_str());

    std::cout << "doc.Accept(validator) = " << doc["root"].Accept(validator) << '\n';

    return 0;

Now that I know about this alternate method, I can easily use it to do context-specific validation of sub-documents/fragments...


Solution

  • I guess this answer is a bit late for you, but this works for me:

        char json[] = "{ \"a\" : 1, \"b\" : 1.2  } ";
        rapidjson::Document d;
        std::cout << "parse json  error? " << d.Parse(json).HasParseError() << "\n";
    
        char schema[] = "{ \"type\" : \"integer\"  } ";
        rapidjson::Document sd;
        std::cout << "parse schema error? " << sd.Parse(schema).HasParseError() << "\n";
        rapidjson::SchemaDocument s{sd}; //sd may now be deleted
    
        rapidjson::SchemaValidator vali{s};
        std::cout << "json " << d.Accept(vali) << "\n"; // 0
        vali.Reset();
        std::cout << "a    " << d.GetObject()["a"].Accept(vali) << "\n"; // 1
        vali.Reset();
        std::cout << "b    " << d.GetObject()["b"].Accept(vali) << "\n"; // 0
    

    I don't know which validate function you are using, but a Document is a GenericValue and the GenericValue provides Accept.