nakischema 0.0.0 → 0.1.0

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 +33 -22
  3. data/nakischema.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dd008df051c38e237c7f65fe70c0680eb06c407
4
- data.tar.gz: ff26282070eaad84c5d5d628bc7868b7857c1b9a
3
+ metadata.gz: f1ee5445ac066ed5b77d5dd3554d96a4c7723e26
4
+ data.tar.gz: 4302db46b28684a12f8c8b259c8597373355a011
5
5
  SHA512:
6
- metadata.gz: ac740957dfad68d5527fe0e0a1c70adb2169563e61b922ec5660a8b298947702503568f764128a8f9f89ac87a0cfa4018490b813d4a938dea43b28b149797870
7
- data.tar.gz: 2db4bc15ed4f118373d621890c0351f015ce337e6ac89ad0fe8e6914090169739bb9fe437d830c98fcf22ca93749cc5616cf6bf8c02ce930fb4ea3058ae398aa
6
+ metadata.gz: 91252167fbe10d1808ea86f108fc37d9ac6be0a36b99cccfab36dc2960ba04b161ac153a66071181cd394dc36ab512c4a5d7363a7e8dd3fe86ba39ea7b52b984
7
+ data.tar.gz: 422749ffe71639df10e08790406a03687d35e056213c1ad977f41b7ceec22ed727e38a28382af7fab75297cec9a808f373d18581a9584ea0e4d43d1111482e15
data/lib/nakischema.rb CHANGED
@@ -1,25 +1,33 @@
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?}"
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
9
+ when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
10
+ # TODO: maybe deprecate the NilClass, TrueClass, FalseClass since they can be asserted via the next case branch
11
+ when Class ; raise_with_path.call "expected #{schema } != #{object.class }" unless schema === object
12
+ when Regexp ; raise_with_path.call "expected #{schema } != #{object.inspect}" unless schema === object
13
+ when Range ; raise_with_path.call "expected #{schema } != #{object }" unless schema.include? object
8
14
  when Hash
9
- raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys_sorted keys values }).empty?
15
+ raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys each_key each_value }).empty?
10
16
  raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array unless (schema.keys & %i{ size }).empty?
11
17
  schema.each do |k, v|
12
18
  case k
13
- # when :keys_sorted ; raise_with_path.call "expected explicit keys #{v} != #{object.keys.sort}" unless v == object.keys.sort
14
19
  when :size ; raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
15
20
  # when Fixnum
16
21
  # raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
17
- # validate object[k], v, [*path, "##{k}"]
22
+ # validate object[k], v, [*path, :"##{k}"]
18
23
  when :keys ; validate object.keys, v, [*path, :keys]
19
- when :hash_opt ; v.each{ |k, v| validate object[k], v, [*path, k] if object.key? k }
20
- when :hash
21
- raise_with_path.call "expected implicit keys #{v} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
22
- v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
24
+ when :values ; validate object.values, v, [*path, :values]
25
+ when :keys_sorted ; validate object.keys.sort, v, [*path, :keys_sorted]
26
+ when :hash_opt ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] if object.key? k }
27
+ when :hash_req ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
28
+ when :hash ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
29
+ raise_with_path.call "expected implicit keys #{v.keys} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
30
+ v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
23
31
  when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, [*path, :"key##{i}"] }
24
32
  when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, [*path, :"value##{i}"] }
25
33
  when :each
@@ -36,35 +44,38 @@ module Nakischema
36
44
  # validate object, v, [*path, :"case##{i}"]
37
45
  # true
38
46
  # 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}"
47
+ when :assertions
48
+ v.each_with_index do |assertion, i|
49
+ begin
50
+ raise Error.new "custom assertion failed" unless assertion.call object, [*path, :"assertion##{i}"]
51
+ rescue Error => e
52
+ raise_with_path.call e, [*path, :"assertion##{i}"]
53
+ end
54
+ end
55
+ else
56
+ raise_with_path.call "unsupported rule #{k.inspect}"
41
57
  end
42
58
  end
43
- when NilClass, TrueClass, FalseClass, String ; 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
59
  when Array
47
60
  if schema.map(&:class) == [Array]
48
61
  raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
49
- 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
62
+ raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size} for #{object.inspect}" unless schema[0].size == object.size
63
+ object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, [*path, :"##{i}"] }
53
64
  else
54
65
  results = schema.lazy.with_index.map do |v, i|
55
66
  # raise_with_path.call "unsupported nested Array" if v.is_a? Array
56
67
  begin
57
- validate object, v, [*path, "variant##{i}"]
68
+ validate object, v, [*path, :"variant##{i}"]
58
69
  nil
59
70
  rescue Error => e
60
71
  e
61
72
  end
62
73
  end
63
- raise_with_path.call \
64
- "expected at least one of #{schema.size} rules to match the #{object.inspect}, errors:\n" +
74
+ raise Error.new "expected at least one of #{schema.size} rules to match the #{object.inspect}, errors:\n" +
65
75
  results.force.compact.map{ |_| _.to_s.gsub(/^/, " ") }.join("\n") if results.all?
66
76
  end
67
- else ; raise_with_path.call "unsupported rule class #{schema.class}"
77
+ else
78
+ raise_with_path.call "unsupported rule class #{schema.class}"
68
79
  end
69
80
  end
70
81
  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.0"
3
+ spec.version = "0.1.0"
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakischema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
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: 2021-08-14 00:00:00.000000000 Z
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: The most compact yet powerful arbitrary nested objects validator. Especially
14
14
  handy to validate JSONs.