json_schemer 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -9
- data/json_schemer.gemspec +7 -1
- data/lib/json_schemer.rb +28 -19
- data/lib/json_schemer/format.rb +61 -0
- data/lib/json_schemer/version.rb +1 -1
- metadata +3 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f44b977f6f07e9dfbb34d58681bc0b22059143bc761364b8c8edb81b5ba7fb16
|
4
|
+
data.tar.gz: f606d14d205b33bb4f1c221cb6526e79afee01d965d61883763c8f516fabd69e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 046d14ee062722084b3b7954c488b6e86ba89b5ba112879b53ecf173c3c969d183ec4baa381c2f30eca61f6595fbe199337cff5ecfbb4eb7226d1f662d4b80e9
|
7
|
+
data.tar.gz: 684807637dc7510c868fc173716a1846c35368db792bd8fa1a8e2de605d3e58901df668b451567610294bd989ea057a6fb28e02bd59c2bd4d1df0b4f7571d629
|
data/Gemfile.lock
CHANGED
@@ -1,27 +1,19 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
json_schemer (0.1.
|
4
|
+
json_schemer (0.1.1)
|
5
5
|
ecma-re-validator (~> 0.1.2)
|
6
6
|
hana (~> 1.3.3)
|
7
|
-
rdf (~> 3.0.1)
|
8
7
|
uri_template (~> 0.7.0)
|
9
8
|
|
10
9
|
GEM
|
11
10
|
remote: https://rubygems.org/
|
12
11
|
specs:
|
13
|
-
concurrent-ruby (1.0.5)
|
14
12
|
ecma-re-validator (0.1.2)
|
15
13
|
regexp_parser (~> 0.2)
|
16
|
-
hamster (3.0.0)
|
17
|
-
concurrent-ruby (~> 1.0)
|
18
14
|
hana (1.3.3)
|
19
|
-
link_header (0.0.8)
|
20
15
|
minitest (5.11.1)
|
21
16
|
rake (10.5.0)
|
22
|
-
rdf (3.0.1)
|
23
|
-
hamster (~> 3.0)
|
24
|
-
link_header (~> 0.0, >= 0.0.8)
|
25
17
|
regexp_parser (0.4.9)
|
26
18
|
uri_template (0.7.0)
|
27
19
|
|
data/json_schemer.gemspec
CHANGED
@@ -24,8 +24,14 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
25
|
spec.add_development_dependency "minitest", "~> 5.0"
|
26
26
|
|
27
|
+
# spec.add_development_dependency "benchmark-ips", "~> 2.7.2"
|
28
|
+
# spec.add_development_dependency "jschema", "~> 0.2.1"
|
29
|
+
# spec.add_development_dependency "json-schema", "~> 2.8.0"
|
30
|
+
# spec.add_development_dependency "json_schema", "~> 0.17.0"
|
31
|
+
# spec.add_development_dependency "json_validation", "~> 0.1.0"
|
32
|
+
# spec.add_development_dependency "jsonschema", "~> 2.0.2"
|
33
|
+
|
27
34
|
spec.add_runtime_dependency "ecma-re-validator", "~> 0.1.2"
|
28
35
|
spec.add_runtime_dependency "hana", "~> 1.3.3"
|
29
|
-
spec.add_runtime_dependency "rdf", "~> 3.0.1"
|
30
36
|
spec.add_runtime_dependency "uri_template", "~> 0.7.0"
|
31
37
|
end
|
data/lib/json_schemer.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "json_schemer/version"
|
4
|
+
require "json_schemer/format"
|
4
5
|
|
5
6
|
require "base64"
|
6
7
|
require "ecma-re-validator"
|
@@ -8,7 +9,6 @@ require "hana"
|
|
8
9
|
require "ipaddr"
|
9
10
|
require "json"
|
10
11
|
require 'net/http'
|
11
|
-
require "rdf"
|
12
12
|
require "time"
|
13
13
|
require "uri"
|
14
14
|
require "uri_template"
|
@@ -16,13 +16,6 @@ require "uri_template"
|
|
16
16
|
module JSONSchemer
|
17
17
|
class Schema
|
18
18
|
BOOLEANS = Set[true, false].freeze
|
19
|
-
# this is no good
|
20
|
-
EMAIL_REGEX = /\A[^@\s]+@([\p{L}\d-]+\.)+[\p{L}\d\-]{2,}\z/i.freeze
|
21
|
-
LABEL_REGEX_STRING = '\p{L}([\p{L}\p{N}\-]*[\p{L}\p{N}])?'
|
22
|
-
HOSTNAME_REGEX = /\A(#{LABEL_REGEX_STRING}\.)*#{LABEL_REGEX_STRING}\z/i.freeze
|
23
|
-
JSON_POINTER_REGEX_STRING = '(\/([^~\/]|~[01])*)*'
|
24
|
-
JSON_POINTER_REGEX = /\A#{JSON_POINTER_REGEX_STRING}\z/.freeze
|
25
|
-
RELATIVE_JSON_POINTER_REGEX = /\A(0|[1-9]\d*)(#|#{JSON_POINTER_REGEX_STRING})?\z/.freeze
|
26
19
|
|
27
20
|
def initialize(schema)
|
28
21
|
@root = schema
|
@@ -215,7 +208,7 @@ module JSONSchemer
|
|
215
208
|
|
216
209
|
yield error(data, schema, pointer, 'maxLength') if max_length && data.size > max_length
|
217
210
|
yield error(data, schema, pointer, 'minLength') if min_length && data.size < min_length
|
218
|
-
yield error(data, schema, pointer, 'pattern') if pattern &&
|
211
|
+
yield error(data, schema, pointer, 'pattern') if pattern && Regexp.new(pattern) !~ data
|
219
212
|
|
220
213
|
validate_string_format(data, schema, pointer, format, &Proc.new) if format
|
221
214
|
|
@@ -252,9 +245,9 @@ module JSONSchemer
|
|
252
245
|
when 'time'
|
253
246
|
valid_date_time?("2001-02-03T#{data}")
|
254
247
|
when 'email'
|
255
|
-
data.ascii_only? &&
|
248
|
+
data.ascii_only? && valid_email?(data)
|
256
249
|
when 'idn-email'
|
257
|
-
|
250
|
+
valid_email?(data)
|
258
251
|
when 'hostname'
|
259
252
|
data.ascii_only? && valid_hostname?(data)
|
260
253
|
when 'idn-hostname'
|
@@ -264,19 +257,19 @@ module JSONSchemer
|
|
264
257
|
when 'ipv6'
|
265
258
|
valid_ip?(data, :v6)
|
266
259
|
when 'uri'
|
267
|
-
data.ascii_only? &&
|
260
|
+
data.ascii_only? && valid_iri?(data)
|
268
261
|
when 'uri-reference'
|
269
|
-
data.ascii_only? && (
|
262
|
+
data.ascii_only? && (valid_iri?(data) || valid_iri_reference?(data))
|
270
263
|
when 'iri'
|
271
|
-
|
264
|
+
valid_iri?(data)
|
272
265
|
when 'iri-reference'
|
273
|
-
|
266
|
+
valid_iri?(data) || valid_iri_reference?(data)
|
274
267
|
when 'uri-template'
|
275
268
|
valid_uri_template?(data)
|
276
269
|
when 'json-pointer'
|
277
270
|
valid_json_pointer?(data)
|
278
271
|
when 'relative-json-pointer'
|
279
|
-
|
272
|
+
valid_relative_json_pointer?(data)
|
280
273
|
when 'regex'
|
281
274
|
EcmaReValidator.valid?(data)
|
282
275
|
end
|
@@ -361,7 +354,7 @@ module JSONSchemer
|
|
361
354
|
[Regexp.new(pattern), property_schema]
|
362
355
|
end
|
363
356
|
regex_pattern_properties.each do |regex, property_schema|
|
364
|
-
if regex
|
357
|
+
if regex =~ key
|
365
358
|
validate(value, property_schema, "#{pointer}/#{key}", parent_uri, &block)
|
366
359
|
matched_key = true
|
367
360
|
end
|
@@ -398,8 +391,12 @@ module JSONSchemer
|
|
398
391
|
false
|
399
392
|
end
|
400
393
|
|
394
|
+
def valid_email?(data)
|
395
|
+
!!(Format::EMAIL_REGEX =~ data)
|
396
|
+
end
|
397
|
+
|
401
398
|
def valid_hostname?(data)
|
402
|
-
HOSTNAME_REGEX
|
399
|
+
!!(Format::HOSTNAME_REGEX =~ data && data.split('.').all? { |label| label.size <= 63 })
|
403
400
|
end
|
404
401
|
|
405
402
|
def valid_ip?(data, type)
|
@@ -409,6 +406,14 @@ module JSONSchemer
|
|
409
406
|
false
|
410
407
|
end
|
411
408
|
|
409
|
+
def valid_iri?(data)
|
410
|
+
!!(Format::IRI =~ data)
|
411
|
+
end
|
412
|
+
|
413
|
+
def valid_iri_reference?(data)
|
414
|
+
!!(Format::IRELATIVE_REF =~ data)
|
415
|
+
end
|
416
|
+
|
412
417
|
def valid_uri_template?(data)
|
413
418
|
URITemplate.new(data)
|
414
419
|
true
|
@@ -417,7 +422,11 @@ module JSONSchemer
|
|
417
422
|
end
|
418
423
|
|
419
424
|
def valid_json_pointer?(data)
|
420
|
-
JSON_POINTER_REGEX
|
425
|
+
!!(Format::JSON_POINTER_REGEX =~ data)
|
426
|
+
end
|
427
|
+
|
428
|
+
def valid_relative_json_pointer?(data)
|
429
|
+
!!(Format::RELATIVE_JSON_POINTER_REGEX =~ data)
|
421
430
|
end
|
422
431
|
|
423
432
|
def join_uri(a, b)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONSchemer::Format
|
4
|
+
# this is no good
|
5
|
+
EMAIL_REGEX = /\A[^@\s]+@([\p{L}\d-]+\.)+[\p{L}\d\-]{2,}\z/i.freeze
|
6
|
+
LABEL_REGEX_STRING = '\p{L}([\p{L}\p{N}\-]*[\p{L}\p{N}])?'
|
7
|
+
HOSTNAME_REGEX = /\A(#{LABEL_REGEX_STRING}\.)*#{LABEL_REGEX_STRING}\z/i.freeze
|
8
|
+
JSON_POINTER_REGEX_STRING = '(\/([^~\/]|~[01])*)*'
|
9
|
+
JSON_POINTER_REGEX = /\A#{JSON_POINTER_REGEX_STRING}\z/.freeze
|
10
|
+
RELATIVE_JSON_POINTER_REGEX = /\A(0|[1-9]\d*)(#|#{JSON_POINTER_REGEX_STRING})?\z/.freeze
|
11
|
+
|
12
|
+
# https://github.com/ruby-rdf/rdf
|
13
|
+
|
14
|
+
# IRI components
|
15
|
+
UCSCHAR = Regexp.compile(<<-EOS.gsub(/\s+/, ''))
|
16
|
+
[\\u00A0-\\uD7FF]|[\\uF900-\\uFDCF]|[\\uFDF0-\\uFFEF]|
|
17
|
+
[\\u{10000}-\\u{1FFFD}]|[\\u{20000}-\\u{2FFFD}]|[\\u{30000}-\\u{3FFFD}]|
|
18
|
+
[\\u{40000}-\\u{4FFFD}]|[\\u{50000}-\\u{5FFFD}]|[\\u{60000}-\\u{6FFFD}]|
|
19
|
+
[\\u{70000}-\\u{7FFFD}]|[\\u{80000}-\\u{8FFFD}]|[\\u{90000}-\\u{9FFFD}]|
|
20
|
+
[\\u{A0000}-\\u{AFFFD}]|[\\u{B0000}-\\u{BFFFD}]|[\\u{C0000}-\\u{CFFFD}]|
|
21
|
+
[\\u{D0000}-\\u{DFFFD}]|[\\u{E1000}-\\u{EFFFD}]
|
22
|
+
EOS
|
23
|
+
IPRIVATE = Regexp.compile("[\\uE000-\\uF8FF]|[\\u{F0000}-\\u{FFFFD}]|[\\u100000-\\u10FFFD]").freeze
|
24
|
+
SCHEME = Regexp.compile("[A-Za-z](?:[A-Za-z0-9+-\.])*").freeze
|
25
|
+
PORT = Regexp.compile("[0-9]*").freeze
|
26
|
+
IP_LITERAL = Regexp.compile("\\[[0-9A-Fa-f:\\.]*\\]").freeze # Simplified, no IPvFuture
|
27
|
+
PCT_ENCODED = Regexp.compile("%[0-9A-Fa-f][0-9A-Fa-f]").freeze
|
28
|
+
GEN_DELIMS = Regexp.compile("[:/\\?\\#\\[\\]@]").freeze
|
29
|
+
SUB_DELIMS = Regexp.compile("[!\\$&'\\(\\)\\*\\+,;=]").freeze
|
30
|
+
RESERVED = Regexp.compile("(?:#{GEN_DELIMS}|#{SUB_DELIMS})").freeze
|
31
|
+
UNRESERVED = Regexp.compile("[A-Za-z0-9\._~-]").freeze
|
32
|
+
|
33
|
+
IUNRESERVED = Regexp.compile("[A-Za-z0-9\._~-]|#{UCSCHAR}").freeze
|
34
|
+
|
35
|
+
IPCHAR = Regexp.compile("(?:#{IUNRESERVED}|#{PCT_ENCODED}|#{SUB_DELIMS}|:|@)").freeze
|
36
|
+
|
37
|
+
IQUERY = Regexp.compile("(?:#{IPCHAR}|#{IPRIVATE}|/|\\?)*").freeze
|
38
|
+
|
39
|
+
IFRAGMENT = Regexp.compile("(?:#{IPCHAR}|/|\\?)*").freeze.freeze
|
40
|
+
|
41
|
+
ISEGMENT = Regexp.compile("(?:#{IPCHAR})*").freeze
|
42
|
+
ISEGMENT_NZ = Regexp.compile("(?:#{IPCHAR})+").freeze
|
43
|
+
ISEGMENT_NZ_NC = Regexp.compile("(?:(?:#{IUNRESERVED})|(?:#{PCT_ENCODED})|(?:#{SUB_DELIMS})|@)+").freeze
|
44
|
+
|
45
|
+
IPATH_ABEMPTY = Regexp.compile("(?:/#{ISEGMENT})*").freeze
|
46
|
+
IPATH_ABSOLUTE = Regexp.compile("/(?:(?:#{ISEGMENT_NZ})(/#{ISEGMENT})*)?").freeze
|
47
|
+
IPATH_NOSCHEME = Regexp.compile("(?:#{ISEGMENT_NZ_NC})(?:/#{ISEGMENT})*").freeze
|
48
|
+
IPATH_ROOTLESS = Regexp.compile("(?:#{ISEGMENT_NZ})(?:/#{ISEGMENT})*").freeze
|
49
|
+
IPATH_EMPTY = Regexp.compile("").freeze
|
50
|
+
|
51
|
+
IREG_NAME = Regexp.compile("(?:(?:#{IUNRESERVED})|(?:#{PCT_ENCODED})|(?:#{SUB_DELIMS}))*").freeze
|
52
|
+
IHOST = Regexp.compile("(?:#{IP_LITERAL})|(?:#{IREG_NAME})").freeze
|
53
|
+
IUSERINFO = Regexp.compile("(?:(?:#{IUNRESERVED})|(?:#{PCT_ENCODED})|(?:#{SUB_DELIMS})|:)*").freeze
|
54
|
+
IAUTHORITY = Regexp.compile("(?:#{IUSERINFO}@)?#{IHOST}(?::#{PORT})?").freeze
|
55
|
+
|
56
|
+
IRELATIVE_PART = Regexp.compile("(?:(?://#{IAUTHORITY}(?:#{IPATH_ABEMPTY}))|(?:#{IPATH_ABSOLUTE})|(?:#{IPATH_NOSCHEME})|(?:#{IPATH_EMPTY}))").freeze
|
57
|
+
IRELATIVE_REF = Regexp.compile("^#{IRELATIVE_PART}(?:\\?#{IQUERY})?(?:\\##{IFRAGMENT})?$").freeze
|
58
|
+
|
59
|
+
IHIER_PART = Regexp.compile("(?:(?://#{IAUTHORITY}#{IPATH_ABEMPTY})|(?:#{IPATH_ABSOLUTE})|(?:#{IPATH_ROOTLESS})|(?:#{IPATH_EMPTY}))").freeze
|
60
|
+
IRI = Regexp.compile("^#{SCHEME}:(?:#{IHIER_PART})(?:\\?#{IQUERY})?(?:\\##{IFRAGMENT})?$").freeze
|
61
|
+
end
|
data/lib/json_schemer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schemer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Harsha
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.3.3
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: rdf
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: 3.0.1
|
90
|
-
type: :runtime
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: 3.0.1
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: uri_template
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,6 +113,7 @@ files:
|
|
127
113
|
- bin/setup
|
128
114
|
- json_schemer.gemspec
|
129
115
|
- lib/json_schemer.rb
|
116
|
+
- lib/json_schemer/format.rb
|
130
117
|
- lib/json_schemer/version.rb
|
131
118
|
homepage: https://github.com/davishmcclurg/json_schemer
|
132
119
|
licenses:
|