nakischema 0.2.0 → 0.2.1

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 +41 -4
  3. data/nakischema.gemspec +4 -1
  4. metadata +31 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7b23047f5d6ea56c755489cc2e2238db4d3c550da31cc8f03a3f91d820ee465
4
- data.tar.gz: 0a0feaf7a0642ad4a42d3ef62aafd34e1a6f20e7dc0b78712d2604c384d6a811
3
+ metadata.gz: 17bf09e8fde06d6abca6cb282d3bfe74195c6b3ea77a0c840d903b728b4441ad
4
+ data.tar.gz: a2b7b578d4f54481c6c990626625ad09d7e1314e9d8623e1a1fa9e4e6c8b14a7
5
5
  SHA512:
6
- metadata.gz: 8d252b3a8fd89e90531c896b30e381121ce16b024480c8d87ede34c4267a0cf03a0278e6e1a356a10c126662aebb16b456b6c2914dbb4591eeab66d41cdc9ec1
7
- data.tar.gz: b7e3989528ac2bf4e233fd30b29da1cc2d0491838c26d6adbf4b014f30eae2015caaab61f9f8566af18aa31a62d23464143dba02dda73d920d8fa4c6301a4475
6
+ metadata.gz: b1efdd78b40f3980d4b1176f4702f614e3afef78db35e4c1395a744a1bae47fef0f159b24012b880bb1038174ad1e3ea9f4f9111ee21799759baff5bc696032f
7
+ data.tar.gz: 3dd739593892acd46186e49959f0f8d5b7680decbaafc9c95b424b40d7d6af0bf7ef9f7c264e38be54506a44b43a4aa90ca5ab8903c5d07b5f99782ffcf0e694
data/lib/nakischema.rb CHANGED
@@ -17,18 +17,22 @@ module Nakischema
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?
18
18
  schema.each do |k, v|
19
19
  case k
20
- when :size ; raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
20
+ when :size ; raise_with_path.call "expected Range != #{v.class}" unless v.is_a? Range
21
+ raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
21
22
  # when Fixnum
22
23
  # raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
23
24
  # validate object[k], v, path: [*path, :"##{k}"]
24
25
  when :keys ; validate object.keys, v, path: [*path, :keys]
25
26
  when :values ; validate object.values, v, path: [*path, :values]
26
27
  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?
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 }
30
+ when :hash_req ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
31
+ raise_with_path.call "expected required keys #{v.keys.sort} ∉ #{object.keys.sort}" unless (v.keys - object.keys).empty?
29
32
  v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] }
30
33
  when :hash ; raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash
31
- raise_with_path.call "expected implicit keys #{v.keys.sort} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
34
+ hash_wo_opt = object.keys.sort - schema.fetch(:hash_opt, {}).keys
35
+ raise_with_path.call "expected implicit keys #{v.keys.sort} != #{hash_wo_opt}" unless v.keys.sort == hash_wo_opt
32
36
  v.each{ |k, v| validate object.fetch(k), v, path: [*path, k] }
33
37
  when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, path: [*path, :"key##{i}"] }
34
38
  when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, path: [*path, :"value##{i}"] }
@@ -136,4 +140,37 @@ module Nakischema
136
140
  raise_with_path.call "unsupported rule class #{schema.class}"
137
141
  end
138
142
  end
143
+
144
+ def self.fixture _
145
+ require "regexp-examples"
146
+ require "addressable"
147
+ case _
148
+ when Hash
149
+ case _.keys
150
+ when %i{ hash } ; _[:hash ].map{ |k,v| [k,fixture(v)] }.shuffle.to_h
151
+ when %i{ hash_req } ; [*_[:hash_req].map{ |k,v| [k,fixture(v)] }, ["foo","bar"]].shuffle.to_h # TODO: assert no collision
152
+ when %i{ size each } ; Array.new(fixture _[:size]){ fixture _[:each] }
153
+ else ; fail _.keys.inspect
154
+ end
155
+ when Array ; [Array] == _.map(&:class) ? _[0].map(&method(:fixture)) : fixture(_.sample)
156
+ when Regexp
157
+ t = _.random_example
158
+ tt = begin
159
+ URI t
160
+ rescue URI::InvalidURIError
161
+ URI Addressable::URI.escape t
162
+ end
163
+ tt.is_a?(URI::HTTP) ? tt.to_s : t
164
+ when Range ; rand _
165
+ when String ; _
166
+ when Class
167
+ case _.name
168
+ when "Integer" ; -rand(1000000)
169
+ when "Hash" ; {}
170
+ else ; fail "bad fixture node class name: #{_.name}"
171
+ end
172
+ else ; fail "bad fixture node class: #{_.class.inspect}"
173
+ end
174
+ end
175
+
139
176
  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.0"
3
+ spec.version = "0.2.1"
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
 
@@ -9,5 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.license = "MIT"
10
10
  spec.metadata = {"source_code_uri" => "https://github.com/nakilon/nakischema"}
11
11
 
12
+ spec.add_dependency "regexp-examples"
13
+ spec.add_dependency "addressable"
14
+
12
15
  spec.files = %w{ LICENSE nakischema.gemspec lib/nakischema.rb }
13
16
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakischema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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: 2022-11-22 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: regexp-examples
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: The most compact yet powerful arbitrary nested objects validator. Especially
14
42
  handy to validate JSONs.
15
43
  email: nakilon@gmail.com