nakischema 0.0.0 → 0.0.1
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/lib/nakischema.rb +9 -9
- 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: 66f0fa36c6867bf9d4ae1e6388522a98baa5ce53
|
4
|
+
data.tar.gz: 3fa3398cecfcb67fa6428e0d69898f3bfc5a5af1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 573bc852f4e8d2af49d856247818d3d7b264fa3aa3df05b306136eb35d72f3979831221bce80859eab539ec591b96a00283d2080a38b0b46495288e947623f78
|
7
|
+
data.tar.gz: '0504529ee39e1306ec3c5469bb3a2a649f5738a61965ff8613f944581e07a041e32785173ec8c48bcf24beba2b41d2d9a5620e8b87e70cad359db4ee43e490c7'
|
data/lib/nakischema.rb
CHANGED
@@ -2,11 +2,11 @@ module Nakischema
|
|
2
2
|
Error = Class.new RuntimeError
|
3
3
|
def self.validate object, schema, path = []
|
4
4
|
raise_with_path = lambda do |msg|
|
5
|
-
raise Error.new "#{msg}#{" (at #{path})" unless path.empty?}"
|
5
|
+
raise Error.new "#{msg}#{" (at #{path})" unless path.empty?}" # TODO: maybe move '(at ...)' to the beginning
|
6
6
|
end
|
7
7
|
case schema
|
8
8
|
when Hash
|
9
|
-
raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{
|
9
|
+
raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys each_key each_value }).empty?
|
10
10
|
raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array unless (schema.keys & %i{ size }).empty?
|
11
11
|
schema.each do |k, v|
|
12
12
|
case k
|
@@ -16,10 +16,10 @@ module Nakischema
|
|
16
16
|
# raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
|
17
17
|
# validate object[k], v, [*path, "##{k}"]
|
18
18
|
when :keys ; validate object.keys, v, [*path, :keys]
|
19
|
-
when :hash_opt ; v.each{ |k, v| validate object
|
20
|
-
when :
|
21
|
-
|
22
|
-
|
19
|
+
when :hash_opt ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] if object.key? k }
|
20
|
+
when :hash_req ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
|
21
|
+
when :hash ; raise_with_path.call "expected implicit keys #{v.keys} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
|
22
|
+
v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
|
23
23
|
when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, [*path, :"key##{i}"] }
|
24
24
|
when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, [*path, :"value##{i}"] }
|
25
25
|
when :each
|
@@ -40,9 +40,9 @@ module Nakischema
|
|
40
40
|
else ; raise_with_path.call "unsupported rule #{k.inspect}"
|
41
41
|
end
|
42
42
|
end
|
43
|
-
when NilClass, TrueClass, FalseClass, String ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
|
44
|
-
when Regexp
|
45
|
-
when Range
|
43
|
+
when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
|
44
|
+
when Regexp ; raise_with_path.call "expected #{schema } != #{object.inspect}" unless schema === object
|
45
|
+
when Range ; raise_with_path.call "expected #{schema } != #{object }" unless schema.include? object
|
46
46
|
when Array
|
47
47
|
if schema.map(&:class) == [Array]
|
48
48
|
raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
|
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.1"
|
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
|
|