nakischema 0.1.4 → 0.2.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 +5 -5
  2. data/lib/nakischema.rb +19 -17
  3. data/nakischema.gemspec +1 -1
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 271b63150c8a6eead2fba711f312b2e2f82ba24a
4
- data.tar.gz: 829b656fc7a75c629d3ccea2ac44ca4de98d4598
2
+ SHA256:
3
+ metadata.gz: d7b23047f5d6ea56c755489cc2e2238db4d3c550da31cc8f03a3f91d820ee465
4
+ data.tar.gz: 0a0feaf7a0642ad4a42d3ef62aafd34e1a6f20e7dc0b78712d2604c384d6a811
5
5
  SHA512:
6
- metadata.gz: 07bd982a7331b8ec0937b97d682b623d4c85ac14bcb71512dc85bc16536f40f8a01dfcac0df379097614f84748a93d2d426c2addb45b0f0ca703ee1b0c069ef6
7
- data.tar.gz: c2716c3ad3e8e804891ef364c31ca7ffc3a2289b16a32f203905362b866bc3f832932be3a0350c25edd41b60d7d22c3b1739d2e279aebd0f1987d76655e07ee2
6
+ metadata.gz: 8d252b3a8fd89e90531c896b30e381121ce16b024480c8d87ede34c4267a0cf03a0278e6e1a356a10c126662aebb16b456b6c2914dbb4591eeab66d41cdc9ec1
7
+ data.tar.gz: b7e3989528ac2bf4e233fd30b29da1cc2d0491838c26d6adbf4b014f30eae2015caaab61f9f8566af18aa31a62d23464143dba02dda73d920d8fa4c6301a4475
data/lib/nakischema.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  module Nakischema
2
2
  Error = Class.new RuntimeError
3
3
 
4
- def self.validate object, schema, path = []
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?}"
6
+ raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}#{" #{message}" if message}"
7
7
  end
8
8
  # TODO: maybe move '(at ...)' to the beginning
9
9
  case schema
@@ -14,26 +14,28 @@ module Nakischema
14
14
  when Range ; raise_with_path.call "expected #{schema } != #{object }" unless schema.include? object
15
15
  when Hash
16
16
  raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys each_key each_value }).empty?
17
- raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array unless (schema.keys & %i{ size }).empty?
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
20
  when :size ; raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
21
21
  # when Fixnum
22
22
  # raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
23
- # validate object[k], v, [*path, :"##{k}"]
24
- when :keys ; validate object.keys, v, [*path, :keys]
25
- when :values ; validate object.values, v, [*path, :values]
26
- when :keys_sorted ; validate object.keys.sort, v, [*path, :keys_sorted]
27
- when :hash_opt ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] if object.key? k }
28
- when :hash_req ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
23
+ # 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 ; v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] if object.key? k }
28
+ when :hash_req ; raise_with_path.call "expected required keys #{v.keys.sort} #{object.keys.sort}" unless (v.keys - object.keys).empty?
29
+ v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] }
29
30
  when :hash ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
30
- raise_with_path.call "expected implicit keys #{v.keys} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
31
- v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
32
- when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, [*path, :"key##{i}"] }
33
- when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, [*path, :"value##{i}"] }
31
+ raise_with_path.call "expected implicit keys #{v.keys.sort} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
32
+ v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] }
33
+ when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, path: [*path, :"key##{i}"] }
34
+ when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, path: [*path, :"value##{i}"] }
35
+ when :method ; v.each{ |m, e| validate object.public_method(m).call, e, path: [*path, :"method##{m}"] }
34
36
  when :each
35
37
  raise_with_path.call "expected iterable != #{object.class}" unless object.respond_to? :each_with_index
36
- object.each_with_index{ |e, i| validate e, v, [*path, :"##{i}"] }
38
+ object.each_with_index{ |e, i| validate e, v, path: [*path, :"##{i}"] }
37
39
  # when :case
38
40
  # 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|
39
41
  # next if begin
@@ -42,7 +44,7 @@ module Nakischema
42
44
  # rescue Error => e
43
45
  # e
44
46
  # end
45
- # validate object, v, [*path, :"case##{i}"]
47
+ # validate object, v, path: [*path, :"case##{i}"]
46
48
  # true
47
49
  # end.none?
48
50
  when :assertions
@@ -61,12 +63,12 @@ module Nakischema
61
63
  if schema.map(&:class) == [Array]
62
64
  raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
63
65
  raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size} for #{object.inspect}" unless schema[0].size == object.size
64
- object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, [*path, :"##{i}"] }
66
+ object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, path: [*path, :"##{i}"] }
65
67
  else
66
68
  results = schema.lazy.with_index.map do |v, i|
67
69
  # raise_with_path.call "unsupported nested Array" if v.is_a? Array
68
70
  begin
69
- validate object, v, [*path, :"variant##{i}"]
71
+ validate object, v, path: [*path, :"variant##{i}"]
70
72
  nil
71
73
  rescue Error => e
72
74
  e
data/nakischema.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "nakischema"
3
- spec.version = "0.1.4"
3
+ spec.version = "0.2.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.1.4
4
+ version: 0.2.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-09-12 00:00:00.000000000 Z
11
+ date: 2022-11-22 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.
@@ -40,8 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements: []
43
- rubyforge_project:
44
- rubygems_version: 2.5.2.3
43
+ rubygems_version: 3.3.25
45
44
  signing_key:
46
45
  specification_version: 4
47
46
  summary: compact yet powerful arbitrary nested objects validator