json-schema 2.3.0 → 2.4.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 +8 -8
- data/README.textile +45 -2
- data/lib/json-schema.rb +1 -8
- data/lib/json-schema/attributes/dependencies_v4.rb +10 -5
- data/lib/json-schema/attributes/formats/custom.rb +0 -1
- data/lib/json-schema/attributes/formats/date.rb +4 -5
- data/lib/json-schema/attributes/formats/date_time.rb +6 -5
- data/lib/json-schema/attributes/formats/date_time_v4.rb +18 -0
- data/lib/json-schema/attributes/formats/ip4.rb +7 -9
- data/lib/json-schema/attributes/formats/ip6.rb +7 -17
- data/lib/json-schema/attributes/formats/time.rb +4 -4
- data/lib/json-schema/attributes/ref.rb +1 -0
- data/lib/json-schema/schema/validator.rb +7 -2
- data/lib/json-schema/{uri → util}/uuid.rb +0 -0
- data/lib/json-schema/validator.rb +41 -68
- data/lib/json-schema/validators/draft1.rb +1 -1
- data/lib/json-schema/validators/draft2.rb +1 -1
- data/lib/json-schema/validators/draft3.rb +1 -1
- data/lib/json-schema/validators/draft4.rb +2 -2
- data/lib/json-schema/validators/hyper-draft4.rb +14 -0
- data/test/test_common_test_suite.rb +13 -16
- data/test/test_custom_format.rb +1 -1
- data/test/test_fragment_validation_with_ref.rb +40 -0
- data/test/test_helper.rb +2 -0
- data/test/test_jsonschema_draft1.rb +10 -0
- data/test/test_jsonschema_draft2.rb +8 -0
- data/test/test_jsonschema_draft3.rb +8 -0
- data/test/test_jsonschema_draft4.rb +34 -8
- data/test/test_ruby_schema.rb +16 -16
- data/test/test_schema_validation.rb +11 -0
- metadata +11 -5
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjMyNzMwODM4YmUyNjNkZjBhNzkzN2E4M2MxYTM2YzhiMzg4MzZlYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZWY0NWQ1ZWRlMzVlNTI4N2NhYjk0ZGM4OGIxMzMzNzU5NGE2ZmEwYg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDE3Yzc0MmYwNmFkNjUzYmNiYTFhYjg4ODY5MzZjODgyZWIyODBkYWUwZjJh
|
10
|
+
NjJjODk4MGFhNTFlOTkyOTZkYTQ5ODQ2YjYxYjJjZGNjZWIxOTcxNzEyMmYy
|
11
|
+
NzI2MDcwNGU5Mzg0N2IyYzU5NzU0NThkMTNjZjFmZDVhZGNiNjc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDJjNzIyZDg0NDUxODQyZjM4MzZkNWUxOWViYTA3MDM4MjdmN2Q2ZjBkOTBi
|
14
|
+
NzllYzUyNDc5MDZkZmM0M2U3NmFiZDc5Njk3YTYxNzBiMjYwYjIwMjZmZWY0
|
15
|
+
M2QxMzIwN2EzOTUxNWRjZmQ5ZGQwZTEwNjBjMDVjZTBlNTAxZWE=
|
data/README.textile
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
!https://travis-ci.org/hoxworth/json-schema.svg?branch=master!:https://travis-ci.org/hoxworth/json-schema
|
2
|
+
!https://codeclimate.com/github/hoxworth/json-schema/badges/gpa.svg!:https://codeclimate.com/github/hoxworth/json-schema
|
2
3
|
|
3
4
|
h1. Ruby JSON Schema Validator
|
4
5
|
|
@@ -24,7 +25,7 @@ From the git repo:
|
|
24
25
|
|
25
26
|
<pre>
|
26
27
|
$ gem build json-schema.gemspec
|
27
|
-
$ gem install json-schema-2.
|
28
|
+
$ gem install json-schema-2.4.0.gem
|
28
29
|
</pre>
|
29
30
|
|
30
31
|
|
@@ -329,6 +330,46 @@ data = {"a" => 0, "b" => "taco"}
|
|
329
330
|
JSON::Validator.validate(schema,data) # => false
|
330
331
|
</pre>
|
331
332
|
|
333
|
+
h3. Custom format validation
|
334
|
+
|
335
|
+
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 <code>JSON::Schema::CustomFormatError</code> to indicate a format violation. The error message will be prepended by the property namne, e.g. "The property '#a'":
|
336
|
+
|
337
|
+
<pre>
|
338
|
+
require 'rubygems'
|
339
|
+
require 'json-schema'
|
340
|
+
|
341
|
+
format_proc = -> value {
|
342
|
+
raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42"
|
343
|
+
}
|
344
|
+
|
345
|
+
# register the proc for format 'the-answer' for draft4 schema
|
346
|
+
JSON::Validator.register_format_validator("the-answer", format_proc, ["draft4"])
|
347
|
+
|
348
|
+
# omitting the version parameter uses ["draft1", "draft2", "draft3", "draft4"] as default
|
349
|
+
JSON::Validator.register_format_validator("the-answer", format_proc)
|
350
|
+
|
351
|
+
# deregistering the custom validator
|
352
|
+
# (also ["draft1", "draft2", "draft3", "draft4"] as default version)
|
353
|
+
JSON::Validator.deregister_format_validator('the-answer', ["draft4"])
|
354
|
+
|
355
|
+
# shortcut to restore the default formats for validators (same default as before)
|
356
|
+
JSON::Validator.restore_default_formats(["draft4"])
|
357
|
+
|
358
|
+
# with the validator registered as above, the following results in
|
359
|
+
# ["The property '#a' must be 42"] as returned errors
|
360
|
+
schema = {
|
361
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
362
|
+
"properties" => {
|
363
|
+
"a" => {
|
364
|
+
"type" => "string",
|
365
|
+
"format" => "the-answer",
|
366
|
+
}
|
367
|
+
}
|
368
|
+
}
|
369
|
+
errors = JSON::Validator.fully_validate(schema, {"a" => "23"})
|
370
|
+
|
371
|
+
</pre>
|
372
|
+
|
332
373
|
h2. JSON Backends
|
333
374
|
|
334
375
|
The JSON Schema library currently supports the <code>json</code> and <code>yajl-ruby</code> 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.
|
@@ -348,8 +389,10 @@ The 'format' attribute is only validated for the following values:
|
|
348
389
|
* date-time
|
349
390
|
* date
|
350
391
|
* time
|
351
|
-
* ip-address
|
392
|
+
* ip-address (IPv4 address in draft1, draft2 and draft3)
|
393
|
+
* ipv4 (IPv4 address in draft4)
|
352
394
|
* ipv6
|
395
|
+
* uri
|
353
396
|
|
354
397
|
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').
|
355
398
|
|
data/lib/json-schema.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
if
|
4
|
-
Gem::Specification::find_by_name('multi_json')
|
5
|
-
rescue Gem::LoadError
|
6
|
-
false
|
7
|
-
rescue
|
8
|
-
Gem.available?('multi_json')
|
9
|
-
end
|
3
|
+
if Gem::Specification::find_all_by_name('multi_json').any?
|
10
4
|
require 'multi_json'
|
11
5
|
|
12
6
|
# Force MultiJson to load an engine before we define the JSON constant here; otherwise,
|
@@ -14,7 +8,6 @@ if begin
|
|
14
8
|
MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
|
15
9
|
end
|
16
10
|
|
17
|
-
require 'rubygems'
|
18
11
|
require 'json-schema/util/hash'
|
19
12
|
require 'json-schema/util/array_set'
|
20
13
|
require 'json-schema/schema'
|
@@ -6,12 +6,17 @@ module JSON
|
|
6
6
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
7
|
if data.is_a?(Hash)
|
8
8
|
current_schema.schema['dependencies'].each do |property,dependency_value|
|
9
|
-
if (data.has_key?(property.to_s) || data.has_key?(property.to_sym))
|
10
|
-
dependency_value.
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
if (data.has_key?(property.to_s) || data.has_key?(property.to_sym))
|
10
|
+
if dependency_value.is_a?(Array)
|
11
|
+
dependency_value.each do |value|
|
12
|
+
if !data.has_key?(value.to_s) && !data.has_key?(value.to_sym)
|
13
|
+
message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
|
14
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
15
|
+
end
|
14
16
|
end
|
17
|
+
elsif dependency_value.is_a?(Hash)
|
18
|
+
schema = JSON::Schema.new(dependency_value,current_schema.uri,validator)
|
19
|
+
schema.validate(data, fragments, processor, options)
|
15
20
|
end
|
16
21
|
end
|
17
22
|
end
|
@@ -1,22 +1,21 @@
|
|
1
1
|
require 'json-schema/attribute'
|
2
|
-
|
2
|
+
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
5
|
class DateFormat < FormatAttribute
|
6
|
+
REGEXP = /\A\d{4}-\d{2}-\d{2}\z/
|
7
|
+
|
6
8
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
9
|
if data.is_a?(String)
|
8
10
|
error_message = "The property '#{build_fragment(fragments)}' must be a date in the format of YYYY-MM-DD"
|
9
|
-
|
10
|
-
if (r.match(data))
|
11
|
+
if REGEXP.match(data)
|
11
12
|
begin
|
12
13
|
Date.parse(data)
|
13
14
|
rescue Exception
|
14
15
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
15
|
-
return
|
16
16
|
end
|
17
17
|
else
|
18
18
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
19
|
-
return
|
20
19
|
end
|
21
20
|
end
|
22
21
|
end
|
@@ -1,32 +1,33 @@
|
|
1
1
|
require 'json-schema/attribute'
|
2
|
-
|
2
|
+
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
5
|
class DateTimeFormat < FormatAttribute
|
6
|
+
REGEXP = /\A\d{4}-\d{2}-\d{2}T(\d{2}):(\d{2}):(\d{2})([\.,]\d+)?(Z|[+-](\d{2})(:?\d{2})?)?\z/
|
7
|
+
|
6
8
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
9
|
# Timestamp in restricted ISO-8601 YYYY-MM-DDThh:mm:ssZ with optional decimal fraction of the second
|
8
10
|
if data.is_a?(String)
|
9
11
|
error_message = "The property '#{build_fragment(fragments)}' must be a date/time in the ISO-8601 format of YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss.ssZ"
|
10
|
-
|
11
|
-
if (m = r.match(data))
|
12
|
+
if (m = REGEXP.match(data))
|
12
13
|
parts = data.split("T")
|
14
|
+
|
13
15
|
begin
|
14
16
|
Date.parse(parts[0])
|
15
17
|
rescue Exception
|
16
18
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
17
19
|
return
|
18
20
|
end
|
21
|
+
|
19
22
|
begin
|
20
23
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[1].to_i > 23
|
21
24
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[2].to_i > 59
|
22
25
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[3].to_i > 59
|
23
26
|
rescue Exception
|
24
27
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
25
|
-
return
|
26
28
|
end
|
27
29
|
else
|
28
30
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
29
|
-
return
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'json-schema/attribute'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class DateTimeV4Format < FormatAttribute
|
6
|
+
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
|
+
if data.is_a?(String)
|
8
|
+
error_message = "The property '#{build_fragment(fragments)}' must be a valid RFC3339 date/time string"
|
9
|
+
begin
|
10
|
+
DateTime.rfc3339(data)
|
11
|
+
rescue ArgumentError
|
12
|
+
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,20 +1,18 @@
|
|
1
1
|
require 'json-schema/attribute'
|
2
|
-
require '
|
2
|
+
require 'ipaddr'
|
3
|
+
|
3
4
|
module JSON
|
4
5
|
class Schema
|
5
6
|
class IP4Format < FormatAttribute
|
6
7
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
8
|
if data.is_a?(String)
|
8
9
|
error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv4 address"
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
else
|
15
|
-
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
16
|
-
return
|
10
|
+
begin
|
11
|
+
ip = IPAddr.new data
|
12
|
+
rescue ArgumentError => e
|
13
|
+
raise e unless e.message == 'invalid address'
|
17
14
|
end
|
15
|
+
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) unless ip && ip.ipv4?
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
@@ -1,28 +1,18 @@
|
|
1
1
|
require 'json-schema/attribute'
|
2
|
-
require '
|
2
|
+
require 'ipaddr'
|
3
|
+
|
3
4
|
module JSON
|
4
5
|
class Schema
|
5
6
|
class IP6Format < FormatAttribute
|
6
7
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
8
|
if data.is_a?(String)
|
8
9
|
error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv6 address"
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if parts.length > 8
|
14
|
-
condensed_zeros = false
|
15
|
-
parts.each do |part|
|
16
|
-
if part.length == 0
|
17
|
-
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if condensed_zeros
|
18
|
-
condensed_zeros = true
|
19
|
-
end
|
20
|
-
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if part.length > 4
|
21
|
-
end
|
22
|
-
else
|
23
|
-
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
24
|
-
return
|
10
|
+
begin
|
11
|
+
ip = IPAddr.new data
|
12
|
+
rescue ArgumentError => e
|
13
|
+
raise e unless e.message == 'invalid address'
|
25
14
|
end
|
15
|
+
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) unless ip && ip.ipv6?
|
26
16
|
end
|
27
17
|
end
|
28
18
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'json-schema/attribute'
|
2
|
-
|
2
|
+
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
5
|
class TimeFormat < FormatAttribute
|
6
|
+
REGEXP = /\A(\d{2}):(\d{2}):(\d{2})\z/
|
7
|
+
|
6
8
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
9
|
if data.is_a?(String)
|
8
10
|
error_message = "The property '#{build_fragment(fragments)}' must be a time in the format of hh:mm:ss"
|
9
|
-
|
10
|
-
if (m = r.match(data))
|
11
|
+
if (m = REGEXP.match(data))
|
11
12
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[1].to_i > 23
|
12
13
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[2].to_i > 59
|
13
14
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[3].to_i > 59
|
14
15
|
else
|
15
16
|
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
|
16
|
-
return
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module JSON
|
2
2
|
class Schema
|
3
3
|
class Validator
|
4
|
-
attr_accessor :attributes, :formats, :uri, :names
|
4
|
+
attr_accessor :attributes, :formats, :uri, :names
|
5
5
|
attr_reader :default_formats
|
6
6
|
|
7
7
|
def initialize()
|
@@ -10,7 +10,7 @@ module JSON
|
|
10
10
|
@default_formats = {}
|
11
11
|
@uri = nil
|
12
12
|
@names = []
|
13
|
-
@
|
13
|
+
@metaschema_name = ''
|
14
14
|
end
|
15
15
|
|
16
16
|
def extend_schema_definition(schema_uri)
|
@@ -26,6 +26,11 @@ module JSON
|
|
26
26
|
end
|
27
27
|
data
|
28
28
|
end
|
29
|
+
|
30
|
+
def metaschema
|
31
|
+
resources = File.expand_path('../../../../resources', __FILE__)
|
32
|
+
File.join(resources, @metaschema_name)
|
33
|
+
end
|
29
34
|
end
|
30
35
|
end
|
31
36
|
end
|
File without changes
|
@@ -70,6 +70,7 @@ module JSON
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def schema_from_fragment(base_schema, fragment)
|
73
|
+
schema_uri = base_schema.uri
|
73
74
|
fragments = fragment.split("/")
|
74
75
|
|
75
76
|
# ensure the first element was a hash, per the fragment spec
|
@@ -82,17 +83,17 @@ module JSON
|
|
82
83
|
if !base_schema.schema.has_key?(f)
|
83
84
|
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
|
84
85
|
end
|
85
|
-
|
86
|
+
base_schema = base_schema.schema[f]
|
86
87
|
elsif base_schema.is_a?(Hash)
|
87
88
|
if !base_schema.has_key?(f)
|
88
89
|
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
|
89
90
|
end
|
90
|
-
|
91
|
+
base_schema = JSON::Schema.new(base_schema[f],schema_uri,@options[:version])
|
91
92
|
elsif base_schema.is_a?(Array)
|
92
93
|
if base_schema[f.to_i].nil?
|
93
94
|
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
|
94
95
|
end
|
95
|
-
|
96
|
+
base_schema = JSON::Schema.new(base_schema[f.to_i],schema_uri,@options[:version])
|
96
97
|
else
|
97
98
|
raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
|
98
99
|
end
|
@@ -417,7 +418,7 @@ module JSON
|
|
417
418
|
when 'yajl'
|
418
419
|
json = StringIO.new(s)
|
419
420
|
parser = Yajl::Parser.new
|
420
|
-
parser.parse(json)
|
421
|
+
parser.parse(json) or raise JSON::Schema::JsonParseError.new("The JSON could not be parsed by yajl")
|
421
422
|
else
|
422
423
|
raise JSON::Schema::JsonParseError.new("No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json")
|
423
424
|
end
|
@@ -425,33 +426,22 @@ module JSON
|
|
425
426
|
end
|
426
427
|
|
427
428
|
if !defined?(MultiJson)
|
428
|
-
if
|
429
|
-
Gem::Specification::find_by_name('json')
|
430
|
-
rescue Gem::LoadError
|
431
|
-
false
|
432
|
-
rescue
|
433
|
-
Gem.available?('json')
|
434
|
-
end
|
429
|
+
if Gem::Specification::find_all_by_name('json').any?
|
435
430
|
require 'json'
|
436
431
|
@@available_json_backends << 'json'
|
437
432
|
@@json_backend = 'json'
|
433
|
+
else
|
434
|
+
# Try force-loading json for rubies > 1.9.2
|
435
|
+
begin
|
436
|
+
require 'json'
|
437
|
+
@@available_json_backends << 'json'
|
438
|
+
@@json_backend = 'json'
|
439
|
+
rescue LoadError
|
440
|
+
end
|
438
441
|
end
|
439
442
|
|
440
|
-
# Try force-loading json for rubies > 1.9.2
|
441
|
-
begin
|
442
|
-
require 'json'
|
443
|
-
@@available_json_backends << 'json'
|
444
|
-
@@json_backend = 'json'
|
445
|
-
rescue LoadError
|
446
|
-
end
|
447
443
|
|
448
|
-
if
|
449
|
-
Gem::Specification::find_by_name('yajl-ruby')
|
450
|
-
rescue Gem::LoadError
|
451
|
-
false
|
452
|
-
rescue
|
453
|
-
Gem.available?('yajl-ruby')
|
454
|
-
end
|
444
|
+
if Gem::Specification::find_all_by_name('yajl-ruby').any?
|
455
445
|
require 'yajl'
|
456
446
|
@@available_json_backends << 'yajl'
|
457
447
|
@@json_backend = 'yajl'
|
@@ -460,9 +450,7 @@ module JSON
|
|
460
450
|
if @@json_backend == 'yajl'
|
461
451
|
@@serializer = lambda{|o| Yajl::Encoder.encode(o) }
|
462
452
|
else
|
463
|
-
@@serializer = lambda{|o|
|
464
|
-
YAML.dump(o)
|
465
|
-
}
|
453
|
+
@@serializer = lambda{|o| YAML.dump(o) }
|
466
454
|
end
|
467
455
|
end
|
468
456
|
|
@@ -478,18 +466,12 @@ module JSON
|
|
478
466
|
|
479
467
|
private
|
480
468
|
|
481
|
-
if
|
482
|
-
Gem::Specification::find_by_name('uuidtools')
|
483
|
-
rescue Gem::LoadError
|
484
|
-
false
|
485
|
-
rescue
|
486
|
-
Gem.available?('uuidtools')
|
487
|
-
end
|
469
|
+
if Gem::Specification::find_all_by_name('uuidtools').any?
|
488
470
|
require 'uuidtools'
|
489
|
-
@@
|
471
|
+
@@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
|
490
472
|
else
|
491
|
-
require 'json-schema/
|
492
|
-
@@
|
473
|
+
require 'json-schema/util/uuid'
|
474
|
+
@@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s }
|
493
475
|
end
|
494
476
|
|
495
477
|
def serialize schema
|
@@ -500,8 +482,8 @@ module JSON
|
|
500
482
|
end
|
501
483
|
end
|
502
484
|
|
503
|
-
def
|
504
|
-
@@
|
485
|
+
def fake_uuid schema
|
486
|
+
@@fake_uuid_generator.call(schema)
|
505
487
|
end
|
506
488
|
|
507
489
|
def schema_to_list(schema)
|
@@ -517,7 +499,7 @@ module JSON
|
|
517
499
|
if schema.is_a?(String)
|
518
500
|
begin
|
519
501
|
# Build a fake URI for this
|
520
|
-
schema_uri = URI.parse(
|
502
|
+
schema_uri = URI.parse(fake_uuid(schema))
|
521
503
|
schema = JSON::Validator.parse(schema)
|
522
504
|
if @options[:list] && @options[:fragment].nil?
|
523
505
|
schema = schema_to_list(schema)
|
@@ -526,15 +508,7 @@ module JSON
|
|
526
508
|
Validator.add_schema(schema)
|
527
509
|
rescue
|
528
510
|
# Build a uri for it
|
529
|
-
schema_uri =
|
530
|
-
if schema_uri.relative?
|
531
|
-
# Check for absolute path
|
532
|
-
if schema[0,1] == '/'
|
533
|
-
schema_uri = URI.parse("file://#{schema}")
|
534
|
-
else
|
535
|
-
schema_uri = URI.parse("file://#{Dir.pwd}/#{schema}")
|
536
|
-
end
|
537
|
-
end
|
511
|
+
schema_uri = normalized_uri(schema)
|
538
512
|
if Validator.schemas[schema_uri.to_s].nil?
|
539
513
|
schema = JSON::Validator.parse(open(schema_uri.to_s).read)
|
540
514
|
if @options[:list] && @options[:fragment].nil?
|
@@ -546,7 +520,7 @@ module JSON
|
|
546
520
|
schema = Validator.schemas[schema_uri.to_s]
|
547
521
|
if @options[:list] && @options[:fragment].nil?
|
548
522
|
schema = schema_to_list(schema.schema)
|
549
|
-
schema_uri = URI.parse(
|
523
|
+
schema_uri = URI.parse(fake_uuid(serialize(schema)))
|
550
524
|
schema = JSON::Schema.new(schema, schema_uri, @options[:version])
|
551
525
|
Validator.add_schema(schema)
|
552
526
|
end
|
@@ -557,7 +531,7 @@ module JSON
|
|
557
531
|
if @options[:list] && @options[:fragment].nil?
|
558
532
|
schema = schema_to_list(schema)
|
559
533
|
end
|
560
|
-
schema_uri = URI.parse(
|
534
|
+
schema_uri = URI.parse(fake_uuid(serialize(schema)))
|
561
535
|
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
|
562
536
|
Validator.add_schema(schema)
|
563
537
|
else
|
@@ -572,28 +546,14 @@ module JSON
|
|
572
546
|
if @options[:json]
|
573
547
|
data = JSON::Validator.parse(data)
|
574
548
|
elsif @options[:uri]
|
575
|
-
json_uri =
|
576
|
-
if json_uri.relative?
|
577
|
-
if data[0,1] == '/'
|
578
|
-
json_uri = URI.parse("file://#{data}")
|
579
|
-
else
|
580
|
-
json_uri = URI.parse("file://#{Dir.pwd}/#{data}")
|
581
|
-
end
|
582
|
-
end
|
549
|
+
json_uri = normalized_uri(data)
|
583
550
|
data = JSON::Validator.parse(open(json_uri.to_s).read)
|
584
551
|
elsif data.is_a?(String)
|
585
552
|
begin
|
586
553
|
data = JSON::Validator.parse(data)
|
587
554
|
rescue
|
588
555
|
begin
|
589
|
-
json_uri =
|
590
|
-
if json_uri.relative?
|
591
|
-
if data[0,1] == '/'
|
592
|
-
json_uri = URI.parse("file://#{data}")
|
593
|
-
else
|
594
|
-
json_uri = URI.parse("file://#{Dir.pwd}/#{data}")
|
595
|
-
end
|
596
|
-
end
|
556
|
+
json_uri = normalized_uri(data)
|
597
557
|
data = JSON::Validator.parse(open(json_uri.to_s).read)
|
598
558
|
rescue
|
599
559
|
# Silently discard the error - the data will not change
|
@@ -604,5 +564,18 @@ module JSON
|
|
604
564
|
data
|
605
565
|
end
|
606
566
|
|
567
|
+
def normalized_uri(data)
|
568
|
+
uri = URI.parse(data)
|
569
|
+
if uri.relative?
|
570
|
+
# Check for absolute path
|
571
|
+
if data[0,1] == '/'
|
572
|
+
uri = URI.parse("file://#{data}")
|
573
|
+
else
|
574
|
+
uri = URI.parse("file://#{Dir.pwd}/#{data}")
|
575
|
+
end
|
576
|
+
end
|
577
|
+
uri
|
578
|
+
end
|
579
|
+
|
607
580
|
end
|
608
581
|
end
|
@@ -35,7 +35,7 @@ module JSON
|
|
35
35
|
@formats = @default_formats.clone
|
36
36
|
@uri = URI.parse("http://json-schema.org/draft-01/schema#")
|
37
37
|
@names = ["draft1"]
|
38
|
-
@
|
38
|
+
@metaschema_name = "draft-01.json"
|
39
39
|
end
|
40
40
|
|
41
41
|
JSON::Validator.register_validator(self.new)
|
@@ -36,7 +36,7 @@ module JSON
|
|
36
36
|
@formats = @default_formats.clone
|
37
37
|
@uri = URI.parse("http://json-schema.org/draft-02/schema#")
|
38
38
|
@names = ["draft2"]
|
39
|
-
@
|
39
|
+
@metaschema_name = "draft-02.json"
|
40
40
|
end
|
41
41
|
|
42
42
|
JSON::Validator.register_validator(self.new)
|
@@ -40,7 +40,7 @@ module JSON
|
|
40
40
|
@formats = @default_formats.clone
|
41
41
|
@uri = URI.parse("http://json-schema.org/draft-03/schema#")
|
42
42
|
@names = ["draft3", "http://json-schema.org/draft-03/schema#"]
|
43
|
-
@
|
43
|
+
@metaschema_name = "draft-03.json"
|
44
44
|
end
|
45
45
|
|
46
46
|
JSON::Validator.register_validator(self.new)
|
@@ -37,7 +37,7 @@ module JSON
|
|
37
37
|
"$ref" => JSON::Schema::RefAttribute
|
38
38
|
}
|
39
39
|
@default_formats = {
|
40
|
-
'date-time' =>
|
40
|
+
'date-time' => DateTimeV4Format,
|
41
41
|
'ipv4' => IP4Format,
|
42
42
|
'ipv6' => IP6Format,
|
43
43
|
'uri' => UriFormat
|
@@ -45,7 +45,7 @@ module JSON
|
|
45
45
|
@formats = @default_formats.clone
|
46
46
|
@uri = URI.parse("http://json-schema.org/draft-04/schema#")
|
47
47
|
@names = ["draft4", "http://json-schema.org/draft-04/schema#"]
|
48
|
-
@
|
48
|
+
@metaschema_name = "draft-04.json"
|
49
49
|
end
|
50
50
|
|
51
51
|
JSON::Validator.register_validator(self.new)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module JSON
|
2
|
+
class Schema
|
3
|
+
|
4
|
+
class HyperDraft4 < Draft4
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
@uri = URI.parse("http://json-schema.org/draft-04/hyper-schema#")
|
8
|
+
end
|
9
|
+
|
10
|
+
JSON::Validator.register_validator(self.new)
|
11
|
+
JSON::Validator.register_default_validator(self.new)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -11,11 +11,8 @@ class CommonTestSuiteTest < Test::Unit::TestCase
|
|
11
11
|
"draft3/disallow.json",
|
12
12
|
"draft3/optional/format.json",
|
13
13
|
"draft3/optional/jsregex.json",
|
14
|
-
"draft3/ref.json",
|
15
14
|
"draft3/refRemote.json",
|
16
|
-
"draft4/dependencies.json",
|
17
15
|
"draft4/optional/format.json",
|
18
|
-
"draft4/ref.json",
|
19
16
|
"draft4/refRemote.json",
|
20
17
|
]
|
21
18
|
|
@@ -31,20 +28,20 @@ class CommonTestSuiteTest < Test::Unit::TestCase
|
|
31
28
|
v = nil
|
32
29
|
|
33
30
|
test["tests"].each do |t|
|
34
|
-
err_id = "#{rel_file}
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
31
|
+
err_id = "#{rel_file}: #{base_description}/#{t['description']}"
|
32
|
+
|
33
|
+
unless IGNORED_TESTS.include?(rel_file)
|
34
|
+
define_method("test_#{err_id}") do
|
35
|
+
assert_nothing_raised("Exception raised running #{err_id}") do
|
36
|
+
v = JSON::Validator.fully_validate(schema,
|
37
|
+
t["data"],
|
38
|
+
:validate_schema => true,
|
39
|
+
:version => version
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
assert_equal t["valid"], v.empty?, "Common test suite case failed: #{err_id}\n#{v}"
|
45
44
|
end
|
46
|
-
|
47
|
-
assert v.empty? == t["valid"], "Common test suite case failed: #{err_id}\n#{v}"
|
48
45
|
end
|
49
46
|
end
|
50
47
|
end
|
data/test/test_custom_format.rb
CHANGED
@@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../lib/json-schema'
|
|
5
5
|
class JSONSchemaCustomFormatTest < Test::Unit::TestCase
|
6
6
|
def setup
|
7
7
|
@all_versions = ['draft1', 'draft2', 'draft3', 'draft4']
|
8
|
-
@format_proc =
|
8
|
+
@format_proc = lambda { |value| raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42" }
|
9
9
|
@schema_4 = {
|
10
10
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
11
11
|
"properties" => {
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
require 'json-schema'
|
3
|
+
|
4
|
+
class FragmentValidationWithRef < Test::Unit::TestCase
|
5
|
+
def whole_schema
|
6
|
+
{
|
7
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
8
|
+
"type" => "object",
|
9
|
+
"definitions" => {
|
10
|
+
"post" => {
|
11
|
+
"type" => "object",
|
12
|
+
"properties" => {
|
13
|
+
"content" => {
|
14
|
+
"type" => "string"
|
15
|
+
},
|
16
|
+
"author" => {
|
17
|
+
"type" => "string"
|
18
|
+
}
|
19
|
+
}
|
20
|
+
},
|
21
|
+
"posts" => {
|
22
|
+
"type" => "array",
|
23
|
+
"items" => {
|
24
|
+
"$ref" => "#/definitions/post"
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_validation_of_fragment
|
32
|
+
data = [{"content" => "ohai", "author" => "Bob"}]
|
33
|
+
v = nil
|
34
|
+
assert_nothing_raised do
|
35
|
+
v = JSON::Validator.fully_validate(whole_schema,data,:fragment => "#/definitions/posts")
|
36
|
+
end
|
37
|
+
|
38
|
+
assert(v.empty?, v.join("\n"))
|
39
|
+
end
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -613,6 +613,8 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
|
|
613
613
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
614
614
|
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
615
615
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
616
|
+
assert(JSON::Validator.validate(schema, {"a" => "::1"}, :version => :draft1), 'validate with shortcut')
|
617
|
+
assert(!JSON::Validator.validate(schema, {"a" => "42"}, :version => :draft1), 'not validate a simple number')
|
616
618
|
end
|
617
619
|
|
618
620
|
def test_format_time
|
@@ -637,6 +639,10 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
|
|
637
639
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
638
640
|
data = {"a" => "12:00:00b"}
|
639
641
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
642
|
+
data = {"a" => "12:00:00"}
|
643
|
+
assert(JSON::Validator.validate(schema,data,:version => :draft1))
|
644
|
+
data = {"a" => "12:00:00\nabc"}
|
645
|
+
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
640
646
|
end
|
641
647
|
|
642
648
|
|
@@ -658,6 +664,8 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
|
|
658
664
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
659
665
|
data = {"a" => "2010-01-01n"}
|
660
666
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
667
|
+
data = {"a" => "2010-01-01\nabc"}
|
668
|
+
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
661
669
|
end
|
662
670
|
|
663
671
|
def test_format_datetime
|
@@ -682,6 +690,8 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
|
|
682
690
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
683
691
|
data = {"a" => "2010-01-0112:00:00Z"}
|
684
692
|
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
693
|
+
data = {"a" => "2010-01-01T12:00:00Z\nabc"}
|
694
|
+
assert(!JSON::Validator.validate(schema,data,:version => :draft1))
|
685
695
|
end
|
686
696
|
|
687
697
|
def test_format_unknown
|
@@ -685,6 +685,8 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
685
685
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
686
686
|
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
687
687
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
688
|
+
assert(JSON::Validator.validate(schema, {"a" => "::1"}, :version => :draft2), 'validate with shortcut')
|
689
|
+
assert(!JSON::Validator.validate(schema, {"a" => "42"}, :version => :draft2), 'not validate a simple number')
|
688
690
|
end
|
689
691
|
|
690
692
|
def test_format_time
|
@@ -709,6 +711,8 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
709
711
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
710
712
|
data = {"a" => "12:00:00b"}
|
711
713
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
714
|
+
data = {"a" => "12:00:00\nabc"}
|
715
|
+
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
712
716
|
end
|
713
717
|
|
714
718
|
|
@@ -730,6 +734,8 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
730
734
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
731
735
|
data = {"a" => "2010-01-01n"}
|
732
736
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
737
|
+
data = {"a" => "2010-01-01\nabc"}
|
738
|
+
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
733
739
|
end
|
734
740
|
|
735
741
|
def test_format_datetime
|
@@ -754,6 +760,8 @@ class JSONSchemaDraft2Test < Test::Unit::TestCase
|
|
754
760
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
755
761
|
data = {"a" => "2010-01-0112:00:00Z"}
|
756
762
|
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
763
|
+
data = {"a" => "2010-01-01T12:00:00Z\nabc"}
|
764
|
+
assert(!JSON::Validator.validate(schema,data,:version => :draft2))
|
757
765
|
end
|
758
766
|
|
759
767
|
def test_format_unknown
|
@@ -984,6 +984,8 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
984
984
|
assert(!JSON::Validator.validate(schema,data))
|
985
985
|
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
986
986
|
assert(!JSON::Validator.validate(schema,data))
|
987
|
+
assert(JSON::Validator.validate(schema, {"a" => "::1"}), 'validate with shortcut')
|
988
|
+
assert(!JSON::Validator.validate(schema, {"a" => "42"}), 'not validate a simple number')
|
987
989
|
end
|
988
990
|
|
989
991
|
def test_format_time
|
@@ -1009,6 +1011,8 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1009
1011
|
assert(!JSON::Validator.validate(schema,data))
|
1010
1012
|
data = {"a" => "12:00:00b"}
|
1011
1013
|
assert(!JSON::Validator.validate(schema,data))
|
1014
|
+
data = {"a" => "12:00:00\nabc"}
|
1015
|
+
assert(!JSON::Validator.validate(schema,data))
|
1012
1016
|
end
|
1013
1017
|
|
1014
1018
|
|
@@ -1031,6 +1035,8 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1031
1035
|
assert(!JSON::Validator.validate(schema,data))
|
1032
1036
|
data = {"a" => "2010-01-01n"}
|
1033
1037
|
assert(!JSON::Validator.validate(schema,data))
|
1038
|
+
data = {"a" => "2010-01-01\nabc"}
|
1039
|
+
assert(!JSON::Validator.validate(schema,data))
|
1034
1040
|
end
|
1035
1041
|
|
1036
1042
|
def test_format_datetime
|
@@ -1060,6 +1066,8 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
|
|
1060
1066
|
assert(!JSON::Validator.validate(schema,data))
|
1061
1067
|
data = {"a" => "2010-01-0112:00:00Z"}
|
1062
1068
|
assert(!JSON::Validator.validate(schema,data))
|
1069
|
+
data = {"a" => "2010-01-01T12:00:00.1Z\nabc"}
|
1070
|
+
assert(!JSON::Validator.validate(schema,data))
|
1063
1071
|
|
1064
1072
|
# test with a specific timezone
|
1065
1073
|
data = {"a" => "2010-01-01T12:00:00+01"}
|
@@ -189,9 +189,7 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
189
189
|
|
190
190
|
assert(JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => ['string', 'null']}, "hello"))
|
191
191
|
assert(!JSON::Validator.validate({"$schema" => "http://json-schema.org/draft-04/schema#",'type' => ['integer', 'object']}, "hello"))
|
192
|
-
|
193
|
-
|
194
|
-
|
192
|
+
end
|
195
193
|
|
196
194
|
def test_required
|
197
195
|
# Set up the default datatype
|
@@ -927,6 +925,8 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
927
925
|
assert(!JSON::Validator.validate(schema,data))
|
928
926
|
data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
|
929
927
|
assert(!JSON::Validator.validate(schema,data))
|
928
|
+
assert(JSON::Validator.validate(schema, {"a" => "::1"}), 'validate with shortcut')
|
929
|
+
assert(!JSON::Validator.validate(schema, {"a" => "42"}), 'not validate a simple number')
|
930
930
|
end
|
931
931
|
|
932
932
|
|
@@ -942,9 +942,9 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
942
942
|
data = {"a" => "2010-01-01T12:00:00.1Z"}
|
943
943
|
assert(JSON::Validator.validate(schema,data))
|
944
944
|
data = {"a" => "2010-01-01T12:00:00,1Z"}
|
945
|
-
assert(JSON::Validator.validate(schema,data))
|
945
|
+
assert(!JSON::Validator.validate(schema,data))
|
946
946
|
data = {"a" => "2010-01-01T12:00:00+0000"}
|
947
|
-
assert(JSON::Validator.validate(schema,data))
|
947
|
+
assert(!JSON::Validator.validate(schema,data))
|
948
948
|
data = {"a" => "2010-01-01T12:00:00+00:00"}
|
949
949
|
assert(JSON::Validator.validate(schema,data))
|
950
950
|
data = {"a" => "2010-01-32T12:00:00Z"}
|
@@ -952,13 +952,13 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
952
952
|
data = {"a" => "2010-13-01T12:00:00Z"}
|
953
953
|
assert(!JSON::Validator.validate(schema,data))
|
954
954
|
data = {"a" => "2010-01-01T24:00:00Z"}
|
955
|
-
assert(
|
955
|
+
assert(JSON::Validator.validate(schema,data))
|
956
956
|
data = {"a" => "2010-01-01T12:60:00Z"}
|
957
957
|
assert(!JSON::Validator.validate(schema,data))
|
958
958
|
data = {"a" => "2010-01-01T12:00:60Z"}
|
959
|
-
assert(
|
959
|
+
assert(JSON::Validator.validate(schema,data))
|
960
960
|
data = {"a" => "2010-01-01T12:00:00z"}
|
961
|
-
assert(
|
961
|
+
assert(JSON::Validator.validate(schema,data))
|
962
962
|
data = {"a" => "2010-01-0112:00:00Z"}
|
963
963
|
assert(!JSON::Validator.validate(schema,data))
|
964
964
|
end
|
@@ -1060,6 +1060,32 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
|
|
1060
1060
|
assert(JSON::Validator.validate(schema,data))
|
1061
1061
|
end
|
1062
1062
|
|
1063
|
+
def test_schema_dependency
|
1064
|
+
schema = {
|
1065
|
+
"type"=> "object",
|
1066
|
+
"properties"=> {
|
1067
|
+
"name"=> { "type"=> "string" },
|
1068
|
+
"credit_card"=> { "type"=> "number" }
|
1069
|
+
},
|
1070
|
+
"required"=> ["name"],
|
1071
|
+
"dependencies"=> {
|
1072
|
+
"credit_card"=> {
|
1073
|
+
"properties"=> {
|
1074
|
+
"billing_address"=> { "type"=> "string" }
|
1075
|
+
},
|
1076
|
+
"required"=> ["billing_address"]
|
1077
|
+
}
|
1078
|
+
}
|
1079
|
+
}
|
1080
|
+
data = {
|
1081
|
+
"name" => "John Doe",
|
1082
|
+
"credit_card" => 5555555555555555
|
1083
|
+
}
|
1084
|
+
assert(!JSON::Validator.validate(schema,data), 'test schema dependency with invalid data')
|
1085
|
+
data['billing_address'] = "Somewhere over the rainbow"
|
1086
|
+
assert(JSON::Validator.validate(schema,data), 'test schema dependency with valid data')
|
1087
|
+
end
|
1088
|
+
|
1063
1089
|
def test_default
|
1064
1090
|
schema = {
|
1065
1091
|
"$schema" => "http://json-schema.org/draft-04/schema#",
|
data/test/test_ruby_schema.rb
CHANGED
@@ -21,16 +21,16 @@ class RubySchemaTest < Test::Unit::TestCase
|
|
21
21
|
|
22
22
|
def test_symbol_keys
|
23
23
|
schema = {
|
24
|
-
type
|
25
|
-
required
|
26
|
-
properties
|
27
|
-
a
|
28
|
-
b
|
24
|
+
:type => 'object',
|
25
|
+
:required => ["a"],
|
26
|
+
:properties => {
|
27
|
+
:a => {:type => "integer", :default => 42},
|
28
|
+
:b => {:type => "integer"}
|
29
29
|
}
|
30
30
|
}
|
31
31
|
|
32
32
|
data = {
|
33
|
-
a
|
33
|
+
:a => 5
|
34
34
|
}
|
35
35
|
|
36
36
|
assert(JSON::Validator.validate(schema, data))
|
@@ -38,15 +38,15 @@ class RubySchemaTest < Test::Unit::TestCase
|
|
38
38
|
|
39
39
|
def test_symbol_keys_in_hash_within_array
|
40
40
|
schema = {
|
41
|
-
type
|
42
|
-
properties
|
43
|
-
a
|
44
|
-
type
|
45
|
-
items
|
41
|
+
:type => 'object',
|
42
|
+
:properties => {
|
43
|
+
:a => {
|
44
|
+
:type => "array",
|
45
|
+
:items => [
|
46
46
|
{
|
47
|
-
properties
|
48
|
-
b
|
49
|
-
type
|
47
|
+
:properties => {
|
48
|
+
:b => {
|
49
|
+
:type => "integer"
|
50
50
|
}
|
51
51
|
}
|
52
52
|
}
|
@@ -56,9 +56,9 @@ class RubySchemaTest < Test::Unit::TestCase
|
|
56
56
|
}
|
57
57
|
|
58
58
|
data = {
|
59
|
-
a
|
59
|
+
:a => [
|
60
60
|
{
|
61
|
-
b
|
61
|
+
:b => 1
|
62
62
|
}
|
63
63
|
]
|
64
64
|
}
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
|
+
require 'tmpdir'
|
2
3
|
require File.dirname(__FILE__) + '/../lib/json-schema'
|
3
4
|
|
4
5
|
class JSONSchemaValidation < Test::Unit::TestCase
|
@@ -82,4 +83,14 @@ class JSONSchemaValidation < Test::Unit::TestCase
|
|
82
83
|
assert(JSON::Validator.validate(valid_schema_v3,data,:validate_schema => true))
|
83
84
|
assert(!JSON::Validator.validate(invalid_schema_v3,data,:validate_schema => true))
|
84
85
|
end
|
86
|
+
|
87
|
+
def test_schema_validation_from_different_directory
|
88
|
+
Dir.mktmpdir do |tmpdir|
|
89
|
+
Dir.chdir(tmpdir) do
|
90
|
+
data = {"b" => {"a" => 5}}
|
91
|
+
assert(JSON::Validator.validate(valid_schema_v4,data,:validate_schema => true, :version => :draft4))
|
92
|
+
assert(!JSON::Validator.validate(invalid_schema_v4,data,:validate_schema => true, :version => :draft4))
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
85
96
|
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: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenny Hoxworth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: hoxworth@gmail.com
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/json-schema/attributes/formats/custom.rb
|
34
34
|
- lib/json-schema/attributes/formats/date.rb
|
35
35
|
- lib/json-schema/attributes/formats/date_time.rb
|
36
|
+
- lib/json-schema/attributes/formats/date_time_v4.rb
|
36
37
|
- lib/json-schema/attributes/formats/ip4.rb
|
37
38
|
- lib/json-schema/attributes/formats/ip6.rb
|
38
39
|
- lib/json-schema/attributes/formats/time.rb
|
@@ -69,14 +70,15 @@ files:
|
|
69
70
|
- lib/json-schema/schema/validator.rb
|
70
71
|
- lib/json-schema/schema.rb
|
71
72
|
- lib/json-schema/uri/file.rb
|
72
|
-
- lib/json-schema/uri/uuid.rb
|
73
73
|
- lib/json-schema/util/array_set.rb
|
74
74
|
- lib/json-schema/util/hash.rb
|
75
|
+
- lib/json-schema/util/uuid.rb
|
75
76
|
- lib/json-schema/validator.rb
|
76
77
|
- lib/json-schema/validators/draft1.rb
|
77
78
|
- lib/json-schema/validators/draft2.rb
|
78
79
|
- lib/json-schema/validators/draft3.rb
|
79
80
|
- lib/json-schema/validators/draft4.rb
|
81
|
+
- lib/json-schema/validators/hyper-draft4.rb
|
80
82
|
- lib/json-schema.rb
|
81
83
|
- resources/draft-01.json
|
82
84
|
- resources/draft-02.json
|
@@ -93,7 +95,9 @@ files:
|
|
93
95
|
- test/test_extends_and_additionalProperties.rb
|
94
96
|
- test/test_files_v3.rb
|
95
97
|
- test/test_fragment_resolution.rb
|
98
|
+
- test/test_fragment_validation_with_ref.rb
|
96
99
|
- test/test_full_validation.rb
|
100
|
+
- test/test_helper.rb
|
97
101
|
- test/test_jsonschema_draft1.rb
|
98
102
|
- test/test_jsonschema_draft2.rb
|
99
103
|
- test/test_jsonschema_draft3.rb
|
@@ -139,12 +143,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
139
143
|
requirements:
|
140
144
|
- - ! '>='
|
141
145
|
- !ruby/object:Gem::Version
|
142
|
-
version:
|
146
|
+
version: 1.8.7
|
143
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
148
|
requirements:
|
145
149
|
- - ! '>='
|
146
150
|
- !ruby/object:Gem::Version
|
147
|
-
version: '
|
151
|
+
version: '1.8'
|
148
152
|
requirements: []
|
149
153
|
rubyforge_project:
|
150
154
|
rubygems_version: 2.0.7
|
@@ -161,7 +165,9 @@ test_files:
|
|
161
165
|
- test/test_extends_and_additionalProperties.rb
|
162
166
|
- test/test_files_v3.rb
|
163
167
|
- test/test_fragment_resolution.rb
|
168
|
+
- test/test_fragment_validation_with_ref.rb
|
164
169
|
- test/test_full_validation.rb
|
170
|
+
- test/test_helper.rb
|
165
171
|
- test/test_jsonschema_draft1.rb
|
166
172
|
- test/test_jsonschema_draft2.rb
|
167
173
|
- test/test_jsonschema_draft3.rb
|