yaml-schema 1.1.0 → 1.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 579d1085952dfe8a826de14f05a2af2073da12bf45ef5d5959155447a02a9c0d
4
- data.tar.gz: 165be9ca5619b56f47f54ba456e31213662a35fff3f41a21b9d03e0e400feae5
3
+ metadata.gz: e7079554145bf81aef3f0fb89dfa5ad5e0867336866989da48e9217cbae1f114
4
+ data.tar.gz: e6bc2b714f98cce44632e17fc6b9d4d8bbaebea4583427897019ce8d28f81293
5
5
  SHA512:
6
- metadata.gz: eadb3ccf99d89937351e506a95dc95d3527e19569e1e89b6dad29f66a2b5f9ade673015838526494a7a3c5c2b4f487bb5b386236505542759f0330e60207dd44
7
- data.tar.gz: 79132f662fe6b73bed894481f10df139b89839fd7cf97ccd972c8e66bbbaaea6184ef248735c4d5b2695609d72826fa0ff247894f468030a02e1d2548b6d9ee3
6
+ metadata.gz: 347fe6c4f3a01eefc61b4f6a18570893c472fa815f942b9ec57ac9ccd8abd6d7f06d73b0f757dd38dee5c9da5f6e01d8c4dd76e5e726d4b92aa0e4a966126f89
7
+ data.tar.gz: 62a7ec295631d838f06c8932f4c1ae614650cfb635b1bd9bb2539425a53575e1bf87a9af748d1632e082c0f25dc93c7cc311173300c5bf47bfbf3cd11dbc6d41
data/lib/yaml-schema.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YAMLSchema
4
- VERSION = "1.1.0"
4
+ VERSION = "1.2.0"
5
5
 
6
6
  class Pointer
7
7
  include Enumerable
@@ -68,6 +68,7 @@ module YAMLSchema
68
68
  class UnexpectedProperty < Exception; end
69
69
  class UnexpectedTag < Exception; end
70
70
  class UnexpectedValue < Exception; end
71
+ class UnexpectedAlias < Exception; end
71
72
  class InvalidSchema < Exception; end
72
73
  class InvalidString < Exception; end
73
74
  class InvalidPattern < Exception; end
@@ -78,8 +79,8 @@ module YAMLSchema
78
79
  ##
79
80
  # Given a particular schema, validate that the node conforms to the
80
81
  # schema. Raises an exception if it is invalid
81
- def self.validate(schema, node)
82
- INSTANCE.validate schema, node
82
+ def self.validate(schema, node, aliases: true)
83
+ INSTANCE.validate schema, node, aliases: aliases
83
84
  end
84
85
 
85
86
  module NodeInfo # :nodoc:
@@ -97,8 +98,8 @@ module YAMLSchema
97
98
  ##
98
99
  # Given a particular schema, validate that the node conforms to the
99
100
  # schema. Raises an exception if it is invalid
100
- def validate(schema, node)
101
- val = _validate(schema["type"], schema, node, Valid, {}, ["root"])
101
+ def validate(schema, node, aliases: true)
102
+ val = _validate(schema["type"], schema, node, Valid, {}, ["root"], aliases)
102
103
  if val.exception
103
104
  raise val
104
105
  else
@@ -109,8 +110,8 @@ module YAMLSchema
109
110
  ##
110
111
  # Given a particular schema, validate that the node conforms to the
111
112
  # schema. Returns an error object if the node is invalid, otherwise false.
112
- def invalid?(schema, node)
113
- res = _validate(schema["type"], schema, node, Valid, {}, ["root"])
113
+ def invalid?(schema, node, aliases: true)
114
+ res = _validate(schema["type"], schema, node, Valid, {}, ["root"], aliases)
114
115
  if Valid == res
115
116
  false
116
117
  else
@@ -126,11 +127,12 @@ module YAMLSchema
126
127
  ex
127
128
  end
128
129
 
129
- def _validate(type, schema, node, valid, aliases, path)
130
+ def _validate(type, schema, node, valid, aliases, path, allow_aliases)
130
131
  return valid if valid.exception
131
132
 
132
133
  if node.anchor
133
134
  if node.alias?
135
+ raise UnexpectedAlias unless allow_aliases
134
136
  node = aliases[node.anchor]
135
137
  else
136
138
  aliases[node.anchor] = node
@@ -142,7 +144,7 @@ module YAMLSchema
142
144
  if Array === type
143
145
  v = valid
144
146
  type.each do |t|
145
- v = _validate t, schema, node, valid, aliases, path
147
+ v = _validate t, schema, node, valid, aliases, path, allow_aliases
146
148
  unless v.exception
147
149
  break
148
150
  end
@@ -178,7 +180,7 @@ module YAMLSchema
178
180
  properties = schema["properties"].dup
179
181
  key_restriction = schema["propertyNames"] || {}
180
182
  node.children.each_slice(2) do |key, val|
181
- valid = _validate("string", key_restriction, key, valid, aliases, path)
183
+ valid = _validate("string", key_restriction, key, valid, aliases, path, allow_aliases)
182
184
 
183
185
  return valid if valid.exception
184
186
 
@@ -190,7 +192,7 @@ module YAMLSchema
190
192
  end
191
193
  }
192
194
 
193
- valid = _validate(sub_schema["type"], sub_schema, val, valid, aliases, path + [key.value])
195
+ valid = _validate(sub_schema["type"], sub_schema, val, valid, aliases, path + [key.value], allow_aliases)
194
196
 
195
197
  return valid if valid.exception
196
198
  end
@@ -205,9 +207,9 @@ module YAMLSchema
205
207
  if schema["items"]
206
208
  sub_schema = schema["items"]
207
209
  node.children.each_slice(2) do |key, val|
208
- valid = _validate("string", {}, key, valid, aliases, path)
210
+ valid = _validate("string", {}, key, valid, aliases, path, allow_aliases)
209
211
  return valid if valid.exception
210
- valid = _validate(sub_schema["type"], sub_schema, val, valid, aliases, path + [key.value])
212
+ valid = _validate(sub_schema["type"], sub_schema, val, valid, aliases, path + [key.value], allow_aliases)
211
213
  return valid if valid.exception
212
214
  end
213
215
  else
@@ -230,12 +232,12 @@ module YAMLSchema
230
232
  if schema["items"]
231
233
  node.children.each_with_index { |item, i|
232
234
  sub_schema = schema["items"]
233
- valid = _validate sub_schema["type"], sub_schema, item, valid, aliases, path + [i]
235
+ valid = _validate sub_schema["type"], sub_schema, item, valid, aliases, path + [i], allow_aliases
234
236
  }
235
237
  elsif schema["prefixItems"]
236
238
  node.children.each_with_index { |item, i|
237
239
  sub_schema = schema["prefixItems"][i]
238
- valid = _validate sub_schema["type"], sub_schema, item, valid, aliases, path + [i]
240
+ valid = _validate sub_schema["type"], sub_schema, item, valid, aliases, path + [i], allow_aliases
239
241
  }
240
242
  else
241
243
  raise NotImplementedError
@@ -1,6 +1,7 @@
1
1
  require "minitest/autorun"
2
2
  require "yaml-schema"
3
3
  require "psych"
4
+ require "date"
4
5
 
5
6
  module YAMLSchema
6
7
  class Validator
@@ -690,6 +691,24 @@ bar: *1
690
691
  }, ast.children.first)
691
692
  end
692
693
 
694
+ def test_optionally_disallow_aliases
695
+ ast = Psych.parse(<<-eoyml)
696
+ ---
697
+ foo: &1
698
+ - foo
699
+ bar: *1
700
+ eoyml
701
+ assert_raises UnexpectedAlias do
702
+ Validator.validate({
703
+ "type" => "object",
704
+ "properties" => {
705
+ "foo" => { "type" => "array", "items" => { "type" => "string" } },
706
+ "bar" => { "type" => "array", "items" => { "type" => "string" } },
707
+ },
708
+ }, ast.children.first, aliases: false)
709
+ end
710
+ end
711
+
693
712
  class CustomInfo
694
713
  def read_tag(node)
695
714
  if node.tag == "!aaron"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson