json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

provide additional option to use custom error handler for set_root_schema

Open BitFis opened this issue 4 years ago • 10 comments

Would it be possible to extend the json_validator::set_root_schema(json &&schema) function to allow also a custom error handler like json_validator::validate(...) has?

This would allow me to provide a more precise and user custom error than the exception provides. I am happy to provide a MR if this feature is welcome. I propose following extension:

// error_handler and works the same way than validator error_handler
void json_validator::set_root_schema(json &&schema, error_handler &err);

I am also open for alternatives.

Cheers

BitFis avatar Nov 30 '21 11:11 BitFis

You mean, instead of the thrown exceptions in case of errors? Are there that many errors thrown when setting the root schema?

pboettch avatar Dec 01 '21 16:12 pboettch

Providing a schema via argument / startup of the application provided by the user those erros will occure. I agree that those errors do not occure as often, but they will occure and providing precise information via error_handler will help fix those. Also I am planning to integrate this schema validator in an embedded environment which will have exceptions turned off.

BitFis avatar Dec 01 '21 16:12 BitFis

I see your point, but I'm unsure which kind of finer graded error-messages this would provide by just having a callback called instead of an exception.

The schema is so free in what can be done with it. There is no schema-syntax check when setting the root-schema, wrong fields will silently be ignored (and maybe treated as unknown keywords).

pboettch avatar Dec 01 '21 16:12 pboettch

That is great, but I ran into the issue when I referenced to an non existent ref. object. Aka, it throws in case of #/bad/ref. So it seems only maybe for this specific use case it makes sense.

BitFis avatar Dec 01 '21 19:12 BitFis

What would you think if dedicated exceptions would be thrown for the errors? I'd prefer actually.

pboettch avatar Dec 01 '21 22:12 pboettch

And the exception would be extended with a json pointer containing location of the bad reference and the bad reference? That would work.

BitFis avatar Dec 02 '21 08:12 BitFis

I know this is possible in Python, but is this doable in C++? Customizing exceptions arguments? I'd be happy to have a look if you'd have some time to try out something that fits your needs. Also see this issue, which would be then related:

#37

pboettch avatar Dec 02 '21 08:12 pboettch

Well, I am not the biggest expert, but as I would do it is extend std::exception or probably better extend json::exception and overwrite const char* what(). In nlohmann::json it seems it's done a bit different, I would check how they did it and reuse as much (https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/exceptions.hpp)

BitFis avatar Dec 02 '21 08:12 BitFis

Sounds good. What would be a estimated time of arrival if you could get your hands on it?

pboettch avatar Dec 10 '21 15:12 pboettch

Hi, It would be very usefull to be able to turn off exceptions for when they are not supported: https://json.nlohmann.me/features/parsing/parse_exceptions/#switch-off-exceptions

Otherwise any issues parsing a root schema will throw an exception.

Thanks

miguelxmiguel avatar Jan 20 '22 10:01 miguelxmiguel