nakischema 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dd008df051c38e237c7f65fe70c0680eb06c407
4
- data.tar.gz: ff26282070eaad84c5d5d628bc7868b7857c1b9a
3
+ metadata.gz: 66f0fa36c6867bf9d4ae1e6388522a98baa5ce53
4
+ data.tar.gz: 3fa3398cecfcb67fa6428e0d69898f3bfc5a5af1
5
5
  SHA512:
6
- metadata.gz: ac740957dfad68d5527fe0e0a1c70adb2169563e61b922ec5660a8b298947702503568f764128a8f9f89ac87a0cfa4018490b813d4a938dea43b28b149797870
7
- data.tar.gz: 2db4bc15ed4f118373d621890c0351f015ce337e6ac89ad0fe8e6914090169739bb9fe437d830c98fcf22ca93749cc5616cf6bf8c02ce930fb4ea3058ae398aa
6
+ metadata.gz: 573bc852f4e8d2af49d856247818d3d7b264fa3aa3df05b306136eb35d72f3979831221bce80859eab539ec591b96a00283d2080a38b0b46495288e947623f78
7
+ data.tar.gz: '0504529ee39e1306ec3c5469bb3a2a649f5738a61965ff8613f944581e07a041e32785173ec8c48bcf24beba2b41d2d9a5620e8b87e70cad359db4ee43e490c7'
data/lib/nakischema.rb CHANGED
@@ -2,11 +2,11 @@ module Nakischema
2
2
  Error = Class.new RuntimeError
3
3
  def self.validate object, schema, path = []
4
4
  raise_with_path = lambda do |msg|
5
- raise Error.new "#{msg}#{" (at #{path})" unless path.empty?}"
5
+ raise Error.new "#{msg}#{" (at #{path})" unless path.empty?}" # TODO: maybe move '(at ...)' to the beginning
6
6
  end
7
7
  case schema
8
8
  when Hash
9
- raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys_sorted keys values }).empty?
9
+ raise_with_path.call "expected Hash != #{object.class}" unless object.is_a? Hash unless (schema.keys & %i{ keys each_key each_value }).empty?
10
10
  raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array unless (schema.keys & %i{ size }).empty?
11
11
  schema.each do |k, v|
12
12
  case k
@@ -16,10 +16,10 @@ module Nakischema
16
16
  # raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
17
17
  # validate object[k], v, [*path, "##{k}"]
18
18
  when :keys ; validate object.keys, v, [*path, :keys]
19
- when :hash_opt ; v.each{ |k, v| validate object[k], v, [*path, k] if object.key? k }
20
- when :hash
21
- raise_with_path.call "expected implicit keys #{v} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
22
- v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
19
+ when :hash_opt ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] if object.key? k }
20
+ when :hash_req ; v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
21
+ when :hash ; raise_with_path.call "expected implicit keys #{v.keys} != #{object.keys.sort}" unless v.keys.sort == object.keys.sort
22
+ v.each{ |k, v| validate object.fetch(k), v, [*path, k] }
23
23
  when :each_key ; object.keys.each_with_index{ |k, i| validate k, v, [*path, :"key##{i}"] }
24
24
  when :each_value ; object.values.each_with_index{ |v_, i| validate v_, v, [*path, :"value##{i}"] }
25
25
  when :each
@@ -40,9 +40,9 @@ module Nakischema
40
40
  else ; raise_with_path.call "unsupported rule #{k.inspect}"
41
41
  end
42
42
  end
43
- when NilClass, TrueClass, FalseClass, String ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
44
- when Regexp ; raise_with_path.call "expected #{schema } != #{object.inspect}" unless schema === object
45
- when Range ; raise_with_path.call "expected #{schema } != #{object }" unless schema.include? object
43
+ when NilClass, TrueClass, FalseClass, String, Symbol ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema == object
44
+ when Regexp ; raise_with_path.call "expected #{schema } != #{object.inspect}" unless schema === object
45
+ when Range ; raise_with_path.call "expected #{schema } != #{object }" unless schema.include? object
46
46
  when Array
47
47
  if schema.map(&:class) == [Array]
48
48
  raise_with_path.call "expected Array != #{object.class}" unless object.is_a? Array
data/nakischema.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "nakischema"
3
- spec.version = "0.0.0"
3
+ spec.version = "0.0.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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakischema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon