opennebula 6.10.3 → 6.99.85.pre
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 +4 -4
- data/lib/cloud/CloudClient.rb +3 -3
- data/lib/models/role.rb +349 -823
- data/lib/models/service.rb +156 -80
- data/lib/models/vmrole.rb +703 -0
- data/lib/models/vrrole.rb +284 -0
- data/lib/models.rb +3 -1
- data/lib/opennebula/acl.rb +1 -1
- data/lib/opennebula/acl_pool.rb +1 -1
- data/lib/opennebula/backupjob.rb +1 -1
- data/lib/opennebula/backupjob_pool.rb +1 -1
- data/lib/opennebula/client.rb +1 -1
- data/lib/opennebula/cluster.rb +45 -2
- data/lib/opennebula/cluster_pool.rb +1 -1
- data/lib/opennebula/datastore.rb +1 -1
- data/lib/opennebula/datastore_pool.rb +1 -1
- data/lib/opennebula/document.rb +1 -1
- data/lib/opennebula/document_json.rb +1 -1
- data/lib/opennebula/document_pool.rb +1 -1
- data/lib/opennebula/document_pool_json.rb +1 -1
- data/lib/opennebula/error.rb +1 -1
- data/lib/opennebula/flow/grammar.rb +1 -1
- data/lib/opennebula/flow/service_pool.rb +1 -1
- data/lib/opennebula/flow/service_template.rb +353 -97
- data/lib/opennebula/flow/service_template_ext.rb +3 -3
- data/lib/opennebula/flow/service_template_pool.rb +1 -1
- data/lib/opennebula/flow/validator.rb +458 -410
- data/lib/opennebula/flow.rb +1 -1
- data/lib/opennebula/group.rb +1 -1
- data/lib/opennebula/group_pool.rb +1 -1
- data/lib/opennebula/hook.rb +1 -1
- data/lib/opennebula/hook_log.rb +1 -1
- data/lib/opennebula/hook_pool.rb +1 -1
- data/lib/opennebula/host.rb +1 -60
- data/lib/opennebula/host_pool.rb +1 -1
- data/lib/opennebula/image.rb +1 -1
- data/lib/opennebula/image_pool.rb +1 -1
- data/lib/opennebula/ldap_auth.rb +1 -1
- data/lib/opennebula/ldap_auth_spec.rb +1 -1
- data/lib/opennebula/lockable_ext.rb +1 -1
- data/lib/opennebula/marketplace.rb +1 -1
- data/lib/opennebula/marketplace_pool.rb +1 -1
- data/lib/opennebula/marketplaceapp.rb +1 -1
- data/lib/opennebula/marketplaceapp_ext.rb +14 -211
- data/lib/opennebula/marketplaceapp_pool.rb +1 -1
- data/lib/opennebula/oneflow_client.rb +11 -9
- data/lib/opennebula/pool.rb +1 -1
- data/lib/opennebula/pool_element.rb +1 -1
- data/lib/opennebula/security_group.rb +1 -1
- data/lib/opennebula/security_group_pool.rb +1 -1
- data/lib/opennebula/server_cipher_auth.rb +1 -1
- data/lib/opennebula/server_x509_auth.rb +1 -1
- data/lib/opennebula/ssh_auth.rb +1 -1
- data/lib/opennebula/system.rb +1 -1
- data/lib/opennebula/template.rb +1 -1
- data/lib/opennebula/template_ext.rb +1 -1
- data/lib/opennebula/template_pool.rb +1 -1
- data/lib/opennebula/user.rb +1 -1
- data/lib/opennebula/user_pool.rb +1 -1
- data/lib/opennebula/utils.rb +2 -2
- data/lib/opennebula/vdc.rb +1 -1
- data/lib/opennebula/vdc_pool.rb +1 -1
- data/lib/opennebula/virtual_machine.rb +3 -12
- data/lib/opennebula/virtual_machine_ext.rb +2 -31
- data/lib/opennebula/virtual_machine_pool.rb +1 -1
- data/lib/opennebula/virtual_network.rb +1 -1
- data/lib/opennebula/virtual_network_pool.rb +1 -1
- data/lib/opennebula/virtual_router.rb +1 -1
- data/lib/opennebula/virtual_router_pool.rb +1 -1
- data/lib/opennebula/vm_group.rb +1 -1
- data/lib/opennebula/vm_group_pool.rb +1 -1
- data/lib/opennebula/vntemplate.rb +1 -1
- data/lib/opennebula/vntemplate_pool.rb +1 -1
- data/lib/opennebula/wait_ext.rb +1 -1
- data/lib/opennebula/x509_auth.rb +1 -1
- data/lib/opennebula/xml_element.rb +2 -2
- data/lib/opennebula/xml_pool.rb +1 -1
- data/lib/opennebula/xml_utils.rb +1 -1
- data/lib/opennebula/zone.rb +1 -1
- data/lib/opennebula/zone_pool.rb +1 -1
- data/lib/opennebula.rb +2 -2
- metadata +6 -67
- data/lib/ActionManager.rb +0 -280
- data/lib/CommandManager.rb +0 -328
- data/lib/DriverExecHelper.rb +0 -213
- data/lib/HostSyncManager.rb +0 -111
- data/lib/OpenNebulaDriver.rb +0 -223
- data/lib/VirtualMachineDriver.rb +0 -404
- data/lib/datacenter.rb +0 -1319
- data/lib/datastore.rb +0 -1049
- data/lib/distributed_firewall.rb +0 -293
- data/lib/file_helper.rb +0 -374
- data/lib/host.rb +0 -1518
- data/lib/logical_port.rb +0 -50
- data/lib/logical_switch.rb +0 -77
- data/lib/memoize.rb +0 -74
- data/lib/network.rb +0 -705
- data/lib/nsx_client.rb +0 -157
- data/lib/nsx_component.rb +0 -28
- data/lib/nsx_constants.rb +0 -162
- data/lib/nsx_driver.rb +0 -91
- data/lib/nsx_error.rb +0 -77
- data/lib/nsx_rule.rb +0 -206
- data/lib/nsxt_client.rb +0 -189
- data/lib/nsxt_dfw.rb +0 -196
- data/lib/nsxt_logical_port.rb +0 -94
- data/lib/nsxt_rule.rb +0 -188
- data/lib/nsxt_tz.rb +0 -38
- data/lib/nsxv_client.rb +0 -189
- data/lib/nsxv_dfw.rb +0 -202
- data/lib/nsxv_logical_port.rb +0 -107
- data/lib/nsxv_rule.rb +0 -172
- data/lib/nsxv_tz.rb +0 -41
- data/lib/opaque_network.rb +0 -134
- data/lib/rest_client.rb +0 -191
- data/lib/scripts_common.rb +0 -176
- data/lib/transport_zone.rb +0 -43
- data/lib/vcenter_driver.rb +0 -152
- data/lib/vcenter_importer.rb +0 -626
- data/lib/vi_client.rb +0 -273
- data/lib/vi_helper.rb +0 -328
- data/lib/virtual_machine.rb +0 -3574
- data/lib/virtual_wire.rb +0 -158
- data/lib/vm_device.rb +0 -80
- data/lib/vm_disk.rb +0 -202
- data/lib/vm_folder.rb +0 -69
- data/lib/vm_helper.rb +0 -30
- data/lib/vm_monitor.rb +0 -305
- data/lib/vm_nic.rb +0 -70
- data/lib/vm_template.rb +0 -2112
- data/lib/vmm_importer.rb +0 -165
@@ -1,5 +1,5 @@
|
|
1
1
|
# -------------------------------------------------------------------------- #
|
2
|
-
# Copyright 2002-
|
2
|
+
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems #
|
3
3
|
# #
|
4
4
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
5
5
|
# not use this file except in compliance with the License. You may obtain #
|
@@ -15,12 +15,15 @@
|
|
15
15
|
#--------------------------------------------------------------------------- #
|
16
16
|
|
17
17
|
require 'uri'
|
18
|
+
require 'json'
|
18
19
|
|
20
|
+
# Overwriting hash class with new methods
|
19
21
|
class Hash
|
22
|
+
|
20
23
|
# Returns a new hash containing the contents of other_hash and the
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
+
# contents of self. If the value for entries with duplicate keys
|
25
|
+
# is a Hash, it will be merged recursively, otherwise it will be that
|
26
|
+
# of other_hash.
|
24
27
|
#
|
25
28
|
# @param [Hash] other_hash
|
26
29
|
#
|
@@ -31,348 +34,392 @@ class Hash
|
|
31
34
|
# h2 = {:a => 22, c => 4, {:b => 5}}
|
32
35
|
#
|
33
36
|
# h1.deep_merge(h2) #=> {:a => 22, c => 4, {:b => 5, :c => 7}}
|
34
|
-
def deep_merge(other_hash)
|
35
|
-
target =
|
36
|
-
|
37
|
-
other_hash.each do |
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
def deep_merge(other_hash, merge_array = true)
|
38
|
+
target = clone
|
39
|
+
|
40
|
+
other_hash.each do |key, value|
|
41
|
+
current = target[key]
|
42
|
+
|
43
|
+
target[key] =
|
44
|
+
if value.is_a?(Hash) && current.is_a?(Hash)
|
45
|
+
current.deep_merge(value)
|
46
|
+
elsif (value.is_a?(Array) && current.is_a?(Array)) && merge_array
|
47
|
+
current + value
|
48
|
+
else
|
49
|
+
value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
target
|
54
|
+
end
|
55
|
+
|
56
|
+
class << self
|
57
|
+
|
58
|
+
# Converts a hash to a raw String in the form KEY = VAL
|
59
|
+
#
|
60
|
+
# @param template [String] Hash content
|
61
|
+
#
|
62
|
+
# @return [Hash, OpenNebula::Error] String representation in the form KEY = VALUE of
|
63
|
+
# the hash, or an OpenNebula Error if the conversion fails
|
64
|
+
def to_raw(content_hash)
|
65
|
+
return '' if content_hash.nil? || content_hash.empty?
|
66
|
+
|
67
|
+
content = ''
|
68
|
+
content_hash.each do |key, value|
|
69
|
+
case value
|
70
|
+
when Hash
|
71
|
+
sub_content = to_raw(value)
|
72
|
+
|
73
|
+
content += "#{key} = [\n"
|
74
|
+
content_lines = sub_content.split("\n")
|
75
|
+
|
76
|
+
content_lines.each_with_index do |line, index|
|
77
|
+
content += line.to_s
|
78
|
+
content += ",\n" unless index == content_lines.size - 1
|
46
79
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
80
|
+
|
81
|
+
content += "\n]\n"
|
82
|
+
when Array
|
83
|
+
value.each do |element|
|
84
|
+
content += to_raw({ key.to_s => element })
|
85
|
+
end
|
86
|
+
else
|
87
|
+
content += "#{key} = \"#{value.to_s.gsub('"', '\"')}\"\n"
|
88
|
+
end
|
50
89
|
end
|
90
|
+
|
91
|
+
content
|
92
|
+
rescue StandardError => e
|
93
|
+
return OpenNebula::Error.new("Error wrapping the hash: #{e.message}")
|
51
94
|
end
|
52
95
|
|
53
|
-
|
54
|
-
|
96
|
+
end
|
97
|
+
|
55
98
|
end
|
56
99
|
|
57
100
|
module Validator
|
58
101
|
|
59
|
-
class ParseException < StandardError; end
|
60
|
-
class SchemaException < StandardError; end
|
61
|
-
|
62
|
-
class
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
# Recursively validate and modify a JSON body based on a schema.
|
81
|
-
#
|
82
|
-
# @see http://tools.ietf.org/html/draft-zyp-json-schema-03
|
83
|
-
#
|
84
|
-
# @param [Hash, Array, String, nil] body JSON represented as Ruby objects
|
85
|
-
# @param [Hash] schema that will be used to validate
|
86
|
-
# @param [String] key of the body that will be validated in this step
|
87
|
-
#
|
88
|
-
# @return [Hash, Array, String, nil] The modified body
|
89
|
-
#
|
90
|
-
# @raise [SchemaException] If the schema is not correctly defined
|
91
|
-
# @raise [ParseException] if the body does not meet the schema definition
|
92
|
-
#
|
93
|
-
# @example Validate a User
|
94
|
-
# schema = {
|
95
|
-
# :type => :object,
|
96
|
-
# :properties => {
|
97
|
-
# 'username' => {
|
98
|
-
# :type => :string
|
99
|
-
# }
|
100
|
-
# }
|
101
|
-
# }
|
102
|
-
#
|
103
|
-
# hash = {
|
104
|
-
# 'username' => 'pepe'
|
105
|
-
# }
|
106
|
-
#
|
107
|
-
# Validator.validate!(hash, schema)
|
108
|
-
# #=> {'username' => 'pepe'}
|
109
|
-
#
|
110
|
-
# @note The parameter body will be modified
|
111
|
-
# @note Schema options supported
|
112
|
-
# :extends
|
113
|
-
# :type => [:object, :array, :string, :null]
|
114
|
-
#
|
115
|
-
def validate!(body, schema, key="")
|
116
|
-
if schema[:extends]
|
117
|
-
base_schema = schema.delete(:extends)
|
118
|
-
schema = base_schema.deep_merge(schema)
|
102
|
+
class ParseException < StandardError; end
|
103
|
+
class SchemaException < StandardError; end
|
104
|
+
|
105
|
+
# The Validator class is used to validate a JSON body based on a schema
|
106
|
+
# which is a Hash that describes the structure of the body.
|
107
|
+
class Validator
|
108
|
+
|
109
|
+
# @param [Hash] opts the options to validate a body
|
110
|
+
# @option opts [Boolean] :default_values Set default values if the schema
|
111
|
+
# specifies it (if true)
|
112
|
+
# @option opts [Boolean] :delete_extra_properties If the body contains properties
|
113
|
+
# not specified in the schema delete them from the body (if true)
|
114
|
+
# or raise an exception (if false)
|
115
|
+
# @option opts [Boolean] :allow_extra_properties Allow properties
|
116
|
+
# not specified in the schema
|
117
|
+
def initialize(opts = {})
|
118
|
+
@opts = {
|
119
|
+
:default_values => true,
|
120
|
+
:delete_extra_properties => false,
|
121
|
+
:allow_extra_properties => false
|
122
|
+
}.merge(opts)
|
119
123
|
end
|
120
124
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
125
|
+
# Recursively validate and modify a JSON body based on a schema.
|
126
|
+
#
|
127
|
+
# @see http://tools.ietf.org/html/draft-zyp-json-schema-03
|
128
|
+
#
|
129
|
+
# @param [Hash, Array, String, nil] body JSON represented as Ruby objects
|
130
|
+
# @param [Hash] schema that will be used to validate
|
131
|
+
# @param [String] key of the body that will be validated in this step
|
132
|
+
#
|
133
|
+
# @return [Hash, Array, String, nil] The modified body
|
134
|
+
#
|
135
|
+
# @raise [SchemaException] If the schema is not correctly defined
|
136
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
137
|
+
#
|
138
|
+
# @example Validate a User
|
139
|
+
# schema = {
|
140
|
+
# :type => :object,
|
141
|
+
# :properties => {
|
142
|
+
# 'username' => {
|
143
|
+
# :type => :string
|
144
|
+
# }
|
145
|
+
# }
|
146
|
+
# }
|
147
|
+
#
|
148
|
+
# hash = {
|
149
|
+
# 'username' => 'pepe'
|
150
|
+
# }
|
151
|
+
#
|
152
|
+
# Validator.validate!(hash, schema)
|
153
|
+
# #=> {'username' => 'pepe'}
|
154
|
+
#
|
155
|
+
# @note The parameter body will be modified
|
156
|
+
# @note Schema options supported
|
157
|
+
# :extends
|
158
|
+
# :type => [:object, :array, :string, :null]
|
159
|
+
#
|
160
|
+
def validate!(body, schema, key = '')
|
161
|
+
if schema[:extends]
|
162
|
+
base_schema = schema.delete(:extends)
|
163
|
+
schema = base_schema.deep_merge(schema)
|
164
|
+
end
|
165
|
+
|
166
|
+
case schema[:type]
|
167
|
+
when :object then validate_object(body, schema, key)
|
168
|
+
when :array then validate_array(body, schema, key)
|
169
|
+
when :string then validate_string(body, schema, key)
|
170
|
+
when :integer then validate_integer(body, schema, key)
|
171
|
+
when :null then validate_null(body, schema, key)
|
172
|
+
when :boolean then validate_boolean(body, schema, key)
|
173
|
+
else raise SchemaException, "Unsupported type #{schema[:type]}"
|
174
|
+
end
|
129
175
|
end
|
130
|
-
end
|
131
176
|
|
132
|
-
|
177
|
+
private
|
178
|
+
|
179
|
+
# Validate an object type
|
180
|
+
#
|
181
|
+
# @param [Hash] body to be validated
|
182
|
+
# @param [Hash] schema_object of the objectto validate the body
|
183
|
+
# @param [String] key of the body that will be validated in this step
|
184
|
+
#
|
185
|
+
# @return [Hash] The modified body
|
186
|
+
#
|
187
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
188
|
+
#
|
189
|
+
# @example Validate with default values
|
190
|
+
# schema_body = {
|
191
|
+
# :type => :object,
|
192
|
+
# :properties => {
|
193
|
+
# 'username' => {
|
194
|
+
# :type => :string,
|
195
|
+
# :default => 'def'
|
196
|
+
# }
|
197
|
+
# }
|
198
|
+
#
|
199
|
+
# body = {}
|
200
|
+
#
|
201
|
+
# Validator.validate_object(body, schema_body)
|
202
|
+
# #=> {'username' => 'def'}
|
203
|
+
#
|
204
|
+
# @note The parameter body will be modified
|
205
|
+
# @note Schema options supported
|
206
|
+
# :properties
|
207
|
+
# :required
|
208
|
+
# :default
|
209
|
+
#
|
210
|
+
def validate_object(body, schema_object, key)
|
211
|
+
unless body.is_a?(Hash)
|
212
|
+
raise ParseException, "KEY: '#{key}' must be an Object;"
|
213
|
+
end
|
133
214
|
|
134
|
-
|
135
|
-
#
|
136
|
-
# @param [Hash] body to be validated
|
137
|
-
# @param [Hash] schema_object of the objectto validate the body
|
138
|
-
# @param [String] key of the body that will be validated in this step
|
139
|
-
#
|
140
|
-
# @return [Hash] The modified body
|
141
|
-
#
|
142
|
-
# @raise [ParseException] if the body does not meet the schema definition
|
143
|
-
#
|
144
|
-
# @example Validate with default values
|
145
|
-
# schema_body = {
|
146
|
-
# :type => :object,
|
147
|
-
# :properties => {
|
148
|
-
# 'username' => {
|
149
|
-
# :type => :string,
|
150
|
-
# :default => 'def'
|
151
|
-
# }
|
152
|
-
# }
|
153
|
-
#
|
154
|
-
# body = {}
|
155
|
-
#
|
156
|
-
# Validator.validate_object(body, schema_body)
|
157
|
-
# #=> {'username' => 'def'}
|
158
|
-
#
|
159
|
-
# @note The parameter body will be modified
|
160
|
-
# @note Schema options supported
|
161
|
-
# :properties
|
162
|
-
# :required
|
163
|
-
# :default
|
164
|
-
#
|
165
|
-
def validate_object(body, schema_object, key)
|
166
|
-
unless body.is_a?(Hash)
|
167
|
-
raise ParseException, "KEY: #{key} must be a Hash; SCHEMA:"
|
168
|
-
end
|
215
|
+
return body if schema_object[:properties].empty?
|
169
216
|
|
170
|
-
|
217
|
+
new_body = body.dup
|
171
218
|
|
172
|
-
|
173
|
-
|
219
|
+
schema_object[:properties].each do |schema_key, schema_value|
|
220
|
+
body_value = new_body.delete(schema_key)
|
174
221
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
222
|
+
if body_value
|
223
|
+
body[schema_key] = validate!(body_value, schema_value, schema_key)
|
224
|
+
else
|
225
|
+
if schema_value[:required]
|
226
|
+
raise ParseException,
|
227
|
+
"Mandatory document key '#{schema_key} (#{schema_value[:type]})' is missing from #{JSON.dump(body)}"
|
228
|
+
end
|
182
229
|
|
183
|
-
|
184
|
-
|
230
|
+
if @opts[:default_values] && schema_value[:default]
|
231
|
+
body[schema_key] = schema_value[:default]
|
232
|
+
end
|
185
233
|
end
|
186
234
|
end
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
new_body.keys.each{ |key|
|
193
|
-
body.delete(key)
|
194
|
-
}
|
195
|
-
else
|
196
|
-
if @opts[:allow_extra_properties]
|
197
|
-
return body
|
235
|
+
|
236
|
+
# raise error if body.keys is not empty
|
237
|
+
unless new_body.keys.empty?
|
238
|
+
if @opts[:delete_extra_properties]
|
239
|
+
new_body.keys.each {|key| body.delete(key) }
|
198
240
|
else
|
199
|
-
|
200
|
-
|
241
|
+
return body if @opts[:allow_extra_properties]
|
242
|
+
|
243
|
+
raise ParseException, "The following keys are not allowed: #{new_body.keys.join(', ')};"
|
201
244
|
end
|
202
245
|
end
|
246
|
+
|
247
|
+
body
|
203
248
|
end
|
204
249
|
|
205
|
-
|
206
|
-
|
250
|
+
# Validate an array type
|
251
|
+
#
|
252
|
+
# @param [Array] body to be validated
|
253
|
+
# @param [Hash] schema_array of the object to validate the body
|
254
|
+
# @param [String] schema_key of the body that will be validated in this step
|
255
|
+
#
|
256
|
+
# @return [Hash] The modified body
|
257
|
+
#
|
258
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
259
|
+
#
|
260
|
+
# @example Validate array
|
261
|
+
# schema = {
|
262
|
+
# :type => :array,
|
263
|
+
# :items => {
|
264
|
+
# :type => :string
|
265
|
+
# }
|
266
|
+
# }
|
267
|
+
#
|
268
|
+
# body = ['pepe', 'luis', 'juan']
|
269
|
+
#
|
270
|
+
# Validator.validate_array(body, schema)
|
271
|
+
# #=> 'username' => ['pepe', 'luis', 'juan']
|
272
|
+
#
|
273
|
+
# @note The parameter body will be modified
|
274
|
+
# @note Schema options supported
|
275
|
+
# :items
|
276
|
+
#
|
277
|
+
def validate_array(body, schema_array, schema_key)
|
278
|
+
unless body.instance_of?(Array)
|
279
|
+
raise ParseException, "KEY: '#{schema_key}' must be an Array;"
|
280
|
+
end
|
207
281
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
# @param [Hash] schema_array of the object to validate the body
|
212
|
-
# @param [String] schema_key of the body that will be validated in this step
|
213
|
-
#
|
214
|
-
# @return [Hash] The modified body
|
215
|
-
#
|
216
|
-
# @raise [ParseException] if the body does not meet the schema definition
|
217
|
-
#
|
218
|
-
# @example Validate array
|
219
|
-
# schema = {
|
220
|
-
# :type => :array,
|
221
|
-
# :items => {
|
222
|
-
# :type => :string
|
223
|
-
# }
|
224
|
-
# }
|
225
|
-
#
|
226
|
-
# body = ['pepe', 'luis', 'juan']
|
227
|
-
#
|
228
|
-
# Validator.validate_array(body, schema)
|
229
|
-
# #=> 'username' => ['pepe', 'luis', 'juan']
|
230
|
-
#
|
231
|
-
# @note The parameter body will be modified
|
232
|
-
# @note Schema options supported
|
233
|
-
# :items
|
234
|
-
#
|
235
|
-
def validate_array(body, schema_array, schema_key)
|
236
|
-
if body.instance_of?(Array)
|
237
|
-
body.collect { |body_item|
|
282
|
+
return body if schema_array[:items].empty?
|
283
|
+
|
284
|
+
body.collect do |body_item|
|
238
285
|
validate!(body_item, schema_array[:items], schema_key)
|
239
|
-
|
240
|
-
else
|
241
|
-
raise ParseException, "KEY: '#{schema_key}' must be an Array;"
|
286
|
+
end
|
242
287
|
end
|
243
|
-
end
|
244
288
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
289
|
+
# Validate an integer type
|
290
|
+
#
|
291
|
+
# @param [Array] body to be validated
|
292
|
+
# @param [Hash] schema_array of the object to validate the body
|
293
|
+
# @param [String] schema_key of the body that will be validated in this step
|
294
|
+
#
|
295
|
+
# @return [Hash] The modified body
|
296
|
+
#
|
297
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
298
|
+
#
|
299
|
+
# @example Validate array
|
300
|
+
# schema = {
|
301
|
+
# :type => :integer
|
302
|
+
# }
|
303
|
+
#
|
304
|
+
# body = 5
|
305
|
+
#
|
306
|
+
# Validator.validate_integer(body, schema)
|
307
|
+
# #=> 5
|
308
|
+
#
|
309
|
+
#
|
310
|
+
def validate_integer(body, schema_array, schema_key)
|
311
|
+
value = Integer(body)
|
312
|
+
|
313
|
+
if schema_array[:maximum]
|
314
|
+
excl = schema_array[:exclusiveMaximum]
|
315
|
+
max = schema_array[:maximum]
|
316
|
+
if !(excl ? value < max : value <= max)
|
317
|
+
raise ParseException, "KEY: '#{schema_key}' must be "\
|
318
|
+
"lower than #{excl ? '' : 'or equal to'} #{max};"
|
319
|
+
end
|
275
320
|
end
|
276
|
-
end
|
277
321
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
322
|
+
if schema_array[:minimum]
|
323
|
+
excl = schema_array[:exclusiveMinimum]
|
324
|
+
min = schema_array[:minimum]
|
325
|
+
if !(excl ? value > min : value >= min)
|
326
|
+
raise ParseException, "KEY: '#{schema_key}' must be "\
|
327
|
+
"greater than #{excl ? '' : 'or equal to'} #{min};"
|
328
|
+
end
|
284
329
|
end
|
285
|
-
end
|
286
330
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
331
|
+
value
|
332
|
+
rescue ArgumentError
|
333
|
+
raise ParseException, "KEY: '#{schema_key}' must be an Integer;"
|
334
|
+
end
|
291
335
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
336
|
+
# Validate an null type
|
337
|
+
#
|
338
|
+
# @param [nil] body to be validated
|
339
|
+
# @param [Hash] schema_null of the object to validate the body
|
340
|
+
# @param [String] schema_key of the body that will be validated in this step
|
341
|
+
#
|
342
|
+
# @return [nil]
|
343
|
+
#
|
344
|
+
# @raise [ParseException] if the body is not nil
|
345
|
+
#
|
346
|
+
# @example Validate array
|
347
|
+
# schema = {
|
348
|
+
# :type => :null
|
349
|
+
# }
|
350
|
+
#
|
351
|
+
# body = nil
|
352
|
+
#
|
353
|
+
# Validator.validate_null(body, schema)
|
354
|
+
# #=> nil
|
355
|
+
#
|
356
|
+
#
|
357
|
+
def validate_null(body, schema_null, schema_key)
|
358
|
+
return if body.nil?
|
359
|
+
|
360
|
+
raise ParseException, "KEY: '#{schema_key}' is not allowed in #{schema_null};"
|
316
361
|
end
|
317
|
-
end
|
318
362
|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
363
|
+
# Validate an boolean type
|
364
|
+
#
|
365
|
+
# @param [Object] body to be validated
|
366
|
+
# @param [Hash] schema_boolean of the object to validate the body
|
367
|
+
# @param [String] schema_key of the body that will be validated in this step
|
368
|
+
#
|
369
|
+
# @return [nil]
|
370
|
+
#
|
371
|
+
# @raise [ParseException] if the body is not a boolean
|
372
|
+
#
|
373
|
+
# @example Validate array
|
374
|
+
# schema = {
|
375
|
+
# :type => :boolean
|
376
|
+
# }
|
377
|
+
#
|
378
|
+
# body = true
|
379
|
+
#
|
380
|
+
# Validator.validate_boolean(body, schema)
|
381
|
+
# #=> nil
|
382
|
+
#
|
383
|
+
#
|
384
|
+
def validate_boolean(body, schema_boolean, schema_key)
|
385
|
+
if body != true && body != false
|
386
|
+
raise ParseException, "KEY: '#{schema_key}' is not allowed in #{schema_boolean};"
|
387
|
+
end
|
388
|
+
|
389
|
+
body
|
343
390
|
end
|
344
391
|
|
345
|
-
|
346
|
-
|
392
|
+
# Validate an string type
|
393
|
+
#
|
394
|
+
# @param [String] body to be validated
|
395
|
+
# @param [Hash] schema_string of the object to validate the body
|
396
|
+
# @param [String] schema_key of the body that will be validated in this step
|
397
|
+
#
|
398
|
+
# @return [String] The modified body
|
399
|
+
#
|
400
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
401
|
+
#
|
402
|
+
# @example Validate array
|
403
|
+
# schema = {
|
404
|
+
# :type => :string
|
405
|
+
# }
|
406
|
+
#
|
407
|
+
# body = "pepe"
|
408
|
+
#
|
409
|
+
# Validator.validate_string(body, schema)
|
410
|
+
# #=> "pepe"
|
411
|
+
#
|
412
|
+
# @note The parameter body will be modified
|
413
|
+
# @note Schema options supported
|
414
|
+
# :format
|
415
|
+
# :enum
|
416
|
+
# :regex
|
417
|
+
#
|
418
|
+
def validate_string(body, schema_string, schema_key)
|
419
|
+
unless body.instance_of?(String)
|
420
|
+
raise ParseException, "KEY: '#{schema_key}' must be a String;"
|
421
|
+
end
|
347
422
|
|
348
|
-
# Validate an string type
|
349
|
-
#
|
350
|
-
# @param [String] body to be validated
|
351
|
-
# @param [Hash] schema_string of the object to validate the body
|
352
|
-
# @param [String] schema_key of the body that will be validated in this step
|
353
|
-
#
|
354
|
-
# @return [String] The modified body
|
355
|
-
#
|
356
|
-
# @raise [ParseException] if the body does not meet the schema definition
|
357
|
-
#
|
358
|
-
# @example Validate array
|
359
|
-
# schema = {
|
360
|
-
# :type => :string
|
361
|
-
# }
|
362
|
-
#
|
363
|
-
# body = "pepe"
|
364
|
-
#
|
365
|
-
# Validator.validate_string(body, schema)
|
366
|
-
# #=> "pepe"
|
367
|
-
#
|
368
|
-
# @note The parameter body will be modified
|
369
|
-
# @note Schema options supported
|
370
|
-
# :format
|
371
|
-
# :enum
|
372
|
-
# :regex
|
373
|
-
#
|
374
|
-
def validate_string(body, schema_string, schema_key)
|
375
|
-
if body.instance_of?(String)
|
376
423
|
if schema_string[:format]
|
377
424
|
check_format(body, schema_string, schema_key)
|
378
425
|
elsif schema_string[:enum]
|
@@ -382,118 +429,119 @@ class Validator
|
|
382
429
|
else
|
383
430
|
body
|
384
431
|
end
|
385
|
-
else
|
386
|
-
raise ParseException, "KEY: '#{schema_key}' must be a String;"
|
387
432
|
end
|
388
|
-
end
|
389
433
|
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
434
|
+
# Validate an string format
|
435
|
+
#
|
436
|
+
# @param [String] body_value to be validated
|
437
|
+
# @param [Hash] schema_string of the object to validate the body
|
438
|
+
# @param [String] schema_key of the body that will be validated in this step
|
439
|
+
#
|
440
|
+
# @return [String] The modified body
|
441
|
+
#
|
442
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
443
|
+
#
|
444
|
+
# @example Validate array
|
445
|
+
# schema = {
|
446
|
+
# :type => :string,
|
447
|
+
# :format => :url
|
448
|
+
# }
|
449
|
+
#
|
450
|
+
# body = "http://localhost:4567"
|
451
|
+
#
|
452
|
+
# Validator.check_format(body, schema)
|
453
|
+
# #=> "http://localhost:4567"
|
454
|
+
#
|
455
|
+
# @note The parameter body will be modified
|
456
|
+
# @note Schema options supported
|
457
|
+
# :url
|
458
|
+
#
|
459
|
+
def check_format(body_value, schema_string, schema_key)
|
460
|
+
case schema_string[:format]
|
461
|
+
when :uri
|
462
|
+
begin
|
463
|
+
require 'uri'
|
464
|
+
URI.parse(body_value)
|
465
|
+
rescue URI::InvalidURIError
|
466
|
+
raise ParseException, "KEY: '#{schema_key}' must be a valid URL;"
|
467
|
+
end
|
468
|
+
|
469
|
+
body_value
|
423
470
|
end
|
424
471
|
|
425
472
|
body_value
|
426
473
|
end
|
427
474
|
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
raise ParseException, "KEY: '#{schema_key}' must be one of"\
|
461
|
-
" #{schema_string[:enum].join(', ')};"
|
475
|
+
# Validate an string enum
|
476
|
+
#
|
477
|
+
# @param [String] body_value to be validated
|
478
|
+
# @param [Hash] schema_string of the object to validate the body
|
479
|
+
# @param [String] schema_key of the body that will be validated in this step
|
480
|
+
#
|
481
|
+
# @return [String] The modified body
|
482
|
+
#
|
483
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
484
|
+
#
|
485
|
+
# @example Validate array
|
486
|
+
# schema = {
|
487
|
+
# :type => :string,
|
488
|
+
# :enum => ['juan', 'luis']
|
489
|
+
# }
|
490
|
+
#
|
491
|
+
# body = "juan"
|
492
|
+
#
|
493
|
+
# Validator.check_enum(body, schema)
|
494
|
+
# #=> "juan"
|
495
|
+
#
|
496
|
+
# @note The parameter body will be modified
|
497
|
+
# @note Schema options supported
|
498
|
+
# :enum
|
499
|
+
#
|
500
|
+
def check_enum(body_value, schema_string, schema_key)
|
501
|
+
if schema_string[:enum].include?(body_value)
|
502
|
+
body_value
|
503
|
+
else
|
504
|
+
raise ParseException, "KEY: '#{schema_key}' must be one of"\
|
505
|
+
" #{schema_string[:enum].join(', ')};"
|
506
|
+
end
|
462
507
|
end
|
463
|
-
end
|
464
508
|
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
509
|
+
# Validate an string regex
|
510
|
+
#
|
511
|
+
# @param [String] body_value to be validated
|
512
|
+
# @param [Hash] schema_string of the object to validate the body
|
513
|
+
# @param [String] schema_key of the body that will be validated in this step
|
514
|
+
#
|
515
|
+
# @return [String] The modified body
|
516
|
+
#
|
517
|
+
# @raise [ParseException] if the body does not meet the schema definition
|
518
|
+
#
|
519
|
+
# @example Validate array
|
520
|
+
# schema = {
|
521
|
+
# :type => :string,
|
522
|
+
# :regex => /^\w+$/
|
523
|
+
# }
|
524
|
+
#
|
525
|
+
# body = "juan"
|
526
|
+
#
|
527
|
+
# Validator.check_regex(body, schema)
|
528
|
+
# #=> "juan"
|
529
|
+
#
|
530
|
+
# @note The parameter body will be modified
|
531
|
+
# @note Schema options supported
|
532
|
+
# :enum
|
533
|
+
#
|
534
|
+
def check_regex(body_value, schema_string, schema_key)
|
535
|
+
if schema_string[:regex] =~ body_value
|
536
|
+
body_value
|
537
|
+
else
|
538
|
+
raise(
|
539
|
+
ParseException,
|
540
|
+
"KEY: '#{schema_key}' must match regexp #{schema_string[:regex].inspect};"
|
541
|
+
)
|
542
|
+
end
|
495
543
|
end
|
544
|
+
|
496
545
|
end
|
497
|
-
end
|
498
546
|
|
499
547
|
end
|