json_schema 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,6 +32,19 @@ module JsonSchema
32
32
 
33
33
  private
34
34
 
35
+ # for use with additionalProperties and strictProperties
36
+ def get_extra_keys(schema, data)
37
+ extra = data.keys - schema.properties.keys
38
+
39
+ if schema.pattern_properties
40
+ schema.pattern_properties.keys.each do |pattern|
41
+ extra -= extra.select { |k| k =~ pattern }
42
+ end
43
+ end
44
+
45
+ extra
46
+ end
47
+
35
48
  # works around &&'s "lazy" behavior
36
49
  def strict_and(valid_old, valid_new)
37
50
  valid_old && valid_new
@@ -89,29 +102,16 @@ module JsonSchema
89
102
  def validate_additional_properties(schema, data, errors, path)
90
103
  return true if schema.additional_properties == true
91
104
 
92
- extra = data.keys - schema.properties.keys
93
-
94
- if schema.pattern_properties
95
- schema.pattern_properties.keys.each do |pattern|
96
- extra -= extra.select { |k| k =~ pattern }
97
- end
98
- end
99
-
100
105
  # schema indicates that all properties not in `properties` should be
101
106
  # validated according to subschema
102
107
  if schema.additional_properties.is_a?(Schema)
108
+ extra = get_extra_keys(schema, data)
103
109
  extra.each do |key|
104
110
  validate_data(schema.additional_properties, data[key], errors, path + [key])
105
111
  end
106
112
  # boolean indicates whether additional properties are allowed
107
113
  else
108
- if extra.empty?
109
- true
110
- else
111
- message = %{Extra keys in object: #{extra.sort.join(", ")}.}
112
- errors << ValidationError.new(schema, path, message)
113
- false
114
- end
114
+ validate_extra(schema, data, errors, path)
115
115
  end
116
116
  end
117
117
 
@@ -191,6 +191,17 @@ module JsonSchema
191
191
  end
192
192
  end
193
193
 
194
+ def validate_extra(schema, data, errors, path)
195
+ extra = get_extra_keys(schema, data)
196
+ if extra.empty?
197
+ true
198
+ else
199
+ message = %{Extra keys in object: #{extra.sort.join(", ")}.}
200
+ errors << ValidationError.new(schema, path, message)
201
+ false
202
+ end
203
+ end
204
+
194
205
  def validate_items(schema, data, errors, path)
195
206
  return true unless schema.items
196
207
  if schema.items.is_a?(Array)
@@ -398,14 +409,8 @@ module JsonSchema
398
409
  def validate_strict_properties(schema, data, errors, path)
399
410
  return true if !schema.strict_properties
400
411
 
401
- missing = schema.properties.keys - data.keys
402
- if missing.empty?
403
- true
404
- else
405
- message = %{Missing required keys "#{missing.sort.join(", ")}" in object; keys are "#{data.keys.sort.join(", ")}".}
406
- errors << ValidationError.new(schema, path, message)
407
- false
408
- end
412
+ strict_and validate_extra(schema, data, errors, path),
413
+ validate_required(schema, data, errors, path, schema.properties.keys)
409
414
  end
410
415
 
411
416
  def validate_type(schema, data, errors, path)
@@ -307,7 +307,7 @@ describe JsonSchema::Validator do
307
307
  }
308
308
  )
309
309
  data_sample["foo"] = "bar"
310
- data_sample["matches-pattern"] = "yes!"
310
+ data_sample["matches_pattern"] = "yes!"
311
311
  refute validate
312
312
  assert_includes error_messages, %{Extra keys in object: foo.}
313
313
  end
@@ -332,7 +332,7 @@ describe JsonSchema::Validator do
332
332
  }
333
333
  )
334
334
  data_sample["foo"] = 4
335
- data_sample["matches-pattern"] = "yes!"
335
+ data_sample["matches_pattern"] = "yes!"
336
336
  refute validate
337
337
  assert_includes error_messages,
338
338
  %{Expected data to be of type "boolean"; value was: 4.}
@@ -418,12 +418,18 @@ describe JsonSchema::Validator do
418
418
 
419
419
  it "validates strictProperties unsuccessfully" do
420
420
  pointer("#/definitions/app").merge!(
421
+ "patternProperties" => {
422
+ "^matches" => {}
423
+ },
421
424
  "strictProperties" => true
422
425
  )
426
+ data_sample["extra_key"] = "value"
427
+ data_sample["matches_pattern"] = "yes!"
423
428
  refute validate
424
429
  missing = @schema.properties.keys.sort - ["name"]
425
430
  assert_includes error_messages,
426
- %{Missing required keys "#{missing.join(", ")}" in object; keys are "name".}
431
+ %{Missing required keys "#{missing.join(", ")}" in object; keys are "extra_key, matches_pattern, name".}
432
+ assert_includes error_messages, %{Extra keys in object: extra_key.}
427
433
  end
428
434
 
429
435
  it "validates allOf" do
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: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: