nakischema 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nakischema.rb +53 -16
- data/nakischema.gemspec +4 -1
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe6edc1f3b95435d8fa18f6d2836086050d96276b1397fce0a378ceafbaf2797
|
4
|
+
data.tar.gz: 2b23892f25a662c9c788a611cf1417dffcbc65a8bf4ae275cd03bfb220b17337
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e3659f172ace66b3b3353e0630f8be2e4baa5c252fa27f22c9def7e37954082a62ed642d082b1015087e2b3126df54c477caa165179e595a401431d84b801cd
|
7
|
+
data.tar.gz: ec1de44276cb84f9d2e2ec374ef5be6c62fa7f9655149f572111c62911a2f83c2360450a29724e6b115147c8c467c8a44b4597363bddeb20067c5dd0b27229c7
|
data/lib/nakischema.rb
CHANGED
@@ -3,7 +3,7 @@ module Nakischema
|
|
3
3
|
|
4
4
|
def self.validate object, schema, message = nil, path: []
|
5
5
|
raise_with_path = lambda do |msg, _path = path|
|
6
|
-
raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}#{" #{message}" if message}"
|
6
|
+
raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}#{" #{message.respond_to?(:call) ? message[object] : message}" if message}" # TODO: lambda message?
|
7
7
|
end
|
8
8
|
# TODO: maybe move '(at ...)' to the beginning
|
9
9
|
case schema
|
@@ -17,25 +17,29 @@ module Nakischema
|
|
17
17
|
raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array unless (schema.keys & %i{ size }).empty? # TODO: maybe allow Hash object?
|
18
18
|
schema.each do |k, v|
|
19
19
|
case k
|
20
|
-
when :size ; raise_with_path.call "expected
|
20
|
+
when :size ; raise_with_path.call "expected Range != #{v.class}" unless v.is_a? Range
|
21
|
+
raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
|
21
22
|
# when Fixnum
|
22
23
|
# raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
|
23
24
|
# validate object[k], v, path: [*path, :"##{k}"]
|
24
|
-
when :keys ; validate object.keys, v, path: [*path, :keys]
|
25
|
-
when :values ; validate object.values, v, path: [*path, :values]
|
26
|
-
when :keys_sorted ; validate object.keys.sort, v, path: [*path, :keys_sorted] # TODO: maybe copypaste the Array validation to reduce [] nesting
|
27
|
-
when :hash_opt ;
|
28
|
-
|
29
|
-
|
25
|
+
when :keys ; validate object.keys, v, message, path: [*path, :keys]
|
26
|
+
when :values ; validate object.values, v, message, path: [*path, :values]
|
27
|
+
when :keys_sorted ; validate object.keys.sort, v, message, path: [*path, :keys_sorted] # TODO: maybe copypaste the Array validation to reduce [] nesting
|
28
|
+
when :hash_opt ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
|
29
|
+
v.each{ |k, v| validate object.fetch(k), v, message, path: [*path, k] if object.key? k }
|
30
|
+
when :hash_req ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
|
31
|
+
raise_with_path.call "expected required keys #{v.keys.sort} ∉ #{object.keys.sort}" unless (v.keys - object.keys).empty?
|
32
|
+
v.each{ |k, v| validate object.fetch(k), v, message, path: [*path, k] }
|
30
33
|
when :hash ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
when :
|
35
|
-
when :
|
34
|
+
hash_wo_opt = object.keys.sort - schema.fetch(:hash_opt, {}).keys
|
35
|
+
raise_with_path.call "expected implicit keys #{v.keys.sort} != #{hash_wo_opt}" unless v.keys.sort == hash_wo_opt
|
36
|
+
v.each{ |k, v| validate object.fetch(k), v, message, path: [*path, k] }
|
37
|
+
when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, message, path: [*path, :"key##{i}"] }
|
38
|
+
when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, message, path: [*path, :"value##{i}"] }
|
39
|
+
when :method ; v.each{ |m, e| validate object.public_method(m).call, e, message, path: [*path, :"method##{m}"] }
|
36
40
|
when :each
|
37
41
|
raise_with_path.call "expected iterable != #{object.class}" unless object.respond_to? :each_with_index
|
38
|
-
object.each_with_index{ |e, i| validate e, v, path: [*path, :"##{i}"] }
|
42
|
+
object.each_with_index{ |e, i| validate e, v, message, path: [*path, :"##{i}"] }
|
39
43
|
# when :case
|
40
44
|
# raise_with_path.call "expected at least one of #{v.size} cases to match the #{object.inspect}" if v.map.with_index do |(k, v), i|
|
41
45
|
# next if begin
|
@@ -63,12 +67,12 @@ module Nakischema
|
|
63
67
|
if schema.map(&:class) == [Array]
|
64
68
|
raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
|
65
69
|
raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size} for #{object.inspect}" unless schema[0].size == object.size
|
66
|
-
object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, path: [*path, :"##{i}"] }
|
70
|
+
object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, message, path: [*path, :"##{i}"] }
|
67
71
|
else
|
68
72
|
results = schema.lazy.with_index.map do |v, i|
|
69
73
|
# raise_with_path.call "unsupported nested Array" if v.is_a? Array
|
70
74
|
begin
|
71
|
-
validate object, v, path: [*path, :"variant##{i}"]
|
75
|
+
validate object, v, message, path: [*path, :"variant##{i}"]
|
72
76
|
nil
|
73
77
|
rescue Error => e
|
74
78
|
e
|
@@ -136,4 +140,37 @@ module Nakischema
|
|
136
140
|
raise_with_path.call "unsupported rule class #{schema.class}"
|
137
141
|
end
|
138
142
|
end
|
143
|
+
|
144
|
+
def self.fixture _
|
145
|
+
require "regexp-examples"
|
146
|
+
require "addressable"
|
147
|
+
case _
|
148
|
+
when Hash
|
149
|
+
case _.keys
|
150
|
+
when %i{ hash } ; _[:hash ].map{ |k,v| [k,fixture(v)] }.shuffle.to_h
|
151
|
+
when %i{ hash_req } ; [*_[:hash_req].map{ |k,v| [k,fixture(v)] }, ["foo","bar"]].shuffle.to_h # TODO: assert no collision
|
152
|
+
when %i{ size each } ; Array.new(fixture _[:size]){ fixture _[:each] }
|
153
|
+
else ; fail _.keys.inspect
|
154
|
+
end
|
155
|
+
when Array ; [Array] == _.map(&:class) ? _[0].map(&method(:fixture)) : fixture(_.sample)
|
156
|
+
when Regexp
|
157
|
+
t = _.random_example
|
158
|
+
tt = begin
|
159
|
+
URI t
|
160
|
+
rescue URI::InvalidURIError
|
161
|
+
URI Addressable::URI.escape t
|
162
|
+
end
|
163
|
+
tt.is_a?(URI::HTTP) ? tt.to_s : t
|
164
|
+
when Range ; rand _
|
165
|
+
when String ; _
|
166
|
+
when Class
|
167
|
+
case _.name
|
168
|
+
when "Integer" ; -rand(1000000)
|
169
|
+
when "Hash" ; {}
|
170
|
+
else ; fail "bad fixture node class name: #{_.name}"
|
171
|
+
end
|
172
|
+
else ; fail "bad fixture node class: #{_.class.inspect}"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
139
176
|
end
|
data/nakischema.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "nakischema"
|
3
|
-
spec.version = "0.2.
|
3
|
+
spec.version = "0.2.2"
|
4
4
|
spec.summary = "compact yet powerful arbitrary nested objects validator"
|
5
5
|
spec.description = "The most compact yet powerful arbitrary nested objects validator. Especially handy to validate JSONs."
|
6
6
|
|
@@ -9,5 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.license = "MIT"
|
10
10
|
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/nakischema"}
|
11
11
|
|
12
|
+
spec.add_dependency "regexp-examples"
|
13
|
+
spec.add_dependency "addressable"
|
14
|
+
|
12
15
|
spec.files = %w{ LICENSE nakischema.gemspec lib/nakischema.rb }
|
13
16
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nakischema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Maslov aka Nakilon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2023-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: regexp-examples
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: addressable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
description: The most compact yet powerful arbitrary nested objects validator. Especially
|
14
42
|
handy to validate JSONs.
|
15
43
|
email: nakilon@gmail.com
|