json_schema 0.7.1 → 0.8.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.
- checksums.yaml +4 -4
- data/lib/json_schema/validator.rb +21 -1
- data/test/json_schema/validator_test.rb +30 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1033f10897ed2983a37fcaec049ced9da6348fa
|
4
|
+
data.tar.gz: f02430273ad024f1fca2de84555d0d922b9c193e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b65e70751cce8c9526237494fd5e2996a75c36759a81533bd377329ada10949f23d05d372f03e4d8045b53658181e0faf33a6760f1728e1d9cf3f9df3dcacd57
|
7
|
+
data.tar.gz: d71350a50a87d62f09d8ea4665c912597d3ed3c723bc62248c468ad238eb53b8748c4f52754b98ff1050845e176b1e0e104eaaabbe1bec12326dbe75bcf0b0f3
|
@@ -506,7 +506,8 @@ module JsonSchema
|
|
506
506
|
if valid_types.any? { |t| data.is_a?(t) }
|
507
507
|
true
|
508
508
|
else
|
509
|
-
|
509
|
+
key = find_parent(schema)
|
510
|
+
message = %{For '#{key}', #{data.inspect} is not #{ErrorFormatter.to_list(schema.type)}.}
|
510
511
|
errors << ValidationError.new(schema, path, message, :invalid_type)
|
511
512
|
false
|
512
513
|
end
|
@@ -523,6 +524,25 @@ module JsonSchema
|
|
523
524
|
end
|
524
525
|
end
|
525
526
|
|
527
|
+
def find_parent(schema)
|
528
|
+
fragment = schema.fragment
|
529
|
+
key = if fragment =~ /patternProperties/
|
530
|
+
split_pointer = schema.pointer.split("/")
|
531
|
+
idx = split_pointer.index("patternProperties")
|
532
|
+
|
533
|
+
# this join mimics the fragment format below in that it's
|
534
|
+
# parent + key
|
535
|
+
if idx - 2 >= 0
|
536
|
+
parts = split_pointer[(idx - 2)..(idx - 1)]
|
537
|
+
end
|
538
|
+
|
539
|
+
# protect against a `nil` that could occur if
|
540
|
+
# `patternProperties` has no parent
|
541
|
+
parts ? parts.compact.join("/") : nil
|
542
|
+
end
|
543
|
+
key || fragment
|
544
|
+
end
|
545
|
+
|
526
546
|
EMAIL_PATTERN = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+$/i
|
527
547
|
|
528
548
|
HOSTNAME_PATTERN = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/
|
@@ -40,7 +40,7 @@ describe JsonSchema::Validator do
|
|
40
40
|
)
|
41
41
|
@data_sample = 4
|
42
42
|
refute validate
|
43
|
-
assert_includes error_messages, %{4 is not an object.}
|
43
|
+
assert_includes error_messages, %{For 'definitions/app', 4 is not an object.}
|
44
44
|
assert_includes error_types, :invalid_type
|
45
45
|
end
|
46
46
|
|
@@ -50,7 +50,7 @@ describe JsonSchema::Validator do
|
|
50
50
|
)
|
51
51
|
@data_sample = 4
|
52
52
|
refute validate
|
53
|
-
assert_includes error_messages, %{4 is not a string.}
|
53
|
+
assert_includes error_messages, %{For 'definitions/app', 4 is not a string.}
|
54
54
|
assert_includes error_types, :invalid_type
|
55
55
|
|
56
56
|
pointer("#/definitions/app").merge!(
|
@@ -58,7 +58,7 @@ describe JsonSchema::Validator do
|
|
58
58
|
)
|
59
59
|
@data_sample = 4
|
60
60
|
refute validate
|
61
|
-
assert_includes error_messages, %{4 is not a string or null.}
|
61
|
+
assert_includes error_messages, %{For 'definitions/app', 4 is not a string or null.}
|
62
62
|
assert_includes error_types, :invalid_type
|
63
63
|
|
64
64
|
pointer("#/definitions/app").merge!(
|
@@ -66,7 +66,7 @@ describe JsonSchema::Validator do
|
|
66
66
|
)
|
67
67
|
@data_sample = 4
|
68
68
|
refute validate
|
69
|
-
assert_includes error_messages, %{4 is not an object, null, or string.}
|
69
|
+
assert_includes error_messages, %{For 'definitions/app', 4 is not an object, null, or string.}
|
70
70
|
assert_includes error_types, :invalid_type
|
71
71
|
end
|
72
72
|
|
@@ -382,7 +382,7 @@ describe JsonSchema::Validator do
|
|
382
382
|
data_sample["foo"] = 4
|
383
383
|
data_sample["matches_pattern"] = "yes!"
|
384
384
|
refute validate
|
385
|
-
assert_includes error_messages, %{4 is not a boolean.}
|
385
|
+
assert_includes error_messages, %{For 'additionalProperties', 4 is not a boolean.}
|
386
386
|
assert_includes error_types, :invalid_type
|
387
387
|
end
|
388
388
|
|
@@ -446,7 +446,15 @@ describe JsonSchema::Validator do
|
|
446
446
|
"KEY" => 456
|
447
447
|
}
|
448
448
|
refute validate
|
449
|
-
assert_includes error_messages, %{456 is not a null or string.}
|
449
|
+
assert_includes error_messages, %{For 'definitions/config_vars', 456 is not a null or string.}
|
450
|
+
assert_includes error_types, :invalid_type
|
451
|
+
end
|
452
|
+
|
453
|
+
it "validates patternProperties with missing parent" do
|
454
|
+
data_sample["S_0"] = 123
|
455
|
+
|
456
|
+
refute validate_parentless_pattern
|
457
|
+
assert_includes error_messages, %{For 'patternProperties/^S_', 123 is not a string.}
|
450
458
|
assert_includes error_types, :invalid_type
|
451
459
|
end
|
452
460
|
|
@@ -802,6 +810,22 @@ describe JsonSchema::Validator do
|
|
802
810
|
JsonPointer.evaluate(schema_sample, path)
|
803
811
|
end
|
804
812
|
|
813
|
+
def validate_parentless_pattern
|
814
|
+
schema = {
|
815
|
+
"$schema" => "http://json-schema.org/draft-04/hyper-schema",
|
816
|
+
"patternProperties" => {
|
817
|
+
"^S_" => {
|
818
|
+
"type" => [
|
819
|
+
"string"
|
820
|
+
]
|
821
|
+
}
|
822
|
+
}
|
823
|
+
}
|
824
|
+
schema = JsonSchema.parse!(schema)
|
825
|
+
@validator = JsonSchema::Validator.new(schema)
|
826
|
+
@validator.validate(data_sample)
|
827
|
+
end
|
828
|
+
|
805
829
|
def schema_sample
|
806
830
|
@schema_sample ||= DataScaffold.schema_sample
|
807
831
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecma-re-validator
|