nakischema 0.1.3 → 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 +25 -23
  3. data/nakischema.gemspec +1 -1
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f12dd71c0e349bb4c32d404317b64e8f4bfe2d4c
4
- data.tar.gz: 56ade8e30f239f41d315b6aefa2bef82e434a605
2
+ SHA256:
3
+ metadata.gz: d7b23047f5d6ea56c755489cc2e2238db4d3c550da31cc8f03a3f91d820ee465
4
+ data.tar.gz: 0a0feaf7a0642ad4a42d3ef62aafd34e1a6f20e7dc0b78712d2604c384d6a811
5
5
  SHA512:
6
- metadata.gz: b32ad0bd582d777c3a8cf03878b359d95278e0690625a4c6184bf26fff4050e60e972539270799a6927f6dc609d18c3edd63cbf92653b01f4f2f387dff973951
7
- data.tar.gz: a014ed22afe9721d23b70eefe0a012188d001db8f8fe179be2c9b0d5557a3e3aa15d5d813ebe78e603cefeb72787c5efb9baf1b17dfc2bd916d6cf7d8c4c95c4
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
@@ -96,12 +98,12 @@ module Nakischema
96
98
  when :exact ; children = object.xpath "./*"
97
99
  names = children.map(&:name).uniq
98
100
  raise_with_path.call "expected implicit children #{v.keys} != #{names}" unless v.keys == names
99
- v.each do |k, v|
100
- selected = children.select{ |_| _.name == k }
101
- validate_oga_xml selected, v, [*path, k]
102
- end
103
- when :children ; v.each{ |k, v| validate_oga_xml object.xpath(k.start_with?("./") ? k : "./#{k}"), v, [*path, k] }
104
- when :attr_req ; v.each{ |k, v| validate_oga_xml object[k], v, [*path, k] }
101
+ v.each{ |k, v| validate_oga_xml children.select{ |_| _.name == k }, v, [*path, k] }
102
+ when :children ; v.each{ |k, v| validate_oga_xml object.xpath(k.start_with?("./") ? k : "./#{k}"), v, [*path, k] }
103
+ when :attr_exact ; names = object.attributes.map &:name
104
+ raise_with_path.call "expected implicit attributes #{v.keys} != #{names}" unless v.keys == names
105
+ v.each{ |k, v| validate_oga_xml object[k], v, [*path, k] }
106
+ when :attr_req ; v.each{ |k, v| validate_oga_xml object[k], v, [*path, k] }
105
107
  when :assertions
106
108
  v.each_with_index do |assertion, i|
107
109
  begin
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.3"
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.3
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