yaml-schema 1.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 579d1085952dfe8a826de14f05a2af2073da12bf45ef5d5959155447a02a9c0d
4
- data.tar.gz: 165be9ca5619b56f47f54ba456e31213662a35fff3f41a21b9d03e0e400feae5
3
+ metadata.gz: 162c65cfdc841b1a41db7d8f5d865b51a145361bfd1623723d73ad8c8fd7030a
4
+ data.tar.gz: 6a64e47154f5ed2c8910c588add2c5ff4bceeadea4240ccd3469b52277f5fe9f
5
5
  SHA512:
6
- metadata.gz: eadb3ccf99d89937351e506a95dc95d3527e19569e1e89b6dad29f66a2b5f9ade673015838526494a7a3c5c2b4f487bb5b386236505542759f0330e60207dd44
7
- data.tar.gz: 79132f662fe6b73bed894481f10df139b89839fd7cf97ccd972c8e66bbbaaea6184ef248735c4d5b2695609d72826fa0ff247894f468030a02e1d2548b6d9ee3
6
+ metadata.gz: 1223fa2441b84f33b57c1f826658de489b2d36a4d76de7dd9770832f4a5c12e815aeabbc242d557a76c6f074ab1d344610e1131dd9ea86144ded4c54ac740e23
7
+ data.tar.gz: 3c6758c8c5efec97b10d02ade2bc25c8fe9f7f4301b01602333c0178bd57804f4db3fbb24c390139a8e12981a88a85de72686d1efc628e1ddd086ca8e4b7952c
@@ -7,6 +7,8 @@ on:
7
7
  jobs:
8
8
  ruby-versions:
9
9
  uses: ruby/actions/.github/workflows/ruby_versions.yml@master
10
+ with:
11
+ engine: cruby
10
12
 
11
13
  test:
12
14
  needs: ruby-versions
data/README.md CHANGED
@@ -11,14 +11,14 @@ A library for validating YAML documents against a schema.
11
11
  ### Basic Validation
12
12
 
13
13
  ```ruby
14
- require 'yaml-schema'
14
+ require "yaml-schema"
15
15
 
16
16
  schema = {
17
- type: 'object',
18
- properties: {
19
- name: { type: 'string' },
20
- age: { type: 'integer' },
21
- active: { type: 'boolean' }
17
+ "type" => "object",
18
+ "properties" => {
19
+ "name" => { "type" => "string" },
20
+ "age" => { "type" => "integer" },
21
+ "active" => { "type" => "boolean" }
22
22
  }
23
23
  }
24
24
 
@@ -53,9 +53,9 @@ Specify multiple valid types for a field:
53
53
 
54
54
  ```ruby
55
55
  schema = {
56
- type: 'object',
57
- properties: {
58
- value: { type: ['null', 'string'] }
56
+ "type" => "object",
57
+ "properties" => {
58
+ "value" => { "type" => ["null", "string"] }
59
59
  }
60
60
  }
61
61
  ```
@@ -64,17 +64,17 @@ schema = {
64
64
 
65
65
  ```ruby
66
66
  schema = {
67
- type: 'object',
68
- properties: {
69
- users: {
70
- type: 'array',
71
- items: {
72
- type: 'object',
73
- properties: {
74
- name: { type: 'string' },
75
- tags: {
76
- type: 'array',
77
- items: { type: 'string' }
67
+ "type" => "object",
68
+ "properties" => {
69
+ "users" => {
70
+ "type" => "array",
71
+ "items" => {
72
+ "type" => "object",
73
+ "properties" => {
74
+ "name" => { "type" => "string" },
75
+ "tags" => {
76
+ "type" => "array",
77
+ "items" => { "type" => "string" }
78
78
  }
79
79
  }
80
80
  }
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.1"
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,17 +79,17 @@ 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:
86
- def self.read_tag(node)
87
+ def self.read_tag node
87
88
  node.tag
88
89
  end
89
90
  end
90
91
 
91
- def initialize(node_info = NodeInfo)
92
+ def initialize node_info = NodeInfo
92
93
  @node_info = node_info
93
94
  end
94
95
 
@@ -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, {}, [], 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, {}, [], aliases)
114
115
  if Valid == res
115
116
  false
116
117
  else
@@ -120,17 +121,18 @@ module YAMLSchema
120
121
 
121
122
  private
122
123
 
123
- def make_error(klass, msg, path)
124
- ex = klass.new msg + " path: #{path.join(" -> ")}"
124
+ def make_error klass, msg, path
125
+ ex = klass.new msg + " path: /#{path.join("/")}"
125
126
  ex.set_backtrace caller
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
 
@@ -186,11 +188,11 @@ module YAMLSchema
186
188
  if schema["additionalProperties"]
187
189
  schema["additionalProperties"]
188
190
  else
189
- return make_error UnexpectedProperty, "unknown property #{key.value.dump}", path
191
+ return make_error UnexpectedProperty, "unknown property #{key.value.dump}", path + [key.value]
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
@@ -287,7 +289,7 @@ module YAMLSchema
287
289
  when "integer", "float", "time", "date", "symbol"
288
290
  found_type = extract_type(node.value)
289
291
  unless found_type == type.to_sym
290
- return make_error UnexpectedValue, "expected #{type}, got #{type}", path
292
+ return make_error UnexpectedValue, "expected #{type}, got #{found_type}", path
291
293
  end
292
294
  else
293
295
  raise "unknown type #{schema["type"]}"
@@ -313,7 +315,7 @@ module YAMLSchema
313
315
  |[-+]?0x[_]*[0-9a-fA-F][0-9a-fA-F_]* (?# base 16))$/x
314
316
 
315
317
  # Tokenize +string+ returning the Ruby object
316
- def extract_type(string)
318
+ def extract_type string
317
319
  return :null if string.empty?
318
320
  # Check for a String type, being careful not to get caught by hash keys, hex values, and
319
321
  # special floats (e.g., -.inf).
@@ -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"
data/yaml-schema.gemspec CHANGED
@@ -15,5 +15,5 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_development_dependency 'rake', '~> 13.0'
17
17
  s.add_development_dependency 'psych', '~> 5.0'
18
- s.add_development_dependency 'minitest', '~> 5.15'
18
+ s.add_development_dependency 'minitest', '>= 5.15'
19
19
  end
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -41,14 +41,14 @@ dependencies:
41
41
  name: minitest
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - "~>"
44
+ - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '5.15'
47
47
  type: :development
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - "~>"
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: '5.15'
54
54
  description: If you need to validate YAML against a schema, use this
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  requirements: []
89
- rubygems_version: 3.6.9
89
+ rubygems_version: 4.0.6
90
90
  specification_version: 4
91
91
  summary: Validate YAML against a schema
92
92
  test_files: