nakischema 0.2.2 → 0.3.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 +4 -4
  2. data/lib/nakischema.rb +25 -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: fe6edc1f3b95435d8fa18f6d2836086050d96276b1397fce0a378ceafbaf2797
4
- data.tar.gz: 2b23892f25a662c9c788a611cf1417dffcbc65a8bf4ae275cd03bfb220b17337
3
+ metadata.gz: 63a4f15494ac50f198c795a569e50304326cbb309e5a30bd0b0446c702da5212
4
+ data.tar.gz: a20e6a51a422b271b1bb98d5f4be387bd3aa89fa8dc1ac5e8701b3376bd6ba82
5
5
  SHA512:
6
- metadata.gz: 4e3659f172ace66b3b3353e0630f8be2e4baa5c252fa27f22c9def7e37954082a62ed642d082b1015087e2b3126df54c477caa165179e595a401431d84b801cd
7
- data.tar.gz: ec1de44276cb84f9d2e2ec374ef5be6c62fa7f9655149f572111c62911a2f83c2360450a29724e6b115147c8c467c8a44b4597363bddeb20067c5dd0b27229c7
6
+ metadata.gz: 2e702f55dffb989ae23ab8c0b9b4d0ad2fc2fdf7f1da3e1b90528b3c370c7bd39409fc49d844e65b2d13a1174c153c8f8dab1d17950c3981757d5f680477b7b8
7
+ data.tar.gz: 5ae028759edb1366dce9203a6cc68c15e23875efaa2ac47dcb3432fc1324ad08badb2d21772fe069735f64d3127063b4703598f41abfc163d6207a509f6636de
data/lib/nakischema.rb CHANGED
@@ -7,11 +7,11 @@ module Nakischema
7
7
  end
8
8
  # TODO: maybe move '(at ...)' to the beginning
9
9
  case schema
10
- when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
10
+ when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema === object
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?
@@ -86,6 +86,13 @@ module Nakischema
86
86
  end
87
87
  end
88
88
 
89
+ def self.valid? object, schema
90
+ validate object, schema
91
+ true
92
+ rescue Error
93
+ false
94
+ end
95
+
89
96
  def self.validate_oga_xml object, schema, path = []
90
97
  raise_with_path = lambda do |msg, _path = path|
91
98
  raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}"
@@ -141,31 +148,36 @@ module Nakischema
141
148
  end
142
149
  end
143
150
 
144
- def self.fixture _
151
+ def self.fixture _, no_shuffle = false
145
152
  require "regexp-examples"
146
153
  require "addressable"
147
154
  case _
155
+ when NilClass ; nil
148
156
  when Hash
149
157
  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] }
158
+ when %i{ hash } ; _[:hash ].map{ |k,v| [k,fixture(v,no_shuffle)] } .then{ |_| no_shuffle ? _ : _.shuffle }.to_h
159
+ when %i{ hash_req } ; [*_[:hash_req].map{ |k,v| [k,fixture(v,no_shuffle)] }, ["foo","bar"]].then{ |_| no_shuffle ? _ : _.shuffle }.to_h # TODO: assert no collision
160
+ when %i{ size each } ; Array.new(_[:size].max){ fixture _[:each], no_shuffle }
153
161
  else ; fail _.keys.inspect
154
162
  end
155
- when Array ; [Array] == _.map(&:class) ? _[0].map(&method(:fixture)) : fixture(_.sample)
163
+ when Array ; [Array] == _.map(&:class) ? _[0].map{ |_| fixture _, no_shuffle } : fixture(_.sample, no_shuffle)
156
164
  when Regexp
157
- t = _.random_example
158
- tt = begin
159
- URI t
165
+ begin
166
+ URI(t = _.random_example)
160
167
  rescue URI::InvalidURIError
161
- URI Addressable::URI.escape t
162
- end
163
- tt.is_a?(URI::HTTP) ? tt.to_s : t
168
+ begin
169
+ URI Addressable::URI.escape t
170
+ rescue Addressable::URI::InvalidURIError
171
+ t
172
+ end
173
+ end.to_s
164
174
  when Range ; rand _
165
175
  when String ; _
176
+ when TrueClass ; true
166
177
  when Class
167
178
  case _.name
168
179
  when "Integer" ; -rand(1000000)
180
+ when "String" ; SecureRandom.random_bytes(1000).force_encoding("utf-8").scrub
169
181
  when "Hash" ; {}
170
182
  else ; fail "bad fixture node class name: #{_.name}"
171
183
  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.2"
3
+ spec.version = "0.3.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.2.2
4
+ version: 0.3.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: 2023-08-03 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: regexp-examples