I overloaded the error handler much like the example here
class custom_error_handler : public nlohmann::json_schema::basic_error_handler
{
void error(const nlohmann::json_pointer<nlohmann::basic_json<>> &pointer, const json &instance,
const std::string &message) override
{
nlohmann::json_schema::basic_error_handler::error(pointer, instance, message);
std::cerr << "ERROR: '" << pointer << "' - '" << instance << "': " << message << "\n";
errorVector.push_back(message);
}
std::vector<std::string> errorVector;
};
I push the message to a vector inside custom_error_handler and can extract some info from the message by string processing. But given that there can be several kinds of errors, it's not feasible to string process message for each of them.
I understand this lib is for debugging only, but is there anyway to provide an "error type" (perhaps as an enum) in the error? That way the calling function can know what the error was (required missing, min length. max length etc.) and we can extract the property from the error message if needed (although it would be super cool to just get that as well0. I think that would take this lib to the next level.
Sample code
auto path = ptr.to_string();
std::string property;
if (message.find("required property") != std::string::npos)
{
// For missing required properties, extract property name from the error message
// Format: "required property 'propertyName' not found"
size_t start = message.find("'") + 1;
size_t end = message.find("'", start);
if (start != std::string::npos && end != std::string::npos)
{
property = message.substr(start, end - start);
}
}
else
{
// Extract the property name from the path for other errors
std::string path = ptr.to_string();
size_t last_slash = path.find_last_of('/');
if (last_slash != std::string::npos)
{
property = path.substr(last_slash + 1);
}
else if (!path.empty())
{
property = path;
}
else
{
property = "";
}
}
This code can extract the missing property, or a property that failed the schema due to whatever reason. But in the latter case it's nearly impossible to know what the failure was unless I reverse engineer all the possible error strings that the validation can generate, then handle each of them. Getting the "property" that failed, and a "failure reason" will help the caller of the validation immediately know where the json is wrong. This will allow the caller to perform more granular error handling.
I overloaded the error handler much like the example here
I push the message to a vector inside custom_error_handler and can extract some info from the message by string processing. But given that there can be several kinds of errors, it's not feasible to string process
messagefor each of them.I understand this lib is for debugging only, but is there anyway to provide an "error type" (perhaps as an enum) in the error? That way the calling function can know what the error was (required missing, min length. max length etc.) and we can extract the property from the error message if needed (although it would be super cool to just get that as well0. I think that would take this lib to the next level.
Sample code
This code can extract the missing property, or a property that failed the schema due to whatever reason. But in the latter case it's nearly impossible to know what the failure was unless I reverse engineer all the possible error strings that the validation can generate, then handle each of them. Getting the "property" that failed, and a "failure reason" will help the caller of the validation immediately know where the json is wrong. This will allow the caller to perform more granular error handling.