json_schema 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_schema/parser.rb +9 -0
- data/lib/json_schema/validator.rb +4 -0
- data/test/json_schema/parser_test.rb +9 -0
- data/test/json_schema/validator_test.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fd4983a9632b29cb79ed96ceba036d590e74719
|
4
|
+
data.tar.gz: 5a819846b5ef11f8be6a5e5e330be6e0de2bc5d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f61c5a056f07c791252e663b82973468b11b72a6dc9b7e2998c236c0bf3babe49865e8c4a081c29d9a6e79082c12c3c4e62dda67ebed2c4c5c494a95e31b2361
|
7
|
+
data.tar.gz: 742206a63613cc097969f0a3ef429c7c3d5cdc22305e0115e8bb9ff1d90062a5ba605474361184eea2edf058bdb1ae65997cda3a5425b42781bafaca9fd14558
|
data/lib/json_schema/parser.rb
CHANGED
@@ -4,6 +4,7 @@ module JsonSchema
|
|
4
4
|
class Parser
|
5
5
|
ALLOWED_TYPES = %w{any array boolean integer number null object string}
|
6
6
|
BOOLEAN = [FalseClass, TrueClass]
|
7
|
+
FORMATS = %w{date date-time email hostname ipv4 ipv6 regex uri uuid}
|
7
8
|
FRIENDLY_TYPES = {
|
8
9
|
Array => "array",
|
9
10
|
FalseClass => "boolean",
|
@@ -287,6 +288,7 @@ module JsonSchema
|
|
287
288
|
schema.min_length = validate_type(schema, [Integer], "minLength")
|
288
289
|
schema.pattern = validate_type(schema, [String], "pattern")
|
289
290
|
schema.pattern = Regexp.new(schema.pattern) if schema.pattern
|
291
|
+
validate_format(schema, schema.format) if schema.format
|
290
292
|
|
291
293
|
# hyperschema
|
292
294
|
schema.links = validate_type(schema, [Array], "links")
|
@@ -331,5 +333,12 @@ module JsonSchema
|
|
331
333
|
value
|
332
334
|
end
|
333
335
|
end
|
336
|
+
|
337
|
+
def validate_format(schema, format)
|
338
|
+
return if FORMATS.include?(format)
|
339
|
+
|
340
|
+
message = %{#{format.inspect} is not a valid format, must be one of #{FORMATS.join(', ')}.}
|
341
|
+
@errors << SchemaError.new(schema, message, :unknown_format)
|
342
|
+
end
|
334
343
|
end
|
335
344
|
end
|
@@ -177,6 +177,8 @@ module JsonSchema
|
|
177
177
|
def validate_format(schema, data, errors, path)
|
178
178
|
return true unless schema.format
|
179
179
|
valid = case schema.format
|
180
|
+
when "date"
|
181
|
+
data =~ DATE_PATTERN
|
180
182
|
when "date-time"
|
181
183
|
data =~ DATE_TIME_PATTERN
|
182
184
|
when "email"
|
@@ -510,6 +512,8 @@ module JsonSchema
|
|
510
512
|
|
511
513
|
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])?)*\.?$/
|
512
514
|
|
515
|
+
DATE_PATTERN = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
|
516
|
+
|
513
517
|
DATE_TIME_PATTERN = /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-2][0-9]:[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|[\-+][0-9]{2}:[0-5][0-9])$/
|
514
518
|
|
515
519
|
# from: http://stackoverflow.com/a/17871737
|
@@ -257,6 +257,15 @@ describe JsonSchema::Parser do
|
|
257
257
|
assert_includes error_types, :unknown_type
|
258
258
|
end
|
259
259
|
|
260
|
+
it "errors on unknown formats" do
|
261
|
+
schema_sample["format"] = "obscure-thing"
|
262
|
+
refute parse
|
263
|
+
assert_includes error_messages, '"obscure-thing" is not a valid format, ' \
|
264
|
+
'must be one of date, date-time, email, ' \
|
265
|
+
'hostname, ipv4, ipv6, regex, uri, uuid.'
|
266
|
+
assert_includes error_types, :unknown_format
|
267
|
+
end
|
268
|
+
|
260
269
|
def error_messages
|
261
270
|
@parser.errors.map { |e| e.message }
|
262
271
|
end
|
@@ -506,6 +506,22 @@ describe JsonSchema::Validator do
|
|
506
506
|
assert_includes error_types, :not_failed
|
507
507
|
end
|
508
508
|
|
509
|
+
it "validates date format successfully" do
|
510
|
+
pointer("#/definitions/app/definitions/owner").merge!(
|
511
|
+
"format" => "date"
|
512
|
+
)
|
513
|
+
data_sample["owner"] = "2014-05-13"
|
514
|
+
assert validate
|
515
|
+
end
|
516
|
+
|
517
|
+
it "validates date format unsuccessfully" do
|
518
|
+
pointer("#/definitions/app/definitions/owner").merge!(
|
519
|
+
"format" => "date"
|
520
|
+
)
|
521
|
+
data_sample["owner"] = "13/05/2014"
|
522
|
+
refute validate
|
523
|
+
end
|
524
|
+
|
509
525
|
it "validates date-time format successfully" do
|
510
526
|
pointer("#/definitions/app/definitions/owner").merge!(
|
511
527
|
"format" => "date-time"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|