json-schema 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,7 @@ From the git repo:
18
18
 
19
19
  <pre>
20
20
  $ gem build json-schema.gemspec
21
- $ gem install json-schema-1.0.10.gem
21
+ $ gem install json-schema-1.0.12.gem
22
22
  </pre>
23
23
 
24
24
 
@@ -161,6 +161,40 @@ data = {
161
161
  JSON::Validator.validate(schema, data, :validate_schema => true)
162
162
  </pre>
163
163
 
164
+ h3. Validate a JSON object against a JSON schema object, while inserting default values from the schema
165
+
166
+ With the :insert_defaults option set to true any missing property that has a
167
+ default value specified in the schema will be inserted into the validated data. The inserted default value is validated hence catching a schema that specifies an invalid default value.
168
+
169
+ <pre>
170
+ require 'rubygems'
171
+ require 'json-schema'
172
+
173
+ schema = {
174
+ "type" => "object",
175
+ "properties" => {
176
+ "a" => {"type" => "integer", "default" => 42, "required" => "true"},
177
+ "b" => {"type" => "integer"}
178
+ }
179
+ }
180
+
181
+ # Would not normally validate because "a" is missing and required by schema,
182
+ # but "default" option allows insertion of valid default.
183
+ data = {
184
+ "b" => 5
185
+ }
186
+
187
+ JSON::Validator.validate(schema, data)
188
+ # false
189
+
190
+ JSON::Validator.validate(schema, data, :insert_defaults => true)
191
+ # true
192
+ # data = {
193
+ # "a" => 42,
194
+ # "b" => 5
195
+ # }
196
+
197
+ </pre>
164
198
  h3. Validate an object against a JSON Schema Draft 2 schema
165
199
 
166
200
  <pre>
@@ -4,7 +4,12 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['properties'].each do |property,property_schema|
7
- if (property_schema['required'] && !data.has_key?(property))
7
+ if !data.has_key?(property) and property_schema['default'] and !property_schema['readonly'] and options[:insert_defaults]
8
+ default = property_schema['default']
9
+ data[property] = (default.is_a?(Hash) ? default.clone : default)
10
+ end
11
+
12
+ if property_schema['required'] and !data.has_key?(property)
8
13
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
9
14
  validation_error(message, fragments, current_schema, self, options[:record_errors])
10
15
  end
@@ -20,4 +25,4 @@ module JSON
20
25
  end
21
26
  end
22
27
  end
23
- end
28
+ end
@@ -109,7 +109,8 @@ module JSON
109
109
  :version => nil,
110
110
  :validate_schema => false,
111
111
  :record_errors => false,
112
- :errors_as_objects => false
112
+ :errors_as_objects => false,
113
+ :insert_defaults => false
113
114
  }
114
115
  @@validators = {}
115
116
  @@default_validator = nil
@@ -150,6 +151,7 @@ module JSON
150
151
  end
151
152
 
152
153
  @validation_options = @options[:record_errors] ? {:record_errors => true} : {}
154
+ @validation_options[:insert_defaults] = true if @options[:insert_defaults]
153
155
 
154
156
  # validate the schema, if requested
155
157
  if @options[:validate_schema]
@@ -1012,6 +1012,74 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1012
1012
  data = {"a" => 1, "b" => 2, "c" => 3}
1013
1013
  assert(JSON::Validator.validate(schema,data))
1014
1014
  end
1015
+
1016
+ def test_default
1017
+ schema = {
1018
+ "type" => "object",
1019
+ "properties" => {
1020
+ "a" => {"type" => "integer", "default" => 42},
1021
+ "b" => {"type" => "integer"}
1022
+ }
1023
+ }
1024
+
1025
+ data = {"b" => 2}
1026
+ assert(JSON::Validator.validate(schema,data))
1027
+ assert_nil(data["a"])
1028
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1029
+ assert_equal(42, data["a"])
1030
+
1031
+ schema = {
1032
+ "type" => "object",
1033
+ "properties" => {
1034
+ "a" => {"type" => "integer", "default" => 42, "required" => true},
1035
+ "b" => {"type" => "integer"}
1036
+ }
1037
+ }
1038
+
1039
+ data = {"b" => 2}
1040
+ assert(!JSON::Validator.validate(schema,data))
1041
+ assert_nil(data["a"])
1042
+ assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1043
+ assert_equal(42, data["a"])
1044
+
1045
+ schema = {
1046
+ "type" => "object",
1047
+ "properties" => {
1048
+ "a" => {"type" => "integer", "default" => 42, "required" => true},
1049
+ "b" => {"type" => "integer"}
1050
+ }
1051
+ }
1052
+
1053
+
1054
+ schema = {
1055
+ "type" => "object",
1056
+ "properties" => {
1057
+ "a" => {"type" => "integer", "default" => 42, "required" => true, "readonly" => true},
1058
+ "b" => {"type" => "integer"}
1059
+ }
1060
+ }
1061
+
1062
+ data = {"b" => 2}
1063
+ assert(!JSON::Validator.validate(schema,data))
1064
+ assert_nil(data["a"])
1065
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1066
+ assert_nil(data["a"])
1067
+
1068
+ schema = {
1069
+ "type" => "object",
1070
+ "properties" => {
1071
+ "a" => {"type" => "integer", "default" => "42"},
1072
+ "b" => {"type" => "integer"}
1073
+ }
1074
+ }
1075
+
1076
+ data = {"b" => 2}
1077
+ assert(JSON::Validator.validate(schema,data))
1078
+ assert_nil(data["a"])
1079
+ assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1080
+ assert_equal("42",data["a"])
1081
+
1082
+ end
1015
1083
 
1016
1084
 
1017
1085
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.11
4
+ version: 1.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: