json_schema 0.0.20 → 0.1.0
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.
data/lib/json_schema/parser.rb
CHANGED
@@ -278,6 +278,7 @@ module JsonSchema
|
|
278
278
|
schema.pattern_properties = validate_type(schema, [Hash], "patternProperties") || {}
|
279
279
|
schema.properties = validate_type(schema, [Hash], "properties") || {}
|
280
280
|
schema.required = validate_type(schema, [Array], "required")
|
281
|
+
schema.strict_properties = validate_type(schema, BOOLEAN, "strictProperties")
|
281
282
|
|
282
283
|
# validation: string
|
283
284
|
schema.format = validate_type(schema, [String], "format")
|
data/lib/json_schema/schema.rb
CHANGED
@@ -153,6 +153,8 @@ module JsonSchema
|
|
153
153
|
attr_copyable :pattern_properties
|
154
154
|
attr_copyable :properties
|
155
155
|
attr_copyable :required
|
156
|
+
# warning: strictProperties is technically V5 spec (but I needed it now)
|
157
|
+
attr_copyable :strict_properties
|
156
158
|
|
157
159
|
# validation: string
|
158
160
|
attr_copyable :format
|
@@ -182,6 +184,7 @@ module JsonSchema
|
|
182
184
|
attr_reader_default :min_exclusive, false
|
183
185
|
attr_reader_default :pattern_properties, {}
|
184
186
|
attr_reader_default :properties, {}
|
187
|
+
attr_reader_default :strict_properties, false
|
185
188
|
attr_reader_default :type, []
|
186
189
|
|
187
190
|
# allow booleans to be access with question mark
|
@@ -221,8 +224,8 @@ module JsonSchema
|
|
221
224
|
def inspect_schema
|
222
225
|
if reference
|
223
226
|
str = reference.to_s
|
224
|
-
str += " [EXPANDED]"
|
225
|
-
str += " [
|
227
|
+
str += expanded? ? " [EXPANDED]" : " [COLLAPSED]"
|
228
|
+
str += original? ? " [ORIGINAL]" : " [CLONE]"
|
226
229
|
str
|
227
230
|
else
|
228
231
|
hash = {}
|
@@ -72,6 +72,7 @@ module JsonSchema
|
|
72
72
|
valid = strict_and valid, validate_pattern_properties(schema, data, errors, path)
|
73
73
|
valid = strict_and valid, validate_properties(schema, data, errors, path)
|
74
74
|
valid = strict_and valid, validate_required(schema, data, errors, path, schema.required)
|
75
|
+
valid = strict_and valid, validate_strict_properties(schema, data, errors, path)
|
75
76
|
end
|
76
77
|
|
77
78
|
# validation: string
|
@@ -388,6 +389,19 @@ module JsonSchema
|
|
388
389
|
end
|
389
390
|
end
|
390
391
|
|
392
|
+
def validate_strict_properties(schema, data, errors, path)
|
393
|
+
return true if !schema.strict_properties
|
394
|
+
|
395
|
+
missing = schema.properties.keys - data.keys
|
396
|
+
if missing.empty?
|
397
|
+
true
|
398
|
+
else
|
399
|
+
message = %{Missing required keys "#{missing.sort.join(", ")}" in object; keys are "#{data.keys.sort.join(", ")}".}
|
400
|
+
errors << ValidationError.new(schema, path, message)
|
401
|
+
false
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
391
405
|
def validate_type(schema, data, errors, path)
|
392
406
|
return true if schema.type.empty?
|
393
407
|
valid_types = schema.type.map { |t| TYPE_MAP[t] }.flatten.compact
|
@@ -122,6 +122,14 @@ describe JsonSchema::Parser do
|
|
122
122
|
assert_equal ["null", "string"], property[1].type
|
123
123
|
end
|
124
124
|
|
125
|
+
it "parses the strictProperties object validation" do
|
126
|
+
pointer("#/definitions/app").merge!(
|
127
|
+
"strictProperties" => true
|
128
|
+
)
|
129
|
+
schema = parse.definitions["app"]
|
130
|
+
assert_equal true, schema.strict_properties
|
131
|
+
end
|
132
|
+
|
125
133
|
# couldn't think of any non-contrived examples to work with here
|
126
134
|
it "parses the basic set of schema validations" do
|
127
135
|
schema = parse.definitions["app"].definitions["contrived"]
|
@@ -401,6 +401,23 @@ describe JsonSchema::Validator do
|
|
401
401
|
%{Missing required keys "name" in object; keys are "".}
|
402
402
|
end
|
403
403
|
|
404
|
+
it "validates strictProperties successfully" do
|
405
|
+
pointer("#/definitions/app").merge!(
|
406
|
+
"strictProperties" => false
|
407
|
+
)
|
408
|
+
assert validate
|
409
|
+
end
|
410
|
+
|
411
|
+
it "validates strictProperties unsuccessfully" do
|
412
|
+
pointer("#/definitions/app").merge!(
|
413
|
+
"strictProperties" => true
|
414
|
+
)
|
415
|
+
refute validate
|
416
|
+
missing = @schema.properties.keys.sort - ["name"]
|
417
|
+
assert_includes error_messages,
|
418
|
+
%{Missing required keys "#{missing.join(", ")}" in object; keys are "name".}
|
419
|
+
end
|
420
|
+
|
404
421
|
it "validates allOf" do
|
405
422
|
pointer("#/definitions/app/definitions/contrived").merge!(
|
406
423
|
"allOf" => [
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-05-
|
12
|
+
date: 2014-05-26 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|