danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
697 stars 160 forks source link

Validating an invalid data #520

Closed jvenkat74 closed 2 months ago

jvenkat74 commented 2 months ago

When trying to validate the data against schema, we get it as valid using jsoncons, whereas it is not valid (as start variable has a string instead of integer)

Enumerate the steps to reproduce the bug

Attached are schema.json and data.json. This works against online JSON Schema validator properly. data.json schema.json

What compiler, architecture, and operating system?

What jsoncons library version?

danielaparker commented 2 months ago

Interesting. This is the output that I get when I run it without format validation:

#include <iostream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonschema/jsonschema.hpp>

// for brevity
using jsoncons::json;
using jsoncons::ojson;
namespace jsonschema = jsoncons::jsonschema;

int main() 
{
    std::string schema_str = R"(/*schema*/)";

    std::string data_str = R"(/*data*/)";

    try
    {
        ojson schema = ojson::parse(schema_str);
        jsonschema::json_schema<ojson> compiled = jsonschema::make_json_schema(std::move(schema)/*,
            jsonschema::evaluation_options{}.require_format_validation(true)*/);
        ojson data = ojson::parse(data_str);

        jsoncons::json_decoder<ojson> decoder;
        compiled.validate(data, decoder);
        ojson output = decoder.get_result();
        std::cout << pretty_print(output) << "\n";
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << "\n";
    }
}

Output:

[
    {
        "valid": false,
        "evaluationPath": "/properties/logicalapps/items/properties/listenIP/oneOf",
        "schemaLocation": "#/properties/logicalapps/items/properties/listenIP/oneOf",
        "instanceLocation": "/logicalapps/0/listenIP",
        "error": "Must be valid against exactly one schema, but found 2 matching schemas at indices 0,1"
    },

    ...

    {
        "valid": false,
        "evaluationPath": "/properties/logicalapps/items/properties/clientMode/items/properties/port/items/properties/start/type",
        "schemaLocation": "#/properties/logicalapps/items/properties/clientMode/items/properties/port/items/properties/start",
        "instanceLocation": "/logicalapps/0/clientMode/0/port/0/start",
        "error": "Expected integer, found string"
    }
]

Without format validation of the ip4 and ip6 addresses, it shows the integer error. But with, it doesn't. I need to check.

danielaparker commented 2 months ago

@jvenkat74 , this should be fixed on master, please check. The format validator was incorrectly returning an abort code on failure, so processing stopped before the port node.

jvenkat74 commented 2 months ago

It has been fixed. Error is getting flagged now. Thanks!