8000 GitHub - lyobzik/jsvor: JsonSchemaValidator · GitHub
[go: up one dir, main page]

Skip to content
< 7FFF script type="application/json" data-target="react-partial.embeddedData">{"props":{"color_mode":"dark","logged_in":false,"marketing_page":false,"home_path":"/","login_path":"/login?return_to=https%3A%2F%2FGitHub.com%2Flyobzik%2Fjsvor","signup_path":"/signup?ref_cta=Sign+up\u0026ref_loc=header+logged+out\u0026ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E\u0026source=header-repo\u0026source_repo=lyobzik%2Fjsvor","signup_enabled":true,"is_signup_controller":false,"show_search_and_nav":true,"hide_search":false,"private_mode_enabled":false,"should_use_dotcom_links":true,"auth_hydro_click":"{\"event_type\":\"authentication.click\",\"payload\":{\"location_in_page\":\"site header menu\",\"repository_id\":null,\"auth_type\":\"SIGN_UP\",\"originating_url\":\"https://GitHub.com/lyobzik/jsvor\",\"user_id\":null}}","auth_hydro_click_hmac":"d29fddd0a304beeff084c6f87c484b00af87cf45fe51a5f20dd0f1dbb90292dd"}}
lyobzikPublic

About

JsonSchemaValidator

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

JsonSchemaValidator

Simple validator of json, based on Json-Schema (draft03).

Usage

#include <iostream>

#include <JsonSchema.h>
#include <JsonErrors.h>
#include <JsonDefs.h>

namespace jsvor = JsonSchemaValidator;

void Print(std::string const &context, std::string const &value,
           jsvor::ValidationResult const &result);

int main(int /*argc*/, char * /*argv*/[]) {
	using namespace jsvor;

	// JsonSchema can be created from text of json-document (exception 'IncorrectJson' will be
	// thrown on error of parsing this text) or from already parsed json-document (in this case
	// lifetime of json-document must be longer than lifetime of 'JsonSchema').
	// Exception 'IncorrectSchema' will be thrown if some part of document has not satisfied
	// to restrictions on JsonSchema.
	JsonSchema schema(R"({"properties": {"data": {
                             "type": "integer", "minimum": 10, "maximum": 100, "divisibleBy": 2}
                         } })");

	for (auto const &value : { R"({"data": 0})", R"({"data": 20})",
	                           R"({"data": 21})", R"({"data": 101})" }) {
		// To validate json-document, you can call the method 'Validate' and then handle outgoing
		// parameter 'result'. From this parameter, you can get overall result of validation,
		// error code and detailed error description.
		ValidationResult result;
		schema.Validate(value, result);
		Print("Validate(1)", value, result);

		// Also you can use version of method 'Validate' with exception. On error this method
		// throws exception 'IncorrectDocument' from which you can get validation error.
		try {
			schema.Validate(value);
		}
		catch (IncorrectDocument const &error) {
			Print("Validate(2)", value, error.GetResult());
		}
	}
}

void Print(std::string const &context, std::string const &value,
           jsvor::ValidationResult const &result) {
	auto errorDescription = (result ? "None." : result.ErrorDescription());
	std::cout << std::boolalpha << context << " <" << value << ">:("
		<< static_cast<bool>(result) << ") " << errorDescription << std::noboolalpha << std::endl;
}

Output:

Validate(1) <{"data": 0}>:(false) Attribute /data must be >=10.
Validate(2) <{"data": 0}>:(false) Attribute /data must be >=10.
Validate(1) <{"data": 20}>:(true) None.
Validate(1) <{"data": 21}>:(false) Element /data must be divisible by 2.
Validate(2) <{"data": 21}>:(false) Element /data must be divisible by 2.
Validate(1) <{"data": 101}>:(false) Attribute /data must be <=100.
Validate(2) <{"data": 101}>:(false) Attribute /data must be <=100.

Dependencies

  • RapidJSON used for parsing json (included).
  • JSON-Schema-Test-Suite used for tests (included).
  • re2 used by default for working with regex. If you define USE_STD_REGEX for working with regex will be used std::regex.
  • WJElement used in project perftests for comparing of working speed.
  • cmake and qmake are used for build.

About

JsonSchemaValidator

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0