Skip to content

Commit

Permalink
#valid?; several fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakilon committed Oct 11, 2023
1 parent 9970bd7 commit 253f619
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
35 changes: 23 additions & 12 deletions lib/nakischema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.validate object, schema, message = nil, path: []
end
# TODO: maybe move '(at ...)' to the beginning
case schema
when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema === object
# TODO: maybe deprecate the NilClass, TrueClass, FalseClass since they can be asserted via the next case branch
when Class ; raise_with_path.call "expected #{schema } != #{object.class }" unless schema === object
when Regexp ; raise_with_path.call "expected #{schema } != #{object.inspect}" unless schema === object
Expand Down Expand Up @@ -86,6 +86,13 @@ def self.validate object, schema, message = nil, path: []
end
end

def self.valid? object, schema
validate object, schema
true
rescue Error
false
end

def self.validate_oga_xml object, schema, path = []
raise_with_path = lambda do |msg, _path = path|
raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}"
Expand Down Expand Up @@ -141,28 +148,32 @@ def self.validate_oga_xml object, schema, path = []
end
end

def self.fixture _
def self.fixture _, no_shuffle = false
require "regexp-examples"
require "addressable"
case _
when NilClass ; nil
when Hash
case _.keys
when %i{ hash } ; _[:hash ].map{ |k,v| [k,fixture(v)] }.shuffle.to_h
when %i{ hash_req } ; [*_[:hash_req].map{ |k,v| [k,fixture(v)] }, ["foo","bar"]].shuffle.to_h # TODO: assert no collision
when %i{ size each } ; Array.new(fixture _[:size]){ fixture _[:each] }
when %i{ hash } ; _[:hash ].map{ |k,v| [k,fixture(v,no_shuffle)] } .then{ |_| no_shuffle ? _ : _.shuffle }.to_h
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
when %i{ size each } ; Array.new(_[:size].max){ fixture _[:each], no_shuffle }
else ; fail _.keys.inspect
end
when Array ; [Array] == _.map(&:class) ? _[0].map(&method(:fixture)) : fixture(_.sample)
when Array ; [Array] == _.map(&:class) ? _[0].map{ |_| fixture _, no_shuffle } : fixture(_.sample, no_shuffle)
when Regexp
t = _.random_example
tt = begin
URI t
begin
URI(t = _.random_example)
rescue URI::InvalidURIError
URI Addressable::URI.escape t
end
tt.is_a?(URI::HTTP) ? tt.to_s : t
begin
URI Addressable::URI.escape t
rescue Addressable::URI::InvalidURIError
t
end
end.to_s
when Range ; rand _
when String ; _
when TrueClass ; true
when Class
case _.name
when "Integer" ; -rand(1000000)
Expand Down
2 changes: 1 addition & 1 deletion nakischema.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = "nakischema"
spec.version = "0.2.3"
spec.version = "0.3.0"
spec.summary = "compact yet powerful arbitrary nested objects validator"
spec.description = "The most compact yet powerful arbitrary nested objects validator. Especially handy to validate JSONs."

Expand Down

0 comments on commit 253f619

Please sign in to comment.