json_schema 0.9.0 → 0.10.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/README.md +6 -4
- data/lib/json_schema/configuration.rb +12 -1
- data/lib/json_schema/parser.rb +5 -3
- data/lib/json_schema/validator.rb +17 -21
- data/test/json_schema/parser_test.rb +22 -3
- data/test/json_schema/validator_test.rb +28 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8dd8c031b8ddc8eab131da6a1d5872b8c3b3ac06
|
4
|
+
data.tar.gz: ee2287a4e2a950024e987364afcd6ee0488b0321
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9a4e08cc35055843d3ac60d02797042258b513941ff965f653739f7f05dd4361127d83b3c1a507873b04931d2fdb3ce9565eb7c8c379e2dc4739ec48e67cf22
|
7
|
+
data.tar.gz: 3ddcf6d896d86616deb12746482cfdb4fd30666dbf55f0af7b1947d5dd93a4e3148b45be581db1107117d91ff9eff285529d0b0184d2cb3e6fbf71003b81ef7a
|
data/README.md
CHANGED
@@ -57,8 +57,10 @@ ruby -Ilib -Itest test/json_schema/validator_test.rb -n /anyOf/
|
|
57
57
|
|
58
58
|
## Release
|
59
59
|
|
60
|
-
|
60
|
+
1. Update the version in `json_schema.gemspec` as appropriate for [semantic
|
61
|
+
versioning](http://semver.org).
|
62
|
+
2. Run the `release` task:
|
61
63
|
|
62
|
-
```
|
63
|
-
bundle exec rake release
|
64
|
-
```
|
64
|
+
```
|
65
|
+
bundle exec rake release
|
66
|
+
```
|
@@ -1,15 +1,26 @@
|
|
1
1
|
module JsonSchema
|
2
2
|
class Configuration
|
3
|
+
attr_reader :custom_formats
|
3
4
|
attr_reader :validate_regex_with
|
4
5
|
|
5
6
|
def validate_regex_with=(validator)
|
6
7
|
@validate_regex_with = validator
|
7
8
|
end
|
8
9
|
|
10
|
+
def register_format(name, validator_proc)
|
11
|
+
@custom_formats[name] = validator_proc
|
12
|
+
end
|
13
|
+
|
14
|
+
# Used for testing.
|
15
|
+
def reset!
|
16
|
+
@validate_regex_with = nil
|
17
|
+
@custom_formats = {}
|
18
|
+
end
|
19
|
+
|
9
20
|
private
|
10
21
|
|
11
22
|
def initialize
|
12
|
-
|
23
|
+
reset!
|
13
24
|
end
|
14
25
|
|
15
26
|
end
|
data/lib/json_schema/parser.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require_relative "../json_reference"
|
2
|
+
require_relative "validator"
|
2
3
|
|
3
4
|
module JsonSchema
|
4
5
|
class Parser
|
5
6
|
ALLOWED_TYPES = %w{any array boolean integer number null object string}
|
6
7
|
BOOLEAN = [FalseClass, TrueClass]
|
7
|
-
FORMATS =
|
8
|
+
FORMATS = JsonSchema::Validator::DEFAULT_FORMAT_VALIDATORS.keys
|
8
9
|
FRIENDLY_TYPES = {
|
9
10
|
Array => "array",
|
10
11
|
FalseClass => "boolean",
|
@@ -346,9 +347,10 @@ module JsonSchema
|
|
346
347
|
end
|
347
348
|
|
348
349
|
def validate_format(schema, format)
|
349
|
-
|
350
|
+
valid_formats = FORMATS + JsonSchema.configuration.custom_formats.keys
|
351
|
+
return if valid_formats.include?(format)
|
350
352
|
|
351
|
-
message = %{#{format.inspect} is not a valid format, must be one of #{
|
353
|
+
message = %{#{format.inspect} is not a valid format, must be one of #{valid_formats.join(', ')}.}
|
352
354
|
@errors << SchemaError.new(schema, message, :unknown_format)
|
353
355
|
end
|
354
356
|
end
|
@@ -184,27 +184,11 @@ module JsonSchema
|
|
184
184
|
|
185
185
|
def validate_format(schema, data, errors, path)
|
186
186
|
return true unless schema.format
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
when "email"
|
193
|
-
data =~ EMAIL_PATTERN
|
194
|
-
when "hostname"
|
195
|
-
data =~ HOSTNAME_PATTERN
|
196
|
-
when "ipv4"
|
197
|
-
data =~ IPV4_PATTERN
|
198
|
-
when "ipv6"
|
199
|
-
data =~ IPV6_PATTERN
|
200
|
-
when "regex"
|
201
|
-
Regexp.new(data) rescue false
|
202
|
-
when "uri"
|
203
|
-
URI.parse(data) rescue false
|
204
|
-
when "uuid"
|
205
|
-
data =~ UUID_PATTERN
|
206
|
-
end
|
207
|
-
if valid
|
187
|
+
validator = (
|
188
|
+
JsonSchema.configuration.custom_formats[schema.format] ||
|
189
|
+
DEFAULT_FORMAT_VALIDATORS[schema.format]
|
190
|
+
)
|
191
|
+
if validator[data]
|
208
192
|
true
|
209
193
|
else
|
210
194
|
message = %{#{data} is not a valid #{schema.format}.}
|
@@ -543,6 +527,18 @@ module JsonSchema
|
|
543
527
|
key || fragment
|
544
528
|
end
|
545
529
|
|
530
|
+
DEFAULT_FORMAT_VALIDATORS = {
|
531
|
+
"date" => ->(data) { data =~ DATE_PATTERN },
|
532
|
+
"date-time" => ->(data) { data =~ DATE_TIME_PATTERN },
|
533
|
+
"email" => ->(data) { data =~ EMAIL_PATTERN },
|
534
|
+
"hostname" => ->(data) { data =~ HOSTNAME_PATTERN },
|
535
|
+
"ipv4" => ->(data) { data =~ IPV4_PATTERN },
|
536
|
+
"ipv6" => ->(data) { data =~ IPV6_PATTERN },
|
537
|
+
"regex" => ->(data) { Regexp.new(data) rescue false },
|
538
|
+
"uri" => ->(data) { URI.parse(data) rescue false },
|
539
|
+
"uuid" => ->(data) { data =~ UUID_PATTERN },
|
540
|
+
}.freeze
|
541
|
+
|
546
542
|
EMAIL_PATTERN = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+$/i
|
547
543
|
|
548
544
|
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])?)*\.?$/
|
@@ -4,9 +4,7 @@ require "json_schema"
|
|
4
4
|
|
5
5
|
describe JsonSchema::Parser do
|
6
6
|
after do
|
7
|
-
JsonSchema.
|
8
|
-
c.validate_regex_with = nil
|
9
|
-
end
|
7
|
+
JsonSchema.configuration.reset!
|
10
8
|
end
|
11
9
|
|
12
10
|
it "parses the basic attributes of a schema" do
|
@@ -288,6 +286,27 @@ describe JsonSchema::Parser do
|
|
288
286
|
assert_includes error_types, :regex_failed
|
289
287
|
end
|
290
288
|
|
289
|
+
it "parses custom formats" do
|
290
|
+
JsonSchema.configure do |c|
|
291
|
+
c.register_format 'the-answer', ->(data) { data.to_i == 42 }
|
292
|
+
end
|
293
|
+
schema_sample["format"] = "the-answer"
|
294
|
+
assert parse
|
295
|
+
end
|
296
|
+
|
297
|
+
it "rejects bad formats even when there are custom formats defined" do
|
298
|
+
JsonSchema.configure do |c|
|
299
|
+
c.register_format "the-answer", ->(data) { data.to_i == 42 }
|
300
|
+
end
|
301
|
+
schema_sample["format"] = "not-a-format"
|
302
|
+
refute parse
|
303
|
+
assert_includes error_messages, '"not-a-format" is not a valid format, ' \
|
304
|
+
'must be one of date, date-time, email, ' \
|
305
|
+
'hostname, ipv4, ipv6, regex, uri, uuid, ' \
|
306
|
+
'the-answer.'
|
307
|
+
assert_includes error_types, :unknown_format
|
308
|
+
end
|
309
|
+
|
291
310
|
def error_messages
|
292
311
|
@parser.errors.map { |e| e.message }
|
293
312
|
end
|
@@ -3,6 +3,10 @@ require "test_helper"
|
|
3
3
|
require "json_schema"
|
4
4
|
|
5
5
|
describe JsonSchema::Validator do
|
6
|
+
after do
|
7
|
+
JsonSchema.configuration.reset!
|
8
|
+
end
|
9
|
+
|
6
10
|
it "can find data valid" do
|
7
11
|
assert validate
|
8
12
|
end
|
@@ -813,6 +817,30 @@ describe JsonSchema::Validator do
|
|
813
817
|
end
|
814
818
|
=end
|
815
819
|
|
820
|
+
it "validates custom formats successfully" do
|
821
|
+
JsonSchema.configure do |c|
|
822
|
+
c.register_format "the-answer", ->(data) { data.to_i == 42 }
|
823
|
+
end
|
824
|
+
pointer("#/definitions/app/definitions/owner").merge!(
|
825
|
+
"format" => "the-answer"
|
826
|
+
)
|
827
|
+
data_sample["owner"] = "42"
|
828
|
+
assert validate
|
829
|
+
end
|
830
|
+
|
831
|
+
it "validates custom formats unsuccessfully" do
|
832
|
+
JsonSchema.configure do |c|
|
833
|
+
c.register_format "the-answer", ->(data) { data.to_i == 42 }
|
834
|
+
end
|
835
|
+
pointer("#/definitions/app/definitions/owner").merge!(
|
836
|
+
"format" => "the-answer"
|
837
|
+
)
|
838
|
+
data_sample["owner"] = "43"
|
839
|
+
refute validate
|
840
|
+
assert_includes error_messages, %{43 is not a valid the-answer.}
|
841
|
+
assert_includes error_types, :invalid_format
|
842
|
+
end
|
843
|
+
|
816
844
|
def data_sample
|
817
845
|
@data_sample ||= DataScaffold.data_sample
|
818
846
|
end
|