nakischema 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nakischema.rb +19 -12
  3. data/nakischema.gemspec +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66f0fa36c6867bf9d4ae1e6388522a98baa5ce53
4
- data.tar.gz: 3fa3398cecfcb67fa6428e0d69898f3bfc5a5af1
3
+ metadata.gz: 23e9302b311123a5b0837d868887c752af34a976
4
+ data.tar.gz: 275eb8ded8c32e65f1daa4daab04c559937f1b58
5
5
  SHA512:
6
- metadata.gz: 573bc852f4e8d2af49d856247818d3d7b264fa3aa3df05b306136eb35d72f3979831221bce80859eab539ec591b96a00283d2080a38b0b46495288e947623f78
7
- data.tar.gz: '0504529ee39e1306ec3c5469bb3a2a649f5738a61965ff8613f944581e07a041e32785173ec8c48bcf24beba2b41d2d9a5620e8b87e70cad359db4ee43e490c7'
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 #{path})" unless path.empty?}" # TODO: maybe move '(at ...)' to the beginning
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 ; v.each_with_index{ |assertion, i| raise_with_path.call "custom assertion failed" unless assertion.call object, [*path, :"assertion##{i}"] }
40
- else ; raise_with_path.call "unsupported rule #{k.inspect}"
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 do |(o, v), i|
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
- raise_with_path.call \
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 ; raise_with_path.call "unsupported rule class #{schema.class}"
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.1"
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakischema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon