nakischema 0.1.0 → 0.1.4

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 +56 -0
  3. data/nakischema.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1ee5445ac066ed5b77d5dd3554d96a4c7723e26
4
- data.tar.gz: 4302db46b28684a12f8c8b259c8597373355a011
3
+ metadata.gz: 271b63150c8a6eead2fba711f312b2e2f82ba24a
4
+ data.tar.gz: 829b656fc7a75c629d3ccea2ac44ca4de98d4598
5
5
  SHA512:
6
- metadata.gz: 91252167fbe10d1808ea86f108fc37d9ac6be0a36b99cccfab36dc2960ba04b161ac153a66071181cd394dc36ab512c4a5d7363a7e8dd3fe86ba39ea7b52b984
7
- data.tar.gz: 422749ffe71639df10e08790406a03687d35e056213c1ad977f41b7ceec22ed727e38a28382af7fab75297cec9a808f373d18581a9584ea0e4d43d1111482e15
6
+ metadata.gz: 07bd982a7331b8ec0937b97d682b623d4c85ac14bcb71512dc85bc16536f40f8a01dfcac0df379097614f84748a93d2d426c2addb45b0f0ca703ee1b0c069ef6
7
+ data.tar.gz: c2716c3ad3e8e804891ef364c31ca7ffc3a2289b16a32f203905362b866bc3f832932be3a0350c25edd41b60d7d22c3b1739d2e279aebd0f1987d76655e07ee2
data/lib/nakischema.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Nakischema
2
2
  Error = Class.new RuntimeError
3
+
3
4
  def self.validate object, schema, path = []
4
5
  raise_with_path = lambda do |msg, _path = path|
5
6
  raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}"
@@ -78,4 +79,59 @@ module Nakischema
78
79
  raise_with_path.call "unsupported rule class #{schema.class}"
79
80
  end
80
81
  end
82
+
83
+ def self.validate_oga_xml object, schema, path = []
84
+ raise_with_path = lambda do |msg, _path = path|
85
+ raise Error.new "#{msg}#{" (at #{_path})" unless _path.empty?}"
86
+ end
87
+ case schema
88
+ when String, Regexp ; raise_with_path.call "expected #{schema.inspect} != #{object.inspect}" unless schema === object
89
+ when Hash
90
+ schema.each do |k, v|
91
+ case k
92
+ when :size ; raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
93
+ when :text ; raise_with_path.call "expected text #{v.inspect} != #{object.text.inspect}" unless v == object.text
94
+ when :each ; raise_with_path.call "expected iterable != #{object.class}" unless object.respond_to? :each_with_index
95
+ object.each_with_index{ |e, i| validate_oga_xml e, v, [*path, :"##{i}"] }
96
+ when :exact ; children = object.xpath "./*"
97
+ names = children.map(&:name).uniq
98
+ raise_with_path.call "expected implicit children #{v.keys} != #{names}" unless v.keys == names
99
+ v.each{ |k, v| validate_oga_xml children.select{ |_| _.name == k }, v, [*path, k] }
100
+ when :children ; v.each{ |k, v| validate_oga_xml object.xpath(k.start_with?("./") ? k : "./#{k}"), v, [*path, k] }
101
+ when :attr_exact ; names = object.attributes.map &:name
102
+ raise_with_path.call "expected implicit attributes #{v.keys} != #{names}" unless v.keys == names
103
+ v.each{ |k, v| validate_oga_xml object[k], v, [*path, k] }
104
+ when :attr_req ; v.each{ |k, v| validate_oga_xml object[k], v, [*path, k] }
105
+ when :assertions
106
+ v.each_with_index do |assertion, i|
107
+ begin
108
+ raise Error.new "custom assertion failed" unless assertion.call object, [*path, :"assertion##{i}"]
109
+ rescue Error => e
110
+ raise_with_path.call e, [*path, :"assertion##{i}"]
111
+ end
112
+ end
113
+ else
114
+ raise_with_path.call "unsupported rule #{k.inspect}"
115
+ end
116
+ end
117
+ when Array
118
+ if schema.map(&:class) == [Array]
119
+ raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size} for #{object.inspect}" unless schema[0].size == object.size
120
+ object.zip(schema[0]).each_with_index{ |(o, v), i| validate_oga_xml o, v, [*path, :"##{i}"] }
121
+ else
122
+ results = schema.lazy.with_index.map do |v, i|
123
+ begin
124
+ validate_oga_xml object, v, [*path, :"variant##{i}"]
125
+ nil
126
+ rescue Error => e
127
+ e
128
+ end
129
+ end
130
+ raise Error.new "expected at least one of #{schema.size} rules to match the #{object.inspect}, errors:\n" +
131
+ results.force.compact.map{ |_| _.to_s.gsub(/^/, " ") }.join("\n") if results.all?
132
+ end
133
+ else
134
+ raise_with_path.call "unsupported rule class #{schema.class}"
135
+ end
136
+ end
81
137
  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.1.0"
3
+ spec.version = "0.1.4"
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.0
4
+ version: 0.1.4
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-06 00:00:00.000000000 Z
11
+ date: 2021-09-12 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.