nakischema 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nakischema.rb +55 -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: 4d78fcac47a9551f79d2a35208246902c1330e71
4
+ data.tar.gz: 9e4f2ac556c22c26669343e44e489808804c56b9
5
5
  SHA512:
6
- metadata.gz: 91252167fbe10d1808ea86f108fc37d9ac6be0a36b99cccfab36dc2960ba04b161ac153a66071181cd394dc36ab512c4a5d7363a7e8dd3fe86ba39ea7b52b984
7
- data.tar.gz: 422749ffe71639df10e08790406a03687d35e056213c1ad977f41b7ceec22ed727e38a28382af7fab75297cec9a808f373d18581a9584ea0e4d43d1111482e15
6
+ metadata.gz: 1b2e80736ba9165d63dba0d13deb6d7286ab0af153c243a82d5826aa9a3cba996d24011e0f573f5b0b6d131ce89300d09bc693afecb34f987beadc273d51cd4f
7
+ data.tar.gz: 0e4204ab10c47f1bdae7856be58acef6a75cf177fc73855e04154cb4aa03c635054b0b3445133082d8ca6503d3dd69484b80ede8aff6f58a7132fdfe2cbb45ae
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,58 @@ 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 Hash
89
+ schema.each do |k, v|
90
+ case k
91
+ when :size ; raise_with_path.call "expected explicit size #{v} != #{object.size}" unless v.include? object.size
92
+ when :text ; raise_with_path.call "expected text #{v.inspect} != #{object.text.inspect}" unless v == object.text
93
+ when :each ; raise_with_path.call "expected iterable != #{object.class}" unless object.respond_to? :each_with_index
94
+ object.each_with_index{ |e, i| validate_oga_xml e, v, [*path, :"##{i}"] }
95
+ when :exact ; children = object.xpath "./*"
96
+ names = children.map(&:name).uniq
97
+ raise_with_path.call "expected implicit children #{v.keys} != #{names}" unless v.keys == names
98
+ v.each do |k, v|
99
+ selected = children.select{ |_| _.name == k }
100
+ validate_oga_xml selected, v, [*path, k]
101
+ end
102
+ when :req ; v.each{ |k, v| validate_oga_xml object.xpath(k.start_with?("./") ? k : "./#{k}"), v, [*path, k] }
103
+ when :attr_req ; v.each{ |k, v| raise_with_path.call "expected #{v} != #{object[k]}", [*path, k] unless v === object[k] }
104
+ when :assertions
105
+ v.each_with_index do |assertion, i|
106
+ begin
107
+ raise Error.new "custom assertion failed" unless assertion.call object, [*path, :"assertion##{i}"]
108
+ rescue Error => e
109
+ raise_with_path.call e, [*path, :"assertion##{i}"]
110
+ end
111
+ end
112
+ else
113
+ raise_with_path.call "unsupported rule #{k.inspect}"
114
+ end
115
+ end
116
+ when Array
117
+ if schema.map(&:class) == [Array]
118
+ raise_with_path.call "expected implicit size #{schema[0].size} != #{object.size} for #{object.inspect}" unless schema[0].size == object.size
119
+ object.zip(schema[0]).each_with_index{ |(o, v), i| validate_oga_xml o, v, [*path, :"##{i}"] }
120
+ else
121
+ results = schema.lazy.with_index.map do |v, i|
122
+ begin
123
+ validate_oga_xml object, v, [*path, :"variant##{i}"]
124
+ nil
125
+ rescue Error => e
126
+ e
127
+ end
128
+ end
129
+ raise Error.new "expected at least one of #{schema.size} rules to match the #{object.inspect}, errors:\n" +
130
+ results.force.compact.map{ |_| _.to_s.gsub(/^/, " ") }.join("\n") if results.all?
131
+ end
132
+ else
133
+ raise_with_path.call "unsupported rule class #{schema.class}"
134
+ end
135
+ end
81
136
  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.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,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.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: 2021-09-06 00:00:00.000000000 Z
11
+ date: 2021-09-11 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.