pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
470 stars 135 forks source link

Enable "pretty error messages" #161

Closed eike-fokken closed 2 years ago

eike-fokken commented 2 years ago

Hi! I intend to provide the validator error message to the user. In my case the json is rather small and it would be nice, if the call to dump in

class throwing_error_handler : public error_handler
{
    void error(const json::json_pointer &ptr, const json &instance, const std::string &message) override
    {
        throw std::invalid_argument(std::string("At ") + ptr.to_string() + " of " + instance.dump() + " - " + message + "\n");
    }
};

(in json-validator.cpp, lines 1290 in my submodule) could be replaced by dump(1,'\t'), so the user gets nicer looking json.

This would be a breaking change, so I thought about providing a default argument bool pretty_output=false to validate and error and dispatching on that one. But that would change a lot of other (virtually called) error methods, which don't have a use for this argument.

Do you see a possibility to support pretty output and if so, do you have opinions on how to implement them? (E.g. would you be fine with adding that default argument, although it is needed only in this special override?). I'd be happy to supply a PR.

eike-fokken commented 2 years ago

Speaking of which. In the same error message it's probably nice to distinguish between missing objects and wrong objects, because the error message for missing objects references a non-existing key. For example:

At  of {"flowInit":{"unit":"1000m_cube_per_hour","value":0.0},"flowMax":{"unit":"1000m_cube_per_hour","value":10000.0},"flowMin":{"unit":"1000m_cube_per_hour","value":0.0},"id":"node_4_ld1","to":"node_ld1"} - required property 'from' not found in object
pboettch commented 2 years ago

Why don't you create your error_handler-implementation to do what you want?

eike-fokken commented 2 years ago

Oh, that was easy, sorry.