nakischema 0.2.1 → 0.2.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 +13 -13
  3. data/nakischema.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17bf09e8fde06d6abca6cb282d3bfe74195c6b3ea77a0c840d903b728b4441ad
4
- data.tar.gz: a2b7b578d4f54481c6c990626625ad09d7e1314e9d8623e1a1fa9e4e6c8b14a7
3
+ metadata.gz: fe6edc1f3b95435d8fa18f6d2836086050d96276b1397fce0a378ceafbaf2797
4
+ data.tar.gz: 2b23892f25a662c9c788a611cf1417dffcbc65a8bf4ae275cd03bfb220b17337
5
5
  SHA512:
6
- metadata.gz: b1efdd78b40f3980d4b1176f4702f614e3afef78db35e4c1395a744a1bae47fef0f159b24012b880bb1038174ad1e3ea9f4f9111ee21799759baff5bc696032f
7
- data.tar.gz: 3dd739593892acd46186e49959f0f8d5b7680decbaafc9c95b424b40d7d6af0bf7ef9f7c264e38be54506a44b43a4aa90ca5ab8903c5d07b5f99782ffcf0e694
6
+ metadata.gz: 4e3659f172ace66b3b3353e0630f8be2e4baa5c252fa27f22c9def7e37954082a62ed642d082b1015087e2b3126df54c477caa165179e595a401431d84b801cd
7
+ data.tar.gz: ec1de44276cb84f9d2e2ec374ef5be6c62fa7f9655149f572111c62911a2f83c2360450a29724e6b115147c8c467c8a44b4597363bddeb20067c5dd0b27229c7
data/lib/nakischema.rb CHANGED
@@ -3,7 +3,7 @@ module Nakischema
3
3
 
4
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?}#{" #{message}" if message}"
6
+ raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}#{" #{message.respond_to?(:call) ? message[object] : message}" if message}" # TODO: lambda message?
7
7
  end
8
8
  # TODO: maybe move '(at ...)' to the beginning
9
9
  case schema
@@ -22,24 +22,24 @@ module Nakischema
22
22
  # when Fixnum
23
23
  # raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
24
24
  # validate object[k], v, path: [*path, :"##{k}"]
25
- when :keys ; validate object.keys, v, path: [*path, :keys]
26
- when :values ; validate object.values, v, path: [*path, :values]
27
- when :keys_sorted ; validate object.keys.sort, v, path: [*path, :keys_sorted] # TODO: maybe copypaste the Array validation to reduce [] nesting
25
+ when :keys ; validate object.keys, v, message, path: [*path, :keys]
26
+ when :values ; validate object.values, v, message, path: [*path, :values]
27
+ when :keys_sorted ; validate object.keys.sort, v, message, path: [*path, :keys_sorted] # TODO: maybe copypaste the Array validation to reduce [] nesting
28
28
  when :hash_opt ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
29
- v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] if object.key? k }
29
+ v.each{ |k, v| validate object.fetch(k), v, message, path: [*path, k] if object.key? k }
30
30
  when :hash_req ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
31
31
  raise_with_path.call "expected required keys #{v.keys.sort} ∉ #{object.keys.sort}" unless (v.keys - object.keys).empty?
32
- v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] }
32
+ v.each{ |k, v| validate object.fetch(k), v, message, path: [*path, k] }
33
33
  when :hash ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
34
34
  hash_wo_opt = object.keys.sort - schema.fetch(:hash_opt, {}).keys
35
35
  raise_with_path.call "expected implicit keys #{v.keys.sort} != #{hash_wo_opt}" unless v.keys.sort == hash_wo_opt
36
- v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] }
37
- when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, path: [*path, :"key##{i}"] }
38
- when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, path: [*path, :"value##{i}"] }
39
- when :method ; v.each{ |m, e| validate object.public_method(m).call, e, path: [*path, :"method##{m}"] }
36
+ v.each{ |k, v| validate object.fetch(k), v, message, path: [*path, k] }
37
+ when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, message, path: [*path, :"key##{i}"] }
38
+ when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, message, path: [*path, :"value##{i}"] }
39
+ when :method ; v.each{ |m, e| validate object.public_method(m).call, e, message, path: [*path, :"method##{m}"] }
40
40
  when :each
41
41
  raise_with_path.call "expected iterable != #{object.class}" unless object.respond_to? :each_with_index
42
- object.each_with_index{ |e, i| validate e, v, path: [*path, :"##{i}"] }
42
+ object.each_with_index{ |e, i| validate e, v, message, path: [*path, :"##{i}"] }
43
43
  # when :case
44
44
  # 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|
45
45
  # next if begin
@@ -67,12 +67,12 @@ module Nakischema
67
67
  if schema.map(&:class) == [Array]
68
68
  raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
69
69
  raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size} for #{object.inspect}" unless schema[0].size == object.size
70
- object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, path: [*path, :"##{i}"] }
70
+ object.zip(schema[0]).each_with_index{ |(o, v), i| validate o, v, message, path: [*path, :"##{i}"] }
71
71
  else
72
72
  results = schema.lazy.with_index.map do |v, i|
73
73
  # raise_with_path.call "unsupported nested Array" if v.is_a? Array
74
74
  begin
75
- validate object, v, path: [*path, :"variant##{i}"]
75
+ validate object, v, message, path: [*path, :"variant##{i}"]
76
76
  nil
77
77
  rescue Error => e
78
78
  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.2.1"
3
+ spec.version = "0.2.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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakischema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
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: 2023-05-14 00:00:00.000000000 Z
11
+ date: 2023-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: regexp-examples