nakischema 0.0.1 → 0.0.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 +19 -12
- data/nakischema.gemspec +1 -1
- 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: 23e9302b311123a5b0837d868887c752af34a976
|
4
|
+
data.tar.gz: 275eb8ded8c32e65f1daa4daab04c559937f1b58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30f2f5ca6da4eed2d5d94a1e967f260e561343db487d14b0114b2f77e40da3d71b4f3c7356520390678746f49fe459f45b7c01f70ddf4f29ab6506ec267d654e
|
7
|
+
data.tar.gz: aecc915fd810136e9465f9921f8878005b3ebc6edb1d4d0f101f1cfc5103b85fa8c79139f6bec9dba6aa0c1dc182bc10d879a5c807cce72356d12c1f3e2a5e6a
|
data/lib/nakischema.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Nakischema
|
2
2
|
Error = Class.new RuntimeError
|
3
3
|
def self.validate object, schema, path = []
|
4
|
-
raise_with_path = lambda do |msg|
|
5
|
-
raise Error.new "#{msg}#{" (at #{
|
4
|
+
raise_with_path = lambda do |msg, _path = path|
|
5
|
+
raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}"
|
6
6
|
end
|
7
|
+
# TODO: maybe move '(at ...)' to the beginning
|
7
8
|
case schema
|
8
9
|
when Hash
|
9
10
|
raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys each_key each_value }).empty?
|
@@ -14,7 +15,7 @@ module Nakischema
|
|
14
15
|
when :size ; raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
|
15
16
|
# when Fixnum
|
16
17
|
# raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
|
17
|
-
# validate object[k], v, [*path, "##{k}"]
|
18
|
+
# validate object[k], v, [*path, :"##{k}"]
|
18
19
|
when :keys ; validate object.keys, v, [*path, :keys]
|
19
20
|
when :hash_opt ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] if object.key? k }
|
20
21
|
when :hash_req ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
|
@@ -36,8 +37,16 @@ module Nakischema
|
|
36
37
|
# validate object, v, [*path, :"case##{i}"]
|
37
38
|
# true
|
38
39
|
# end.none?
|
39
|
-
when :assertions
|
40
|
-
|
40
|
+
when :assertions
|
41
|
+
v.each_with_index do |assertion, i|
|
42
|
+
begin
|
43
|
+
raise Error.new "custom assertion failed" unless assertion.call object, [*path, :"assertion##{i}"]
|
44
|
+
rescue Error => e
|
45
|
+
raise_with_path.call e, [*path, :"assertion##{i}"]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
else
|
49
|
+
raise_with_path.call "unsupported rule #{k.inspect}"
|
41
50
|
end
|
42
51
|
end
|
43
52
|
when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
|
@@ -47,24 +56,22 @@ module Nakischema
|
|
47
56
|
if schema.map(&:class) == [Array]
|
48
57
|
raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
|
49
58
|
raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size}" unless schema[0].size == object.size
|
50
|
-
object.zip(schema[0]).each_with_index
|
51
|
-
validate o, v, [*path, :"##{i}"]
|
52
|
-
end
|
59
|
+
object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, [*path, :"##{i}"] }
|
53
60
|
else
|
54
61
|
results = schema.lazy.with_index.map do |v, i|
|
55
62
|
# raise_with_path.call "unsupported nested Array" if v.is_a? Array
|
56
63
|
begin
|
57
|
-
validate object, v, [*path, "variant##{i}"]
|
64
|
+
validate object, v, [*path, :"variant##{i}"]
|
58
65
|
nil
|
59
66
|
rescue Error => e
|
60
67
|
e
|
61
68
|
end
|
62
69
|
end
|
63
|
-
|
64
|
-
"expected at least one of #{schema.size} rules to match the #{object.inspect}, errors:\n" +
|
70
|
+
raise Error.new "expected at least one of #{schema.size} rules to match the #{object.inspect}, errors:\n" +
|
65
71
|
results.force.compact.map{ |_| _.to_s.gsub(/^/, " ") }.join("\n") if results.all?
|
66
72
|
end
|
67
|
-
else
|
73
|
+
else
|
74
|
+
raise_with_path.call "unsupported rule class #{schema.class}"
|
68
75
|
end
|
69
76
|
end
|
70
77
|
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.0.
|
3
|
+
spec.version = "0.0.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
|
|