nakischema 0.2.1 → 0.2.3

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 +15 -14
  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: f6b78ac714d96363a641affd005c7ba1b2c316591b83d95f5d02f0caccc1761b
4
+ data.tar.gz: 2780c15868fc68411189254543426b01714a76431b3d42960c02750a7ff99c61
5
5
  SHA512:
6
- metadata.gz: b1efdd78b40f3980d4b1176f4702f614e3afef78db35e4c1395a744a1bae47fef0f159b24012b880bb1038174ad1e3ea9f4f9111ee21799759baff5bc696032f
7
- data.tar.gz: 3dd739593892acd46186e49959f0f8d5b7680decbaafc9c95b424b40d7d6af0bf7ef9f7c264e38be54506a44b43a4aa90ca5ab8903c5d07b5f99782ffcf0e694
6
+ metadata.gz: 8524b72b2f0d9d29e79e0c1c1f66b87f01105d421e4e959198d850388ad2fa9ceec24ff0cf819ed35891b46b27f7bbdee3f77c263baeae67fa7ed909418e1438
7
+ data.tar.gz: 339416a5c8e5bacef02ae4d83124fffff7f7bc46a12d1bd1981b1c42246058b6f6f76ad08820ebd64b11cd39d809014d47e9ce4a6a828f719a07f1bbd06a683e
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
@@ -11,7 +11,7 @@ module Nakischema
11
11
  # TODO: maybe deprecate the NilClass, TrueClass, FalseClass since they can be asserted via the next case branch
12
12
  when Class ; raise_with_path.call "expected #{schema } != #{object.class }" unless schema === object
13
13
  when Regexp ; raise_with_path.call "expected #{schema } != #{object.inspect}" unless schema === object
14
- when Range ; raise_with_path.call "expected #{schema } != #{object }" unless schema.include? object
14
+ when Range ; raise_with_path.call "expected #{schema } != #{object.inspect}" 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
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?
@@ -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
@@ -166,6 +166,7 @@ module Nakischema
166
166
  when Class
167
167
  case _.name
168
168
  when "Integer" ; -rand(1000000)
169
+ when "String" ; SecureRandom.random_bytes(1000).force_encoding("utf-8").scrub
169
170
  when "Hash" ; {}
170
171
  else ; fail "bad fixture node class name: #{_.name}"
171
172
  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.2.1"
3
+ spec.version = "0.2.3"
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.3
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-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: regexp-examples