From 5cd9e7f862dc24d8aaaf372546e7a99e84208fd8 Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Sun, 26 Oct 2014 13:38:11 +0100 Subject: [PATCH 1/3] Add description for custom format usage --- README.textile | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.textile b/README.textile index e76f3ba1..e2d60b5a 100644 --- a/README.textile +++ b/README.textile @@ -329,6 +329,41 @@ data = {"a" => 0, "b" => "taco"} JSON::Validator.validate(schema,data) # => false +h3. Custom format validation + +The JSON schema standard allows custom formats in schema definitions which should be ignored by validators that do not support them. JSON::Schema allows registering procs as custom format validators which receive the value to be checked as parameter and must raise a JSON::Schema::CustomFormatError to indicate a format violation: + +
+require 'rubygems'
+require 'json-schema'
+
+format_proc = -> value { raise JSON::Schema::CustomFormatError.new("must be 42"j) unless value == "42" }
+
+# register the proc for format 'the-answer' for draft4 schema
+JSON::Validator.register_format_validator("the-answer", @format_proc, ["draft4"])
+# omitting the version parameter uses ["draft1", "draft2", "draft3", "draft4"] as default
+JSON::Validator.register_format_validator("the-answer", @format_proc)
+
+# to deregister the custom validator call (also ["draft1", "draft2", "draft3", "draft4"] as default)
+JSON::Validator.deregister_format_validator('the-answer', ["draft4"])
+
+# there is a shortcut to restore the default formats for validators (same default as before)
+JSON::Validator.restore_default_formats(["draft4"])
+
+# Having registered the validator as above, the following will result in ["The property '#a' must be 42"] as returned errors
+schema = {
+  "$schema" => "http://json-schema.org/draft-04/schema#",
+  "properties" => {
+    "a" => {
+      "type" => "string",
+      "format" => "the-answer",
+    }
+  }
+}
+errors = JSON::Validator.fully_validate(schema, {"a" => "23"})
+
+
+ h2. JSON Backends The JSON Schema library currently supports the json and yajl-ruby backend JSON parsers. If either of these libraries are installed, they will be automatically loaded and used to parse any JSON strings supplied by the user. From 0aee368f6744c3f4a3f8f4e5d6c56cc30ecc7e7e Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Sun, 26 Oct 2014 13:38:21 +0100 Subject: [PATCH 2/3] Update format list --- README.textile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.textile b/README.textile index e2d60b5a..f57e3c2e 100644 --- a/README.textile +++ b/README.textile @@ -383,8 +383,10 @@ The 'format' attribute is only validated for the following values: * date-time * date * time -* ip-address +* ip-address (IPv4 address in draft1, draft2 and draft3) +* ipv4 (IPv4 address in draft4) * ipv6 +* uri All other 'format' attribute values are simply checked to ensure the instance value is of the correct datatype (e.g., an instance value is validated to be an integer or a float in the case of 'utc-millisec'). From 512b409df4dac69741c134482519015de209258f Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Sun, 26 Oct 2014 13:47:22 +0100 Subject: [PATCH 3/3] Improve description --- README.textile | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/README.textile b/README.textile index f57e3c2e..b5470bb3 100644 --- a/README.textile +++ b/README.textile @@ -331,26 +331,31 @@ JSON::Validator.validate(schema,data) # => false h3. Custom format validation -The JSON schema standard allows custom formats in schema definitions which should be ignored by validators that do not support them. JSON::Schema allows registering procs as custom format validators which receive the value to be checked as parameter and must raise a JSON::Schema::CustomFormatError to indicate a format violation: +The JSON schema standard allows custom formats in schema definitions which should be ignored by validators that do not support them. JSON::Schema allows registering procs as custom format validators which receive the value to be checked as parameter and must raise a JSON::Schema::CustomFormatError to indicate a format violation. The error message will be prepended by the property namne, e.g. "The property '#a'":
 require 'rubygems'
 require 'json-schema'
 
-format_proc = -> value { raise JSON::Schema::CustomFormatError.new("must be 42"j) unless value == "42" }
+format_proc = -> value {
+  raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42"
+}
 
 # register the proc for format 'the-answer' for draft4 schema
-JSON::Validator.register_format_validator("the-answer", @format_proc, ["draft4"])
+JSON::Validator.register_format_validator("the-answer", format_proc, ["draft4"])
+
 # omitting the version parameter uses ["draft1", "draft2", "draft3", "draft4"] as default
-JSON::Validator.register_format_validator("the-answer", @format_proc)
+JSON::Validator.register_format_validator("the-answer", format_proc)
 
-# to deregister the custom validator call (also ["draft1", "draft2", "draft3", "draft4"] as default)
+# deregistering the custom validator
+# (also ["draft1", "draft2", "draft3", "draft4"] as default version)
 JSON::Validator.deregister_format_validator('the-answer', ["draft4"])
 
-# there is a shortcut to restore the default formats for validators (same default as before)
+# shortcut to restore the default formats for validators (same default as before)
 JSON::Validator.restore_default_formats(["draft4"])
 
-# Having registered the validator as above, the following will result in ["The property '#a' must be 42"] as returned errors
+# with the validator registered as above, the following results in
+# ["The property '#a' must be 42"] as returned errors
 schema = {
   "$schema" => "http://json-schema.org/draft-04/schema#",
   "properties" => {