pboettch / json-schema-validator

JSON schema validator for JSON for Modern C++
Other
511 stars 143 forks source link

Add format checkers to an existing validator. #329

Open zaun opened 1 month ago

zaun commented 1 month ago

Something like:

validator.set_format_check("url", [](const std::string &value) {
    const std::regex uri_regex(R"(^https?:\/\/[^\s]+$)");
    if (!std::regex_match(value, uri_regex)) {
        throw std::invalid_argument("Invalid URI format: " + value);
    }
});

Would allow easy adding of format checkers to an existing validator. It also, at least in my opinion, is easier to read than a monolithic format checker. A setup like this would allow the default checkers to be added automatically, but still let the user overwrite them with their own if they want.

pboettch commented 1 month ago

Nice idea. Are you able to develop this feature?

zaun commented 1 month ago

I can look into it.

Would you still want the ability to have the monolithic format checker? I would recommend removing it for simplicity, but that would be a breaking change to the API.