MQTTSuite
Loading...
Searching...
No Matches
nlohmann::json_schema Namespace Reference

Classes

class  root_schema
class  error_handler
class  basic_error_handler
class  json_validator

Typedefs

typedef std::function< void(const json_uri &, json &)> schema_loader
typedef std::function< void(const std::string &, const std::string &)> format_checker
typedef std::function< void(const std::string &, const std::string &, const json &)> content_checker

Functions

void default_string_format_check (const std::string &format, const std::string &value)

Variables

json draft7_schema_builtin

Typedef Documentation

◆ content_checker

typedef std::function<void(const std::string & , const std::string & , const json & )> nlohmann::json_schema::content_checker

Definition at line 135 of file json-schema.hpp.

◆ format_checker

typedef std::function<void(const std::string & , const std::string & )> nlohmann::json_schema::format_checker

Definition at line 134 of file json-schema.hpp.

◆ schema_loader

typedef std::function<void(const json_uri & , json & )> nlohmann::json_schema::schema_loader

Definition at line 133 of file json-schema.hpp.

Function Documentation

◆ default_string_format_check()

void nlohmann::json_schema::default_string_format_check ( const std::string & format,
const std::string & value )

Checks validity for built-ins by converting the definitions given as ABNF in the linked RFC from

See also
https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats into regular expressions using
https://www.msweet.org/abnf/ and some manual editing.
https://json-schema.org/latest/json-schema-validation.html

Checks validity of JSON schema built-in string format specifiers like 'date-time', 'ipv4', ...

Definition at line 353 of file string-format-check.cpp.

354{
355 if (format == "date-time") {
356 rfc3339_date_time_check(value);
357 } else if (format == "date") {
358 rfc3339_date_check(value);
359 } else if (format == "time") {
360 rfc3339_time_check(value);
361 } else if (format == "uri") {
362 rfc3986_uri_check(value);
363 } else if (format == "email") {
364 if (!is_ascii(value)) {
365 throw std::invalid_argument(value + " contains non-ASCII values, not RFC 5321 compliant.");
366 }
367 if (!is_address(&*value.begin(), &*value.end())) {
368 throw std::invalid_argument(value + " is not a valid email according to RFC 5321.");
369 }
370 } else if (format == "idn-email") {
371 if (!is_address(&*value.begin(), &*value.end())) {
372 throw std::invalid_argument(value + " is not a valid idn-email according to RFC 6531.");
373 }
374 } else if (format == "hostname") {
375 static const REGEX_NAMESPACE::regex hostRegex{hostname};
376 if (!REGEX_NAMESPACE::regex_match(value, hostRegex)) {
377 throw std::invalid_argument(value + " is not a valid hostname according to RFC 3986 Appendix A.");
378 }
379 } else if (format == "ipv4") {
380 const static REGEX_NAMESPACE::regex ipv4Regex{"^" + ipv4Address + "$"};
381 if (!REGEX_NAMESPACE::regex_match(value, ipv4Regex)) {
382 throw std::invalid_argument(value + " is not an IPv4 string according to RFC 2673.");
383 }
384 } else if (format == "ipv6") {
385 static const REGEX_NAMESPACE::regex ipv6Regex{ipv6Address};
386 if (!REGEX_NAMESPACE::regex_match(value, ipv6Regex)) {
387 throw std::invalid_argument(value + " is not an IPv6 string according to RFC 5954.");
388 }
389 } else if (format == "uuid") {
390 static const REGEX_NAMESPACE::regex uuidRegex{uuid};
391 if (!REGEX_NAMESPACE::regex_match(value, uuidRegex)) {
392 throw std::invalid_argument(value + " is not an uuid string according to RFC 4122.");
393 }
394 } else if (format == "regex") {
395 try {
396 REGEX_NAMESPACE::regex re(value, std::regex::ECMAScript);
397 } catch (std::exception &exception) {
398 throw exception;
399 }
400 } else {
401 /* yet unsupported JSON schema draft 7 built-ins */
402 static const std::vector<std::string> jsonSchemaStringFormatBuiltIns{
403 "date-time", "time", "date", "email", "idn-email", "hostname", "idn-hostname", "ipv4", "ipv6", "uri",
404 "uri-reference", "iri", "iri-reference", "uri-template", "json-pointer", "relative-json-pointer", "regex"};
405 if (std::find(jsonSchemaStringFormatBuiltIns.begin(), jsonSchemaStringFormatBuiltIns.end(), format) != jsonSchemaStringFormatBuiltIns.end()) {
406 throw std::logic_error("JSON schema string format built-in " + format + " not yet supported. " +
407 "Please open an issue or use a custom format checker.");
408 }
409
410 throw std::logic_error("Don't know how to validate " + format);
411 }
412}
bool is_address(const char *p, const char *pe)

References anonymous_namespace{string-format-check.cpp}::hostname, anonymous_namespace{string-format-check.cpp}::ipv4Address, anonymous_namespace{string-format-check.cpp}::ipv6Address, is_address(), anonymous_namespace{string-format-check.cpp}::is_ascii(), anonymous_namespace{string-format-check.cpp}::rfc3339_date_check(), anonymous_namespace{string-format-check.cpp}::rfc3339_date_time_check(), anonymous_namespace{string-format-check.cpp}::rfc3339_time_check(), anonymous_namespace{string-format-check.cpp}::rfc3986_uri_check(), and anonymous_namespace{string-format-check.cpp}::uuid.

Referenced by main(), main(), and testStringFormat().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ draft7_schema_builtin

json nlohmann::json_schema::draft7_schema_builtin

Definition at line 16 of file json-schema-draft7.json.cpp.

Referenced by loader().