json_schema 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d133299adf9a9c71dad5525df64e25d4846e35e
4
- data.tar.gz: 4d9aed1582b80eefcfc12656dddaf65b77fd894a
3
+ metadata.gz: 18987da218c27d388ff9125c52d5de7f134ca0bd
4
+ data.tar.gz: ccc08d794c3acca53bd41381ee93289725756948
5
5
  SHA512:
6
- metadata.gz: 6f156d37e4067bae3bbd2e7a471f921d362afea8e2900c88c13278b383efd52a5f7fefd3129418c2dd406a0ec5a103b748aa7183238d7445e67dbbe969f63b4b
7
- data.tar.gz: 9e118b7e172ed7c52b8b9cddd26a29e6a8e872e0f8782f6a57f34bdd323165b3996b4d405e8639441fb4a5f93fbb155f24b5fd59ecf98cfead00e4a49dee0ad6
6
+ metadata.gz: a6fd39a06bffa1e891ba948eafd12c8c1a6d79fc42307b334c455fe11d26a1ed5dab8d89e27e6b199b6a302bef947ceebc39405070f0f473e90b7ba8f69f28d0
7
+ data.tar.gz: 27679f000218340b5ed6d249ae0b1ca0217de4cde8fb57b5b1dd121d49390f1fec57f48697897b64ba1d52911657c5b9a21c71772838bcacde7aaccabe00eaa1
data/README.md CHANGED
@@ -30,6 +30,9 @@ schema.validate!(data)
30
30
  schema.links.each do |link|
31
31
  puts "#{link.method} #{link.href}"
32
32
  end
33
+
34
+ # abort on first error, instead of listing them all:
35
+ schema.validate!(data, fail_fast: true)
33
36
  ```
34
37
 
35
38
  Errors have a `message` (for humans), and `type` (for machines).
@@ -40,6 +43,11 @@ for more info.
40
43
  Non-bang methods return a two-element array, with `true`/`false` at index 0
41
44
  to indicate pass/fail, and an array of errors at index 1 (if any).
42
45
 
46
+ Passing `fail_fast: true` (default: `false`) will cause the validator to abort
47
+ on the first error it encounters and report just that. Even on fully valid data
48
+ this can offer some speed improvement, since it doesn't have to collect error
49
+ messages that might be later discarded (think of e.g. the `anyOf` directive).
50
+
43
51
  ## Development
44
52
 
45
53
  Run the test suite with:
@@ -21,6 +21,9 @@ parser = OptionParser.new { |opts|
21
21
  opts.on("-s", "--schema SCHEMA", "Additional schema to use for references") do |s|
22
22
  command.extra_schemas << s
23
23
  end
24
+ opts.on("-f", "--fail-fast", "Abort after encountering the first validation error") do |s|
25
+ command.fail_fast = true
26
+ end
24
27
  }
25
28
 
26
29
  if $0 == __FILE__
@@ -5,6 +5,7 @@ require_relative "../json_schema"
5
5
  module Commands
6
6
  class ValidateSchema
7
7
  attr_accessor :detect
8
+ attr_accessor :fail_fast
8
9
  attr_accessor :extra_schemas
9
10
 
10
11
  attr_accessor :errors
@@ -12,6 +13,7 @@ module Commands
12
13
 
13
14
  def initialize
14
15
  @detect = false
16
+ @fail_fast = false
15
17
  @extra_schemas = []
16
18
 
17
19
  @errors = []
@@ -46,7 +48,7 @@ module Commands
46
48
  end
47
49
  end
48
50
 
49
- valid, errors = schema.validate(data)
51
+ valid, errors = schema.validate(data, fail_fast: fail_fast)
50
52
 
51
53
  if valid
52
54
  @messages += ["#{data_file} is valid."]
@@ -270,14 +270,14 @@ module JsonSchema
270
270
  end
271
271
  end
272
272
 
273
- def validate(data)
273
+ def validate(data, fail_fast: false)
274
274
  validator = Validator.new(self)
275
- valid = validator.validate(data)
275
+ valid = validator.validate(data, fail_fast: fail_fast)
276
276
  [valid, validator.errors]
277
277
  end
278
278
 
279
- def validate!(data)
280
- Validator.new(self).validate!(data)
279
+ def validate!(data, fail_fast: false)
280
+ Validator.new(self).validate!(data, fail_fast: fail_fast)
281
281
  end
282
282
 
283
283
  # Link subobject for a hyperschema.
@@ -8,15 +8,30 @@ module JsonSchema
8
8
  @schema = schema
9
9
  end
10
10
 
11
- def validate(data)
11
+ def validate(data, fail_fast: false)
12
12
  @errors = []
13
13
  @visits = {}
14
- validate_data(@schema, data, @errors, ['#'])
14
+ @fail_fast = fail_fast
15
+
16
+ # This dynamically creates the "strict_or_fast_and" method which is used
17
+ # throughout the validator to combine the previous validation result with
18
+ # another validation check.
19
+ # Logic wise, we could simply define this method without meta programming
20
+ # and decide every time to either call fast_and or strict_end.
21
+ # Unfortunately this has a small overhead, that adds up over the runtime
22
+ # of the validator – about 5% if we check @fail_fast everytime.
23
+ # For more details, please see https://github.com/brandur/json_schema/pull/96
24
+ and_operation = method(@fail_fast ? :fast_and : :strict_and)
25
+ define_singleton_method(:strict_or_fast_and, and_operation)
26
+
27
+ catch(:fail_fast) do
28
+ validate_data(@schema, data, @errors, ['#'])
29
+ end
15
30
  @errors.size == 0
16
31
  end
17
32
 
18
- def validate!(data)
19
- if !validate(data)
33
+ def validate!(data, fail_fast: false)
34
+ if !validate(data, fail_fast: fail_fast)
20
35
  raise AggregateError.new(@errors)
21
36
  end
22
37
  end
@@ -59,6 +74,11 @@ module JsonSchema
59
74
  valid_old && valid_new
60
75
  end
61
76
 
77
+ def fast_and(valid_old, valid_new)
78
+ throw :fail_fast, false if !valid_new
79
+ valid_old && valid_new
80
+ end
81
+
62
82
  def validate_data(schema, data, errors, path)
63
83
  valid = true
64
84
  # detect a validation loop
@@ -67,46 +87,46 @@ module JsonSchema
67
87
  end
68
88
 
69
89
  # validation: any
70
- valid = strict_and valid, validate_all_of(schema, data, errors, path)
71
- valid = strict_and valid, validate_any_of(schema, data, errors, path)
72
- valid = strict_and valid, validate_enum(schema, data, errors, path)
73
- valid = strict_and valid, validate_one_of(schema, data, errors, path)
74
- valid = strict_and valid, validate_not(schema, data, errors, path)
75
- valid = strict_and valid, validate_type(schema, data, errors, path)
90
+ valid = strict_or_fast_and valid, validate_all_of(schema, data, errors, path)
91
+ valid = strict_or_fast_and valid, validate_any_of(schema, data, errors, path)
92
+ valid = strict_or_fast_and valid, validate_enum(schema, data, errors, path)
93
+ valid = strict_or_fast_and valid, validate_one_of(schema, data, errors, path)
94
+ valid = strict_or_fast_and valid, validate_not(schema, data, errors, path)
95
+ valid = strict_or_fast_and valid, validate_type(schema, data, errors, path)
76
96
 
77
97
  # validation: array
78
98
  if data.is_a?(Array)
79
- valid = strict_and valid, validate_items(schema, data, errors, path)
80
- valid = strict_and valid, validate_max_items(schema, data, errors, path)
81
- valid = strict_and valid, validate_min_items(schema, data, errors, path)
82
- valid = strict_and valid, validate_unique_items(schema, data, errors, path)
99
+ valid = strict_or_fast_and valid, validate_items(schema, data, errors, path)
100
+ valid = strict_or_fast_and valid, validate_max_items(schema, data, errors, path)
101
+ valid = strict_or_fast_and valid, validate_min_items(schema, data, errors, path)
102
+ valid = strict_or_fast_and valid, validate_unique_items(schema, data, errors, path)
83
103
  end
84
104
 
85
105
  # validation: integer/number
86
106
  if data.is_a?(Float) || data.is_a?(Integer)
87
- valid = strict_and valid, validate_max(schema, data, errors, path)
88
- valid = strict_and valid, validate_min(schema, data, errors, path)
89
- valid = strict_and valid, validate_multiple_of(schema, data, errors, path)
107
+ valid = strict_or_fast_and valid, validate_max(schema, data, errors, path)
108
+ valid = strict_or_fast_and valid, validate_min(schema, data, errors, path)
109
+ valid = strict_or_fast_and valid, validate_multiple_of(schema, data, errors, path)
90
110
  end
91
111
 
92
112
  # validation: object
93
113
  if data.is_a?(Hash)
94
- valid = strict_and valid, validate_additional_properties(schema, data, errors, path)
95
- valid = strict_and valid, validate_dependencies(schema, data, errors, path)
96
- valid = strict_and valid, validate_max_properties(schema, data, errors, path)
97
- valid = strict_and valid, validate_min_properties(schema, data, errors, path)
98
- valid = strict_and valid, validate_pattern_properties(schema, data, errors, path)
99
- valid = strict_and valid, validate_properties(schema, data, errors, path)
100
- valid = strict_and valid, validate_required(schema, data, errors, path, schema.required)
101
- valid = strict_and valid, validate_strict_properties(schema, data, errors, path)
114
+ valid = strict_or_fast_and valid, validate_additional_properties(schema, data, errors, path)
115
+ valid = strict_or_fast_and valid, validate_dependencies(schema, data, errors, path)
116
+ valid = strict_or_fast_and valid, validate_max_properties(schema, data, errors, path)
117
+ valid = strict_or_fast_and valid, validate_min_properties(schema, data, errors, path)
118
+ valid = strict_or_fast_and valid, validate_pattern_properties(schema, data, errors, path)
119
+ valid = strict_or_fast_and valid, validate_properties(schema, data, errors, path)
120
+ valid = strict_or_fast_and valid, validate_required(schema, data, errors, path, schema.required)
121
+ valid = strict_or_fast_and valid, validate_strict_properties(schema, data, errors, path)
102
122
  end
103
123
 
104
124
  # validation: string
105
125
  if data.is_a?(String)
106
- valid = strict_and valid, validate_format(schema, data, errors, path)
107
- valid = strict_and valid, validate_max_length(schema, data, errors, path)
108
- valid = strict_and valid, validate_min_length(schema, data, errors, path)
109
- valid = strict_and valid, validate_pattern(schema, data, errors, path)
126
+ valid = strict_or_fast_and valid, validate_format(schema, data, errors, path)
127
+ valid = strict_or_fast_and valid, validate_max_length(schema, data, errors, path)
128
+ valid = strict_or_fast_and valid, validate_min_length(schema, data, errors, path)
129
+ valid = strict_or_fast_and valid, validate_pattern(schema, data, errors, path)
110
130
  end
111
131
 
112
132
  valid
@@ -139,7 +159,7 @@ module JsonSchema
139
159
  # there is some performance implication to producing each sub error.
140
160
  # Normally we can short circuit the validation after encountering only
141
161
  # one problem, but here we have to evaluate all subschemas every time.
142
- if JsonSchema.configuration.all_of_sub_errors
162
+ if JsonSchema.configuration.all_of_sub_errors && !@fail_fast
143
163
  sub_errors = []
144
164
  valid = schema.all_of.map do |subschema|
145
165
  current_sub_errors = []
@@ -164,7 +184,10 @@ module JsonSchema
164
184
 
165
185
  sub_errors = schema.any_of.map do |subschema|
166
186
  current_sub_errors = []
167
- return true if validate_data(subschema, data, current_sub_errors, path)
187
+ valid = catch(:fail_fast) do
188
+ validate_data(subschema, data, current_sub_errors, path)
189
+ end
190
+ return true if valid
168
191
  current_sub_errors
169
192
  end
170
193
 
@@ -254,12 +277,12 @@ module JsonSchema
254
277
  valid = true
255
278
  if data.size > schema.items.count && schema.additional_items.is_a?(Schema)
256
279
  (schema.items.count..data.count - 1).each do |i|
257
- valid = strict_and valid,
280
+ valid = strict_or_fast_and valid,
258
281
  validate_data(schema.additional_items, data[i], errors, path + [i])
259
282
  end
260
283
  end
261
284
  schema.items.each_with_index do |subschema, i|
262
- valid = strict_and valid,
285
+ valid = strict_or_fast_and valid,
263
286
  validate_data(subschema, data[i], errors, path + [i])
264
287
  end
265
288
  valid
@@ -267,7 +290,7 @@ module JsonSchema
267
290
  else
268
291
  valid = true
269
292
  data.each_with_index do |value, i|
270
- valid = strict_and valid,
293
+ valid = strict_or_fast_and valid,
271
294
  validate_data(schema.items, value, errors, path + [i])
272
295
  end
273
296
  valid
@@ -411,7 +434,9 @@ module JsonSchema
411
434
 
412
435
  num_valid = schema.one_of.count do |subschema|
413
436
  current_sub_errors = []
414
- valid = validate_data(subschema, data, current_sub_errors, path)
437
+ valid = catch(:fail_fast) do
438
+ validate_data(subschema, data, current_sub_errors, path)
439
+ end
415
440
  sub_errors << current_sub_errors
416
441
  valid
417
442
  end
@@ -460,7 +485,7 @@ module JsonSchema
460
485
  schema.pattern_properties.each do |pattern, subschema|
461
486
  data.each do |key, value|
462
487
  if key =~ pattern
463
- valid = strict_and valid,
488
+ valid = strict_or_fast_and valid,
464
489
  validate_data(subschema, value, errors, path + [key])
465
490
  end
466
491
  end
@@ -472,10 +497,9 @@ module JsonSchema
472
497
  return true if schema.properties.empty?
473
498
  valid = true
474
499
  schema.properties.each do |key, subschema|
475
- if data.key?(key)
476
- valid = strict_and valid,
477
- validate_data(subschema, data[key], errors, path + [key])
478
- end
500
+ next unless data.key?(key)
501
+ valid = strict_or_fast_and valid,
502
+ validate_data(subschema, data[key], errors, path + [key])
479
503
  end
480
504
  valid
481
505
  end
@@ -496,7 +520,7 @@ module JsonSchema
496
520
  def validate_strict_properties(schema, data, errors, path)
497
521
  return true if !schema.strict_properties
498
522
 
499
- strict_and validate_extra(schema, data, errors, path),
523
+ strict_or_fast_and validate_extra(schema, data, errors, path),
500
524
  validate_required(schema, data, errors, path, schema.properties.keys)
501
525
  end
502
526
 
@@ -15,6 +15,16 @@ describe Commands::ValidateSchema do
15
15
  refute success
16
16
  end
17
17
 
18
+ it "runs successfully in fail fast mode" do
19
+ temp_file(basic_schema) do |path|
20
+ @command.fail_fast = true
21
+ success = @command.run([schema_path, path])
22
+ assert_equal [], @command.errors
23
+ assert_equal ["#{path} is valid."], @command.messages
24
+ assert success
25
+ end
26
+ end
27
+
18
28
  it "runs successfully in detect mode" do
19
29
  temp_file(basic_schema) do |path|
20
30
  @command.extra_schemas << schema_path
@@ -8,7 +8,7 @@ describe JsonSchema::Validator do
8
8
  end
9
9
 
10
10
  it "can find data valid" do
11
- assert validate
11
+ assert_valid
12
12
  end
13
13
 
14
14
  it "validates enum successfully" do
@@ -16,7 +16,7 @@ describe JsonSchema::Validator do
16
16
  "enum" => ["private", "public"]
17
17
  )
18
18
  data_sample["visibility"] = "public"
19
- assert validate
19
+ assert_valid
20
20
  end
21
21
 
22
22
  it "validates enum unsuccessfully" do
@@ -24,7 +24,7 @@ describe JsonSchema::Validator do
24
24
  "enum" => ["private", "public"]
25
25
  )
26
26
  data_sample["visibility"] = "personal"
27
- refute validate
27
+ refute_valid
28
28
  assert_includes error_messages,
29
29
  %{personal is not a member of ["private", "public"].}
30
30
  assert_includes error_types, :invalid_type
@@ -35,7 +35,7 @@ describe JsonSchema::Validator do
35
35
  "type" => ["object"]
36
36
  )
37
37
  @data_sample = { "name" => "cloudnasium" }
38
- assert validate
38
+ assert_valid
39
39
  end
40
40
 
41
41
  it "validates type unsuccessfully" do
@@ -43,7 +43,7 @@ describe JsonSchema::Validator do
43
43
  "type" => ["object"]
44
44
  )
45
45
  @data_sample = 4
46
- refute validate
46
+ refute_valid
47
47
  assert_includes error_messages, %{For 'definitions/app', 4 is not an object.}
48
48
  assert_includes error_types, :invalid_type
49
49
  assert_includes error_data, 4
@@ -54,7 +54,7 @@ describe JsonSchema::Validator do
54
54
  "type" => ["string"]
55
55
  )
56
56
  @data_sample = 4
57
- refute validate
57
+ refute_valid
58
58
  assert_includes error_messages, %{For 'definitions/app', 4 is not a string.}
59
59
  assert_includes error_types, :invalid_type
60
60
 
@@ -62,7 +62,7 @@ describe JsonSchema::Validator do
62
62
  "type" => ["string", "null"]
63
63
  )
64
64
  @data_sample = 4
65
- refute validate
65
+ refute_valid
66
66
  assert_includes error_messages, %{For 'definitions/app', 4 is not a string or null.}
67
67
  assert_includes error_types, :invalid_type
68
68
 
@@ -70,7 +70,7 @@ describe JsonSchema::Validator do
70
70
  "type" => ["object", "null", "string"]
71
71
  )
72
72
  @data_sample = 4
73
- refute validate
73
+ refute_valid
74
74
  assert_includes error_messages, %{For 'definitions/app', 4 is not an object, null, or string.}
75
75
  assert_includes error_types, :invalid_type
76
76
  end
@@ -82,7 +82,7 @@ describe JsonSchema::Validator do
82
82
  }
83
83
  )
84
84
  data_sample["flags"] = ["websockets"]
85
- assert validate
85
+ assert_valid
86
86
  end
87
87
 
88
88
  it "validates items with list unsuccessfully" do
@@ -92,7 +92,7 @@ describe JsonSchema::Validator do
92
92
  }
93
93
  )
94
94
  data_sample["flags"] = ["1337"]
95
- refute validate
95
+ refute_valid
96
96
  assert_includes error_messages,
97
97
  %{1337 does not match /^[a-z][a-z\\-]*[a-z]$/.}
98
98
  assert_includes error_types, :pattern_failed
@@ -107,7 +107,7 @@ describe JsonSchema::Validator do
107
107
  ]
108
108
  )
109
109
  data_sample["flags"] = ["cedar", "https"]
110
- assert validate
110
+ assert_valid
111
111
  end
112
112
 
113
113
  it "validates items with tuple with additionalItems boolean successfully" do
@@ -119,7 +119,7 @@ describe JsonSchema::Validator do
119
119
  ]
120
120
  )
121
121
  data_sample["flags"] = ["cedar", "https", "websockets"]
122
- assert validate
122
+ assert_valid
123
123
  end
124
124
 
125
125
  it "validates items with tuple with additionalItems boolean unsuccessfully" do
@@ -131,7 +131,7 @@ describe JsonSchema::Validator do
131
131
  ]
132
132
  )
133
133
  data_sample["flags"] = ["cedar", "https", "websockets"]
134
- refute validate
134
+ refute_valid
135
135
  assert_includes error_messages, %{No more than 2 items are allowed; 3 were supplied.}
136
136
  assert_includes error_types, :max_items_failed
137
137
  assert_includes error_data, ["cedar", "https", "websockets"]
@@ -146,7 +146,7 @@ describe JsonSchema::Validator do
146
146
  ]
147
147
  )
148
148
  data_sample["flags"] = ["cedar", "https", "websockets"]
149
- assert validate
149
+ assert_valid
150
150
  end
151
151
 
152
152
  it "validates items with tuple with additionalItems schema unsuccessfully for non-conforming additional item" do
@@ -158,7 +158,7 @@ describe JsonSchema::Validator do
158
158
  ]
159
159
  )
160
160
  data_sample["flags"] = ["cedar", "https", "websockets"]
161
- refute validate
161
+ refute_valid
162
162
  assert_includes error_messages,
163
163
  %{websockets is not a member of ["foo", "bar"].}
164
164
  assert_includes error_types, :invalid_type
@@ -174,7 +174,7 @@ describe JsonSchema::Validator do
174
174
  ]
175
175
  )
176
176
  data_sample["flags"] = ["cedar", "https", "websockets", "1337"]
177
- refute validate
177
+ refute_valid
178
178
  assert_includes error_messages,
179
179
  %{websockets is not a member of ["foo", "bar"].}
180
180
  assert_includes error_types, :invalid_type
@@ -194,7 +194,7 @@ describe JsonSchema::Validator do
194
194
  ]
195
195
  )
196
196
  data_sample["flags"] = ["cedar", "1337", "websockets"]
197
- refute validate
197
+ refute_valid
198
198
  assert_includes error_messages,
199
199
  %{websockets is not a member of ["foo", "bar"].}
200
200
  assert_includes error_types, :invalid_type
@@ -213,7 +213,7 @@ describe JsonSchema::Validator do
213
213
  ]
214
214
  )
215
215
  data_sample["flags"] = ["cedar"]
216
- refute validate
216
+ refute_valid
217
217
  assert_includes error_messages,
218
218
  %{2 items required; only 1 was supplied.}
219
219
  assert_includes error_types, :min_items_failed
@@ -229,7 +229,7 @@ describe JsonSchema::Validator do
229
229
  ]
230
230
  )
231
231
  data_sample["flags"] = ["cedar", "https", "websockets"]
232
- refute validate
232
+ refute_valid
233
233
  assert_includes error_messages,
234
234
  %{No more than 2 items are allowed; 3 were supplied.}
235
235
  assert_includes error_types, :max_items_failed
@@ -245,7 +245,7 @@ describe JsonSchema::Validator do
245
245
  ]
246
246
  )
247
247
  data_sample["flags"] = ["cedar", "1337"]
248
- refute validate
248
+ refute_valid
249
249
  assert_includes error_messages,
250
250
  %{1337 is not a member of ["http", "https"].}
251
251
  assert_includes error_types, :invalid_type
@@ -257,7 +257,7 @@ describe JsonSchema::Validator do
257
257
  "maxItems" => 10
258
258
  )
259
259
  data_sample["flags"] = (0...10).to_a
260
- assert validate
260
+ assert_valid
261
261
  end
262
262
 
263
263
  it "validates maxItems unsuccessfully" do
@@ -265,7 +265,7 @@ describe JsonSchema::Validator do
265
265
  "maxItems" => 10
266
266
  )
267
267
  data_sample["flags"] = (0...11).to_a
268
- refute validate
268
+ refute_valid
269
269
  assert_includes error_messages,
270
270
  %{No more than 10 items are allowed; 11 were supplied.}
271
271
  assert_includes error_types, :max_items_failed
@@ -277,7 +277,7 @@ describe JsonSchema::Validator do
277
277
  "minItems" => 1
278
278
  )
279
279
  data_sample["flags"] = ["websockets"]
280
- assert validate
280
+ assert_valid
281
281
  end
282
282
 
283
283
  it "validates minItems unsuccessfully" do
@@ -285,7 +285,7 @@ describe JsonSchema::Validator do
285
285
  "minItems" => 1
286
286
  )
287
287
  data_sample["flags"] = []
288
- refute validate
288
+ refute_valid
289
289
  assert_includes error_messages, %{1 item required; only 0 were supplied.}
290
290
  assert_includes error_types, :min_items_failed
291
291
  assert_includes error_data, []
@@ -296,7 +296,7 @@ describe JsonSchema::Validator do
296
296
  "uniqueItems" => true
297
297
  )
298
298
  data_sample["flags"] = ["websockets"]
299
- assert validate
299
+ assert_valid
300
300
  end
301
301
 
302
302
  it "validates uniqueItems unsuccessfully" do
@@ -304,7 +304,7 @@ describe JsonSchema::Validator do
304
304
  "uniqueItems" => true
305
305
  )
306
306
  data_sample["flags"] = ["websockets", "websockets"]
307
- refute validate
307
+ refute_valid
308
308
  assert_includes error_messages, %{Duplicate items are not allowed.}
309
309
  assert_includes error_types, :unique_items_failed
310
310
  assert_includes error_data, ["websockets", "websockets"]
@@ -316,7 +316,7 @@ describe JsonSchema::Validator do
316
316
  "maximum" => 10
317
317
  )
318
318
  data_sample["id"] = 11
319
- refute validate
319
+ refute_valid
320
320
  assert_includes error_messages, %{11 must be less than or equal to 10.}
321
321
  assert_includes error_types, :max_failed
322
322
  assert_includes error_data, 11
@@ -328,7 +328,7 @@ describe JsonSchema::Validator do
328
328
  "maximum" => 10
329
329
  )
330
330
  data_sample["id"] = 10
331
- refute validate
331
+ refute_valid
332
332
  assert_includes error_messages, %{10 must be less than 10.}
333
333
  assert_includes error_types, :max_failed
334
334
  end
@@ -339,7 +339,7 @@ describe JsonSchema::Validator do
339
339
  "maximum" => 10.0
340
340
  )
341
341
  data_sample["cost"] = 10.1
342
- refute validate
342
+ refute_valid
343
343
  assert_includes error_messages, %{10.1 must be less than or equal to 10.0.}
344
344
  assert_includes error_types, :max_failed
345
345
  end
@@ -350,7 +350,7 @@ describe JsonSchema::Validator do
350
350
  "maximum" => 10.0
351
351
  )
352
352
  data_sample["cost"] = 10.0
353
- refute validate
353
+ refute_valid
354
354
  assert_includes error_messages, %{10.0 must be less than 10.0.}
355
355
  assert_includes error_types, :max_failed
356
356
  end
@@ -361,7 +361,7 @@ describe JsonSchema::Validator do
361
361
  "minimum" => 1
362
362
  )
363
363
  data_sample["id"] = 0
364
- refute validate
364
+ refute_valid
365
365
  assert_includes error_messages, %{0 must be greater than or equal to 1.}
366
366
  assert_includes error_types, :min_failed
367
367
  assert_includes error_data, 0
@@ -373,7 +373,7 @@ describe JsonSchema::Validator do
373
373
  "minimum" => 1
374
374
  )
375
375
  data_sample["id"] = 1
376
- refute validate
376
+ refute_valid
377
377
  assert_includes error_messages, %{1 must be greater than 1.}
378
378
  end
379
379
 
@@ -383,7 +383,7 @@ describe JsonSchema::Validator do
383
383
  "minimum" => 0.0
384
384
  )
385
385
  data_sample["cost"] = -0.01
386
- refute validate
386
+ refute_valid
387
387
  assert_includes error_messages,
388
388
  %{-0.01 must be greater than or equal to 0.0.}
389
389
  assert_includes error_types, :min_failed
@@ -395,7 +395,7 @@ describe JsonSchema::Validator do
395
395
  "minimum" => 0.0
396
396
  )
397
397
  data_sample["cost"] = 0.0
398
- refute validate
398
+ refute_valid
399
399
  assert_includes error_messages, %{0.0 must be greater than 0.0.}
400
400
  assert_includes error_types, :min_failed
401
401
  end
@@ -405,7 +405,7 @@ describe JsonSchema::Validator do
405
405
  "multipleOf" => 2
406
406
  )
407
407
  data_sample["id"] = 1
408
- refute validate
408
+ refute_valid
409
409
  assert_includes error_messages, %{1 is not a multiple of 2.}
410
410
  assert_includes error_types, :multiple_of_failed
411
411
  assert_includes error_data, 1
@@ -416,7 +416,7 @@ describe JsonSchema::Validator do
416
416
  "multipleOf" => 0.01
417
417
  )
418
418
  data_sample["cost"] = 0.005
419
- refute validate
419
+ refute_valid
420
420
  assert_includes error_messages, %{0.005 is not a multiple of 0.01.}
421
421
  assert_includes error_types, :multiple_of_failed
422
422
  end
@@ -426,7 +426,7 @@ describe JsonSchema::Validator do
426
426
  "additionalProperties" => true
427
427
  )
428
428
  data_sample["foo"] = "bar"
429
- assert validate
429
+ assert_valid
430
430
  end
431
431
 
432
432
  it "validates additionalProperties boolean unsuccessfully" do
@@ -438,7 +438,7 @@ describe JsonSchema::Validator do
438
438
  )
439
439
  data_sample["foo"] = "bar"
440
440
  data_sample["matches_pattern"] = "yes!"
441
- refute validate
441
+ refute_valid
442
442
  assert_includes error_messages, %{"foo" is not a permitted key.}
443
443
  assert_includes error_types, :invalid_keys
444
444
  end
@@ -453,7 +453,7 @@ describe JsonSchema::Validator do
453
453
  data_sample["foo"] = "bar"
454
454
  data_sample["baz"] = "blah"
455
455
  data_sample["matches_pattern"] = "yes!"
456
- refute validate
456
+ refute_valid
457
457
  assert_includes error_messages, %{"baz", "foo" are not permitted keys.}
458
458
  assert_includes error_types, :invalid_keys
459
459
  end
@@ -465,7 +465,7 @@ describe JsonSchema::Validator do
465
465
  }
466
466
  )
467
467
  data_sample["foo"] = true
468
- assert validate
468
+ assert_valid
469
469
  end
470
470
 
471
471
  it "validates additionalProperties schema unsuccessfully" do
@@ -479,7 +479,7 @@ describe JsonSchema::Validator do
479
479
  )
480
480
  data_sample["foo"] = 4
481
481
  data_sample["matches_pattern"] = "yes!"
482
- refute validate
482
+ refute_valid
483
483
  assert_includes error_messages, %{For 'additionalProperties', 4 is not a boolean.}
484
484
  assert_includes error_types, :invalid_type
485
485
  end
@@ -489,7 +489,7 @@ describe JsonSchema::Validator do
489
489
  "production" => "ssl"
490
490
  )
491
491
  data_sample["production"] = true
492
- refute validate
492
+ refute_valid
493
493
  assert_includes error_messages,
494
494
  %{"ssl" wasn't supplied.}
495
495
  end
@@ -506,7 +506,7 @@ describe JsonSchema::Validator do
506
506
  )
507
507
  data_sample["cost"] = 10.0
508
508
  data_sample["ssl"] = true
509
- refute validate
509
+ refute_valid
510
510
  assert_includes error_messages, %{10.0 must be greater than or equal to 20.0.}
511
511
  assert_includes error_types, :min_failed
512
512
  end
@@ -516,7 +516,7 @@ describe JsonSchema::Validator do
516
516
  "maxProperties" => 0
517
517
  )
518
518
  data_sample["name"] = "cloudnasium"
519
- refute validate
519
+ refute_valid
520
520
  assert_includes error_messages, %{No more than 0 properties are allowed; 1 was supplied.}
521
521
  assert_includes error_types, :max_properties_failed
522
522
  assert_includes error_data, { "name" => "cloudnasium" }
@@ -527,7 +527,7 @@ describe JsonSchema::Validator do
527
527
  "minProperties" => 2
528
528
  )
529
529
  data_sample["name"] = "cloudnasium"
530
- refute validate
530
+ refute_valid
531
531
  assert_includes error_messages, %{At least 2 properties are required; 1 was supplied.}
532
532
  assert_includes error_types, :min_properties_failed
533
533
  assert_includes error_data, { "name" => "cloudnasium" }
@@ -545,7 +545,7 @@ describe JsonSchema::Validator do
545
545
  "" => 123,
546
546
  "KEY" => 456
547
547
  }
548
- refute validate
548
+ refute_valid
549
549
  assert_includes error_messages, %{For 'definitions/config_vars', 456 is not a null or string.}
550
550
  assert_includes error_types, :invalid_type
551
551
  end
@@ -563,7 +563,7 @@ describe JsonSchema::Validator do
563
563
  "required" => ["name"]
564
564
  )
565
565
  data_sample.delete("name")
566
- refute validate
566
+ refute_valid
567
567
  assert_includes error_messages, %{"name" wasn't supplied.}
568
568
  assert_includes error_types, :required_failed
569
569
  assert_includes error_data, ["name"]
@@ -573,7 +573,7 @@ describe JsonSchema::Validator do
573
573
  pointer("#/definitions/app").merge!(
574
574
  "strictProperties" => false
575
575
  )
576
- assert validate
576
+ assert_valid
577
577
  end
578
578
 
579
579
  it "validates strictProperties unsuccessfully" do
@@ -585,7 +585,7 @@ describe JsonSchema::Validator do
585
585
  )
586
586
  data_sample["extra_key"] = "value"
587
587
  data_sample["matches_pattern"] = "yes!"
588
- refute validate
588
+ refute_valid
589
589
  missing = @schema.properties.keys.sort - ["name"]
590
590
  assert_includes error_messages, %{"#{missing.join('", "')}" weren't supplied.}
591
591
  assert_includes error_messages, %{"extra_key" is not a permitted key.}
@@ -600,7 +600,7 @@ describe JsonSchema::Validator do
600
600
  ]
601
601
  )
602
602
  data_sample["contrived"] = "ab"
603
- refute validate
603
+ refute_valid
604
604
  assert_includes error_messages, %{Not all subschemas of "allOf" matched.}
605
605
  assert_includes error_types, :all_of_failed
606
606
  end
@@ -613,7 +613,7 @@ describe JsonSchema::Validator do
613
613
  ]
614
614
  )
615
615
  data_sample["contrived"] = "ab"
616
- refute validate
616
+ refute_valid
617
617
  assert_includes error_messages, %{At least 3 characters are required; only 2 were supplied.}
618
618
  assert_includes error_data, "ab"
619
619
  end
@@ -629,7 +629,7 @@ describe JsonSchema::Validator do
629
629
  ]
630
630
  )
631
631
  data_sample["contrived"] = "ab"
632
- refute validate
632
+ refute_valid
633
633
  assert_includes error_messages, %{Not all subschemas of "allOf" matched.}
634
634
  assert_includes error_types, :all_of_failed
635
635
  all_of_error = @validator.errors.find { |error| error.type == :all_of_failed }
@@ -649,7 +649,7 @@ describe JsonSchema::Validator do
649
649
  ]
650
650
  )
651
651
  data_sample["contrived"] = "ab"
652
- refute validate
652
+ refute_valid
653
653
  assert_includes error_messages, %{No subschema in "anyOf" matched.}
654
654
  assert_includes error_types, :any_of_failed
655
655
  any_of_error = @validator.errors.find { |error| error.type == :any_of_failed }
@@ -670,7 +670,7 @@ describe JsonSchema::Validator do
670
670
  ]
671
671
  )
672
672
  data_sample["contrived"] = "foo"
673
- refute validate
673
+ refute_valid
674
674
  assert_includes error_messages, %{More than one subschema in "oneOf" matched.}
675
675
  assert_includes error_types, :one_of_failed
676
676
  one_of_error = @validator.errors.find { |error| error.type == :one_of_failed }
@@ -686,7 +686,7 @@ describe JsonSchema::Validator do
686
686
  "not" => { "pattern" => "^$" }
687
687
  )
688
688
  data_sample["contrived"] = ""
689
- refute validate
689
+ refute_valid
690
690
  assert_includes error_messages, %{Matched "not" subschema.}
691
691
  assert_includes error_types, :not_failed
692
692
  assert_includes error_data, ""
@@ -697,7 +697,7 @@ describe JsonSchema::Validator do
697
697
  "format" => "date"
698
698
  )
699
699
  data_sample["owner"] = "2014-05-13"
700
- assert validate
700
+ assert_valid
701
701
  end
702
702
 
703
703
  it "validates date format unsuccessfully" do
@@ -705,7 +705,7 @@ describe JsonSchema::Validator do
705
705
  "format" => "date"
706
706
  )
707
707
  data_sample["owner"] = "13/05/2014"
708
- refute validate
708
+ refute_valid
709
709
  end
710
710
 
711
711
  it "validates date-time format successfully" do
@@ -713,7 +713,7 @@ describe JsonSchema::Validator do
713
713
  "format" => "date-time"
714
714
  )
715
715
  data_sample["owner"] = "2014-05-13T08:42:40Z"
716
- assert validate
716
+ assert_valid
717
717
  end
718
718
 
719
719
  it "validates date-time format with time zone successfully" do
@@ -721,7 +721,7 @@ describe JsonSchema::Validator do
721
721
  "format" => "date-time"
722
722
  )
723
723
  data_sample["owner"] = "2014-05-13T08:42:40-00:00"
724
- assert validate
724
+ assert_valid
725
725
  end
726
726
 
727
727
  it "validates date-time format with time fraction successfully" do
@@ -729,7 +729,7 @@ describe JsonSchema::Validator do
729
729
  "format" => "date-time"
730
730
  )
731
731
  data_sample["owner"] = "2014-05-13T08:42:40.444Z"
732
- assert validate
732
+ assert_valid
733
733
  end
734
734
 
735
735
  it "validates date-time format unsuccessfully" do
@@ -737,7 +737,7 @@ describe JsonSchema::Validator do
737
737
  "format" => "date-time"
738
738
  )
739
739
  data_sample["owner"] = "2014-05-13T08:42:40"
740
- refute validate
740
+ refute_valid
741
741
  assert_includes error_messages, %{2014-05-13T08:42:40 is not a valid date-time.}
742
742
  assert_includes error_types, :invalid_format
743
743
  assert_includes error_data, "2014-05-13T08:42:40"
@@ -748,7 +748,7 @@ describe JsonSchema::Validator do
748
748
  "format" => "email"
749
749
  )
750
750
  data_sample["owner"] = "dwarf@example.com"
751
- assert validate
751
+ assert_valid
752
752
  end
753
753
 
754
754
  it "validates email format with long TLDs successfully" do
@@ -756,7 +756,7 @@ describe JsonSchema::Validator do
756
756
  "format" => "email"
757
757
  )
758
758
  data_sample["owner"] = "dwarf@example.technology"
759
- assert validate
759
+ assert_valid
760
760
  end
761
761
 
762
762
  it "validates email format unsuccessfully" do
@@ -764,7 +764,7 @@ describe JsonSchema::Validator do
764
764
  "format" => "email"
765
765
  )
766
766
  data_sample["owner"] = "@example.com"
767
- refute validate
767
+ refute_valid
768
768
  assert_includes error_messages, %{@example.com is not a valid email.}
769
769
  assert_includes error_types, :invalid_format
770
770
  end
@@ -774,7 +774,7 @@ describe JsonSchema::Validator do
774
774
  "format" => "hostname"
775
775
  )
776
776
  data_sample["owner"] = "example.com"
777
- assert validate
777
+ assert_valid
778
778
  end
779
779
 
780
780
  it "validates hostname format unsuccessfully" do
@@ -782,7 +782,7 @@ describe JsonSchema::Validator do
782
782
  "format" => "hostname"
783
783
  )
784
784
  data_sample["owner"] = "@example.com"
785
- refute validate
785
+ refute_valid
786
786
  assert_includes error_messages, %{@example.com is not a valid hostname.}
787
787
  assert_includes error_types, :invalid_format
788
788
  end
@@ -792,7 +792,7 @@ describe JsonSchema::Validator do
792
792
  "format" => "ipv4"
793
793
  )
794
794
  data_sample["owner"] = "1.2.3.4"
795
- assert validate
795
+ assert_valid
796
796
  end
797
797
 
798
798
  it "validates ipv4 format unsuccessfully" do
@@ -800,7 +800,7 @@ describe JsonSchema::Validator do
800
800
  "format" => "ipv4"
801
801
  )
802
802
  data_sample["owner"] = "1.2.3.4.5"
803
- refute validate
803
+ refute_valid
804
804
  assert_includes error_messages, %{1.2.3.4.5 is not a valid ipv4.}
805
805
  assert_includes error_types, :invalid_format
806
806
  end
@@ -810,7 +810,7 @@ describe JsonSchema::Validator do
810
810
  "format" => "ipv6"
811
811
  )
812
812
  data_sample["owner"] = "1::3:4:5:6:7:8"
813
- assert validate
813
+ assert_valid
814
814
  end
815
815
 
816
816
  it "validates ipv6 format unsuccessfully" do
@@ -818,7 +818,7 @@ describe JsonSchema::Validator do
818
818
  "format" => "ipv6"
819
819
  )
820
820
  data_sample["owner"] = "1::3:4:5:6:7:8:9"
821
- refute validate
821
+ refute_valid
822
822
  assert_includes error_messages, %{1::3:4:5:6:7:8:9 is not a valid ipv6.}
823
823
  assert_includes error_types, :invalid_format
824
824
  end
@@ -828,7 +828,7 @@ describe JsonSchema::Validator do
828
828
  "format" => "regex"
829
829
  )
830
830
  data_sample["owner"] = "^owner@heroku\.com$"
831
- assert validate
831
+ assert_valid
832
832
  end
833
833
 
834
834
  it "validates regex format successfully" do
@@ -836,7 +836,7 @@ describe JsonSchema::Validator do
836
836
  "format" => "regex"
837
837
  )
838
838
  data_sample["owner"] = "^owner($"
839
- refute validate
839
+ refute_valid
840
840
  assert_includes error_messages, %{^owner($ is not a valid regex.}
841
841
  assert_includes error_types, :invalid_format
842
842
  end
@@ -846,7 +846,7 @@ describe JsonSchema::Validator do
846
846
  "format" => "uri"
847
847
  )
848
848
  data_sample["owner"] = "https://example.com"
849
- assert validate
849
+ assert_valid
850
850
  end
851
851
 
852
852
  it "validates relative uri format successfully" do
@@ -854,7 +854,7 @@ describe JsonSchema::Validator do
854
854
  "format" => "uri"
855
855
  )
856
856
  data_sample["owner"] = "schemata/app"
857
- assert validate
857
+ assert_valid
858
858
  end
859
859
 
860
860
  it "validates uri format unsuccessfully" do
@@ -862,7 +862,7 @@ describe JsonSchema::Validator do
862
862
  "format" => "uri"
863
863
  )
864
864
  data_sample["owner"] = "http://example.com[]"
865
- refute validate
865
+ refute_valid
866
866
  assert_includes error_messages, %{http://example.com[] is not a valid uri.}
867
867
  assert_includes error_types, :invalid_format
868
868
  end
@@ -872,7 +872,7 @@ describe JsonSchema::Validator do
872
872
  "format" => "uri-reference"
873
873
  )
874
874
  data_sample["owner"] = "https://example.com"
875
- assert validate
875
+ assert_valid
876
876
  end
877
877
 
878
878
  it "validates relative uri format successfully" do
@@ -880,7 +880,7 @@ describe JsonSchema::Validator do
880
880
  "format" => "uri"
881
881
  )
882
882
  data_sample["owner"] = "#hello"
883
- assert validate
883
+ assert_valid
884
884
  end
885
885
 
886
886
  it "validates uri format unsuccessfully" do
@@ -888,7 +888,7 @@ describe JsonSchema::Validator do
888
888
  "format" => "uri-reference"
889
889
  )
890
890
  data_sample["owner"] = "http://example.com[]"
891
- refute validate
891
+ refute_valid
892
892
  assert_includes error_messages, %{http://example.com[] is not a valid uri-reference.}
893
893
  assert_includes error_types, :invalid_format
894
894
  end
@@ -898,7 +898,7 @@ describe JsonSchema::Validator do
898
898
  "format" => "uuid"
899
899
  )
900
900
  data_sample["owner"] = "01234567-89ab-cdef-0123-456789abcdef"
901
- assert validate
901
+ assert_valid
902
902
  end
903
903
 
904
904
  it "validates uuid format unsuccessfully" do
@@ -906,7 +906,7 @@ describe JsonSchema::Validator do
906
906
  "format" => "uuid"
907
907
  )
908
908
  data_sample["owner"] = "123"
909
- refute validate
909
+ refute_valid
910
910
  assert_includes error_messages, %{123 is not a valid uuid.}
911
911
  assert_includes error_types, :invalid_format
912
912
  end
@@ -916,7 +916,7 @@ describe JsonSchema::Validator do
916
916
  "maxLength" => 3
917
917
  )
918
918
  data_sample["name"] = "abcd"
919
- refute validate
919
+ refute_valid
920
920
  assert_includes error_messages, %{Only 3 characters are allowed; 4 were supplied.}
921
921
  assert_includes error_types, :max_length_failed
922
922
  end
@@ -926,7 +926,7 @@ describe JsonSchema::Validator do
926
926
  "minLength" => 3
927
927
  )
928
928
  data_sample["name"] = "ab"
929
- refute validate
929
+ refute_valid
930
930
  assert_includes error_messages, %{At least 3 characters are required; only 2 were supplied.}
931
931
  assert_includes error_types, :min_length_failed
932
932
  end
@@ -936,7 +936,7 @@ describe JsonSchema::Validator do
936
936
  "pattern" => "^[a-z][a-z0-9-]{3,30}$",
937
937
  )
938
938
  data_sample["name"] = "ab"
939
- refute validate
939
+ refute_valid
940
940
  assert_includes error_messages, %{ab does not match /^[a-z][a-z0-9-]{3,30}$/.}
941
941
  assert_includes error_types, :pattern_failed
942
942
  assert_includes error_data, "ab"
@@ -947,7 +947,7 @@ describe JsonSchema::Validator do
947
947
  "enum" => ["private", "public"]
948
948
  )
949
949
  data_sample["visibility"] = "personal"
950
- refute validate
950
+ refute_valid
951
951
  assert_equal "#/visibility", @validator.errors[0].pointer
952
952
  end
953
953
 
@@ -957,7 +957,7 @@ describe JsonSchema::Validator do
957
957
  "not" => { "$ref" => "#/definitions/app" }
958
958
  )
959
959
  data_sample["visibility"] = "personal"
960
- refute validate
960
+ refute_valid
961
961
  assert_includes error_messages, %{Validation loop detected.}
962
962
  end
963
963
  =end
@@ -970,7 +970,7 @@ describe JsonSchema::Validator do
970
970
  "format" => "the-answer"
971
971
  )
972
972
  data_sample["owner"] = "42"
973
- assert validate
973
+ assert_valid
974
974
  end
975
975
 
976
976
  it "validates custom formats unsuccessfully" do
@@ -981,7 +981,7 @@ describe JsonSchema::Validator do
981
981
  "format" => "the-answer"
982
982
  )
983
983
  data_sample["owner"] = "43"
984
- refute validate
984
+ refute_valid
985
985
  assert_includes error_messages, %{43 is not a valid the-answer.}
986
986
  assert_includes error_types, :invalid_format
987
987
  end
@@ -1043,11 +1043,26 @@ describe JsonSchema::Validator do
1043
1043
  @schema_sample ||= DataScaffold.schema_sample
1044
1044
  end
1045
1045
 
1046
- def validate
1046
+ def validator
1047
1047
  @schema = JsonSchema.parse!(schema_sample)
1048
1048
  @schema.expand_references!
1049
1049
  @schema = @schema.definitions["app"]
1050
- @validator = JsonSchema::Validator.new(@schema)
1051
- @validator.validate(data_sample)
1050
+ JsonSchema::Validator.new(@schema)
1051
+ end
1052
+
1053
+ # assert_valid asserts that both the "fail fast" and the "full error messages"
1054
+ # code paths consider the data sample valid for the set schema.
1055
+ def assert_valid
1056
+ @validator = validator
1057
+ assert @validator.validate(data_sample, fail_fast: true)
1058
+ assert @validator.validate(data_sample, fail_fast: false)
1059
+ end
1060
+
1061
+ # refute_valid asserts that both the "fail fast" and the "full error messages"
1062
+ # code paths consider the data sample erroneous for the set schema.
1063
+ def refute_valid
1064
+ @validator = validator
1065
+ refute @validator.validate(data_sample, fail_fast: true)
1066
+ refute @validator.validate(data_sample, fail_fast: false)
1052
1067
  end
1053
1068
  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.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-26 00:00:00.000000000 Z
11
+ date: 2018-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecma-re-validator