apipie-params 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README.md +23 -0
- data/Rakefile +7 -0
- data/apipie-params.gemspec +23 -0
- data/lib/apipie-params.rb +6 -0
- data/lib/apipie/params.rb +7 -0
- data/lib/apipie/params/description.rb +56 -0
- data/lib/apipie/params/descriptor.rb +343 -0
- data/lib/apipie/params/dsl.rb +39 -0
- data/lib/apipie/params/errors.rb +35 -0
- data/lib/apipie/params/version.rb +5 -0
- data/test/description_test.rb +58 -0
- data/test/descriptor_test.rb +120 -0
- data/test/test_helper.rb +13 -0
- metadata +129 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 Pavel Pokorný
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Apipie Params
|
2
|
+
=============
|
3
|
+
|
4
|
+
DSL for Hash/JSON descriptions. Describe format of hashes using Ruby
|
5
|
+
code, generate a json-schema for it and validate values against it.
|
6
|
+
|
7
|
+
It's based on the DSL of apipie-rails. See
|
8
|
+
[its documentation](https://github.com/Pajk/apipie-rails#parameter-description)
|
9
|
+
for more details. It basically lets you use the param descriptions
|
10
|
+
outside of Rails controllers.
|
11
|
+
|
12
|
+
In this phase, the validation itself is an experimental feature. You
|
13
|
+
need to install `json-schema` gem explicitly.
|
14
|
+
|
15
|
+
License
|
16
|
+
-------
|
17
|
+
|
18
|
+
MIT
|
19
|
+
|
20
|
+
Author
|
21
|
+
------
|
22
|
+
|
23
|
+
Ivan Nečas
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "apipie/params/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "apipie-params"
|
7
|
+
s.version = Apipie::Params::VERSION
|
8
|
+
s.authors = ["Ivan Necas"]
|
9
|
+
s.email = ["inecas@redhat.com"]
|
10
|
+
s.homepage = "http://github.com/iNecas/apipie-params"
|
11
|
+
s.summary = "DSL for describing data structures"
|
12
|
+
s.description = "Allows defining structure of data and " +
|
13
|
+
"perform validation against it using json-schema"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency "activesupport"
|
20
|
+
s.add_dependency "rake"
|
21
|
+
s.add_development_dependency "json-schema"
|
22
|
+
s.add_development_dependency "minitest"
|
23
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Apipie
|
4
|
+
module Params
|
5
|
+
class Description
|
6
|
+
|
7
|
+
attr_accessor :name, :desc, :allow_nil, :required, :descriptor, :options
|
8
|
+
|
9
|
+
def self.define(&block)
|
10
|
+
param_description = Description.new(nil, nil, {})
|
11
|
+
param_description.descriptor = Descriptor::Hash.new(block, {})
|
12
|
+
return param_description
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(name, descriptor_arg, options = {}, &block)
|
16
|
+
@options = options
|
17
|
+
@name = name
|
18
|
+
@desc = @options[:desc]
|
19
|
+
# if required is specified, set to boolean of the value, nil
|
20
|
+
# otherwise: nil allows us specify the default value later.
|
21
|
+
@required = @options.has_key?(:required) ? !!@options[:required] : nil
|
22
|
+
@allow_nil = @options.has_key?(:allow_nil) ? !!@options[:allow_nil] : nil
|
23
|
+
|
24
|
+
unless descriptor_arg.nil?
|
25
|
+
@descriptor = Params::Descriptor::Base.find(descriptor_arg,
|
26
|
+
options,
|
27
|
+
block)
|
28
|
+
else
|
29
|
+
@descriptor = nil
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate!(value)
|
35
|
+
descriptor.validate!(self, value)
|
36
|
+
end
|
37
|
+
|
38
|
+
def respond_to?(method)
|
39
|
+
case method.to_s
|
40
|
+
when 'params', 'param'
|
41
|
+
@descriptor.respond_to?(method)
|
42
|
+
else
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(method, *args, &block)
|
48
|
+
if respond_to?(method)
|
49
|
+
@descriptor.send(method, *args, &block)
|
50
|
+
else
|
51
|
+
super
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,343 @@
|
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module Apipie
|
5
|
+
module Params
|
6
|
+
module Descriptor
|
7
|
+
|
8
|
+
class Base
|
9
|
+
def initialize(options)
|
10
|
+
if options.is_a? ::Hash
|
11
|
+
@options = options
|
12
|
+
else
|
13
|
+
@options = {}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.inherited(subclass)
|
18
|
+
@descriptor_classes ||= []
|
19
|
+
@descriptor_classes.insert 0, subclass
|
20
|
+
end
|
21
|
+
|
22
|
+
# find the right descriptor for given options
|
23
|
+
def self.find(argument, options, block)
|
24
|
+
if argument.is_a? Descriptor::Base
|
25
|
+
return argument
|
26
|
+
end
|
27
|
+
|
28
|
+
# in case param description (or something else quacking the
|
29
|
+
# same) is passed
|
30
|
+
if argument.respond_to?(:descriptor) &&
|
31
|
+
argument.descriptor.is_a?(Descriptor::Base)
|
32
|
+
return argument.descriptor
|
33
|
+
end
|
34
|
+
|
35
|
+
@descriptor_classes.each do |descriptor_class|
|
36
|
+
descriptor = descriptor_class.build(argument, options, block)
|
37
|
+
return descriptor if descriptor
|
38
|
+
end
|
39
|
+
|
40
|
+
return nil
|
41
|
+
end
|
42
|
+
|
43
|
+
# to be used in the error description and json representation
|
44
|
+
def description
|
45
|
+
""
|
46
|
+
end
|
47
|
+
|
48
|
+
def invalid_param_error(param_description, error_value, errors = [])
|
49
|
+
Params::Errors::Invalid.new(param_description, error_value, description)
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_json
|
53
|
+
self.json_schema
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
class JsonSchema < Base
|
59
|
+
def self.inherited(subclass)
|
60
|
+
Base.inherited(subclass)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.build(*args)
|
64
|
+
# this is an abstract class
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
68
|
+
def json_schema
|
69
|
+
{'description' => description}
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def validate!(param_description, value)
|
74
|
+
encapsulated_value = {'root' => value}
|
75
|
+
encapsulated_schema = {
|
76
|
+
'type' => 'object',
|
77
|
+
'properties' => {'root' => json_schema}
|
78
|
+
}
|
79
|
+
errors = JSON::Validator.fully_validate(encapsulated_schema,
|
80
|
+
encapsulated_value.with_indifferent_access,
|
81
|
+
:errors_as_objects => true)
|
82
|
+
|
83
|
+
if errors.any?
|
84
|
+
raise invalid_param_error(param_description, value, errors)
|
85
|
+
else
|
86
|
+
return true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
# validate arguments type
|
93
|
+
class String < JsonSchema
|
94
|
+
|
95
|
+
def self.build(type, options, block)
|
96
|
+
self.new(options) if type == ::String
|
97
|
+
end
|
98
|
+
|
99
|
+
def description
|
100
|
+
"Must be a string"
|
101
|
+
end
|
102
|
+
|
103
|
+
def json_schema
|
104
|
+
super.merge('type' => 'string')
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
# validate arguments type
|
110
|
+
class Integer < JsonSchema
|
111
|
+
|
112
|
+
def self.build(type, options, block)
|
113
|
+
self.new(options) if type == ::Integer
|
114
|
+
end
|
115
|
+
|
116
|
+
def description
|
117
|
+
"Must be an integer"
|
118
|
+
end
|
119
|
+
|
120
|
+
def json_schema
|
121
|
+
super.merge('type' => 'integer')
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
# validate arguments value with regular expression
|
127
|
+
class Regexp < JsonSchema
|
128
|
+
|
129
|
+
def self.build(regexp, options, block)
|
130
|
+
self.new(regexp, options) if regexp.is_a? ::Regexp
|
131
|
+
end
|
132
|
+
|
133
|
+
def initialize(regexp, options)
|
134
|
+
super(options)
|
135
|
+
@regexp = regexp
|
136
|
+
end
|
137
|
+
|
138
|
+
def description
|
139
|
+
"Must match regular expression /#{@regexp.source}/."
|
140
|
+
end
|
141
|
+
|
142
|
+
def json_schema
|
143
|
+
super.merge('type' => 'string', 'pattern' => @regexp.source)
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
# arguments value must be one of given in array
|
149
|
+
class Enum < JsonSchema
|
150
|
+
|
151
|
+
def self.build(enum, options, block)
|
152
|
+
if enum.is_a?(::Array) && block.nil?
|
153
|
+
self.new(enum, options)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def initialize(enum, options)
|
158
|
+
super(options)
|
159
|
+
@enum = enum
|
160
|
+
end
|
161
|
+
|
162
|
+
def description
|
163
|
+
"Must be one of: #{@enum.join(', ')}."
|
164
|
+
end
|
165
|
+
|
166
|
+
def json_schema
|
167
|
+
super.merge('type' => 'any', 'enum' => @enum)
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
class Hash < JsonSchema
|
173
|
+
|
174
|
+
class DSL
|
175
|
+
include Params::DSL
|
176
|
+
|
177
|
+
def initialize(&block)
|
178
|
+
instance_eval(&block)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def self.build(argument, options, block)
|
183
|
+
if block.is_a?(::Proc) && block.arity <= 0 && argument == ::Hash
|
184
|
+
self.new(block, options)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def initialize(block, options)
|
189
|
+
super(options)
|
190
|
+
@dsl_data = DSL.new(&block)._apipie_params_dsl_data
|
191
|
+
end
|
192
|
+
|
193
|
+
def params
|
194
|
+
@params ||= @dsl_data.map do |name, arg, options, block|
|
195
|
+
Description.new(name, arg, options, &block)
|
196
|
+
end
|
197
|
+
return @params
|
198
|
+
end
|
199
|
+
|
200
|
+
def param(param_name)
|
201
|
+
params.find { |param| param.name.to_s == param_name.to_s }
|
202
|
+
end
|
203
|
+
|
204
|
+
def description
|
205
|
+
"Must be a Hash"
|
206
|
+
end
|
207
|
+
|
208
|
+
def json_schema
|
209
|
+
properties = params.reduce({}) do |hash, description|
|
210
|
+
hash.update(description.name.to_s => description.descriptor.json_schema)
|
211
|
+
end
|
212
|
+
super.merge('type' => 'object',
|
213
|
+
'properties' => properties)
|
214
|
+
end
|
215
|
+
|
216
|
+
def invalid_param_error(param_description, error_value, errors)
|
217
|
+
descriptions = errors.map do |error|
|
218
|
+
fragment_descriptor(param_description, error[:fragment])
|
219
|
+
end
|
220
|
+
# TODO: handle multiple errors at the same time
|
221
|
+
invalid_param = descriptions.first
|
222
|
+
description = invalid_param.descriptor.description
|
223
|
+
Params::Errors::Invalid.new(invalid_param, error_value, description)
|
224
|
+
end
|
225
|
+
|
226
|
+
def fragment_descriptor(param_description, fragment)
|
227
|
+
keys_path = fragment.sub(/\A#\/root\//,'').split('/')
|
228
|
+
keys_path.delete_if { |a| a =~ /\A\d+\Z/ }
|
229
|
+
keys_path.reduce(param_description) do |description, key|
|
230
|
+
description.param(key)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
class Array < JsonSchema
|
237
|
+
|
238
|
+
extend Forwardable
|
239
|
+
|
240
|
+
def_delegators :@descriptor, :invalid_param_error
|
241
|
+
|
242
|
+
def self.build(argument, options, block)
|
243
|
+
if argument == ::Array && block.is_a?(::Proc)
|
244
|
+
self.new(block, options)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def initialize(descriptor_or_block, options)
|
249
|
+
super(options)
|
250
|
+
case descriptor_or_block
|
251
|
+
when ::Proc
|
252
|
+
@descriptor = Hash.new(descriptor_or_block, options)
|
253
|
+
when Descriptor::Base
|
254
|
+
@descriptor = descriptor_or_block
|
255
|
+
else
|
256
|
+
raise ArgumentError, "Proc or Descriptor::Base expected, got #{descriptor_or_block.class.name}"
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
def description
|
261
|
+
"Must be an Array"
|
262
|
+
end
|
263
|
+
|
264
|
+
def json_schema
|
265
|
+
super.merge(
|
266
|
+
'type' => 'array',
|
267
|
+
'items' => @descriptor.json_schema
|
268
|
+
)
|
269
|
+
end
|
270
|
+
|
271
|
+
# delegate to params and param only if @descriptor supports those
|
272
|
+
def respond_to?(method)
|
273
|
+
case method.to_s
|
274
|
+
when 'params', 'param'
|
275
|
+
@descriptor.respond_to?(method)
|
276
|
+
else
|
277
|
+
super
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
def method_missing(method, *args, &block)
|
282
|
+
if respond_to?(method)
|
283
|
+
@descriptor.send(method, *args, &block)
|
284
|
+
else
|
285
|
+
super
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
# special type of descriptor: we say that it's not specified
|
292
|
+
class Undef < JsonSchema
|
293
|
+
|
294
|
+
def self.build(argument, options, block)
|
295
|
+
if argument == :undef
|
296
|
+
self.new(options)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
def json_schema
|
301
|
+
super.merge('type' => 'any')
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
class Number < Regexp
|
306
|
+
|
307
|
+
def self.build(argument, options, block)
|
308
|
+
if argument == :number
|
309
|
+
self.new(self.pattern, options)
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def description
|
314
|
+
"Must be a number."
|
315
|
+
end
|
316
|
+
|
317
|
+
def self.pattern
|
318
|
+
/\A(0|[1-9]\d*)\Z$/
|
319
|
+
end
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
class Boolean < Enum
|
324
|
+
|
325
|
+
def self.build(argument, options, block)
|
326
|
+
if argument == :bool
|
327
|
+
self.new(valid_values, options)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
def self.valid_values
|
332
|
+
%w[true false]
|
333
|
+
end
|
334
|
+
|
335
|
+
def description
|
336
|
+
"Must be 'true' or 'false'"
|
337
|
+
end
|
338
|
+
|
339
|
+
end
|
340
|
+
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Apipie
|
2
|
+
module Params
|
3
|
+
module DSL
|
4
|
+
def _apipie_params_dsl_data
|
5
|
+
@_apipie_params_dsl_data ||= _apipie_params_dsl_data_init
|
6
|
+
end
|
7
|
+
|
8
|
+
def _apipie_params_dsl_data_init
|
9
|
+
@_apipie_params_dsl_data = []
|
10
|
+
end
|
11
|
+
|
12
|
+
# Describe method's parameter
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
# param :greeting, String, :desc => "arbitrary text", :required => true
|
16
|
+
#
|
17
|
+
def param(param_name, descriptor_arg, desc_or_options = nil, options = {}, &block) #:doc:
|
18
|
+
if desc_or_options.is_a? String
|
19
|
+
options = options.merge(:desc => desc_or_options)
|
20
|
+
end
|
21
|
+
_apipie_params_dsl_data << [param_name,
|
22
|
+
descriptor_arg,
|
23
|
+
options,
|
24
|
+
block]
|
25
|
+
end
|
26
|
+
|
27
|
+
# +descriptor+ might be instance of Descriptor::Base or
|
28
|
+
# something that has it in :descriptor method
|
29
|
+
def array_of(descriptor)
|
30
|
+
descriptor = if descriptor.is_a? Descriptor::Base
|
31
|
+
elsif descriptor.respond_to?(:descriptor)
|
32
|
+
descriptor.descriptor
|
33
|
+
end
|
34
|
+
Descriptor::Array.new(descriptor, {})
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Apipie
|
2
|
+
module Params
|
3
|
+
module Errors
|
4
|
+
|
5
|
+
class ParamError < StandardError
|
6
|
+
attr_accessor :description
|
7
|
+
|
8
|
+
def initialize(description)
|
9
|
+
@description = description
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Missing < ParamError
|
14
|
+
def to_s
|
15
|
+
"Missing parameter #{@description.name}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Invalid < ParamError
|
20
|
+
attr_accessor :value, :error
|
21
|
+
|
22
|
+
def initialize(description, value, error)
|
23
|
+
super(description)
|
24
|
+
@value = value
|
25
|
+
@error = error
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
"Invalid parameter '#{description.name}' value #{@value.inspect}: #{@error}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Apipie
|
4
|
+
module Params
|
5
|
+
class DescriptionTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_define
|
8
|
+
person_description = Params.define do
|
9
|
+
param :name, String
|
10
|
+
param :age, :number, 'in years'
|
11
|
+
param :address, Hash do
|
12
|
+
param :street, String
|
13
|
+
param :zip, String
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_equal [:name, :age, :address], person_description.params.map(&:name)
|
18
|
+
assert_equal [:street, :zip], person_description.param(:address).params.map(&:name)
|
19
|
+
|
20
|
+
classroom_description = Params.define do
|
21
|
+
param :name, String
|
22
|
+
param :teacher, person_description
|
23
|
+
param :students, array_of(person_description)
|
24
|
+
end
|
25
|
+
|
26
|
+
teacher_description = classroom_description.param(:teacher)
|
27
|
+
assert_equal [:name, :age, :address], teacher_description.params.map(&:name)
|
28
|
+
students_description = classroom_description.param(:students)
|
29
|
+
assert_instance_of Descriptor::Array, students_description.descriptor
|
30
|
+
assert_equal [:name, :age, :address], students_description.params.map(&:name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_params
|
34
|
+
description = Description.new('test', nil, {})
|
35
|
+
descriptor_with_params =
|
36
|
+
Descriptor::Hash.new(Proc.new { param(:test, String) }, {})
|
37
|
+
descriptor_without_params = Descriptor::String.new({})
|
38
|
+
|
39
|
+
description.descriptor = descriptor_with_params
|
40
|
+
assert description.respond_to?(:params)
|
41
|
+
assert description.respond_to?(:param)
|
42
|
+
|
43
|
+
description.descriptor = descriptor_without_params
|
44
|
+
assert !description.respond_to?(:params)
|
45
|
+
assert !description.respond_to?(:param)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_validate
|
49
|
+
description = Description.new('test', String, {})
|
50
|
+
description.validate!('123')
|
51
|
+
assert_raise Errors::Invalid do
|
52
|
+
description.validate!(123)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Apipie
|
4
|
+
module Params
|
5
|
+
describe Descriptor do
|
6
|
+
|
7
|
+
def self.describe_validator(valid_value, invalid_value, exception_matcher)
|
8
|
+
describe 'valid value' do
|
9
|
+
let(:value) { valid_value }
|
10
|
+
|
11
|
+
it 'succeeds' do
|
12
|
+
subject.validate!(value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'invalid value' do
|
17
|
+
let(:value) { invalid_value }
|
18
|
+
|
19
|
+
it 'fails' do
|
20
|
+
exception = lambda do
|
21
|
+
subject.validate!(value)
|
22
|
+
end.must_raise Params::Errors::Invalid
|
23
|
+
exception.message.must_match(exception_matcher)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'custom descriptor' do
|
29
|
+
subject { Description.new('test', :custom, {}) }
|
30
|
+
#describe_validator('valid', 'invalid', /value has to be "value"/)
|
31
|
+
it 'allows to extend the default json schema'
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'string descriptor' do
|
35
|
+
subject { Description.new('test', String, {}) }
|
36
|
+
describe_validator('valid', :invalid, /Must be a string/)
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'integer descriptor' do
|
40
|
+
subject { Description.new('test', Integer, {}) }
|
41
|
+
describe_validator(123, '123', /Must be an integer/)
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'regexp descriptor' do
|
45
|
+
subject { Description.new('test', /\Avalid/, {}) }
|
46
|
+
describe_validator('valid', 'invalid', /Must match/)
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'enum descriptor' do
|
50
|
+
subject { Description.new('test', ['valid', 'valider'], {}) }
|
51
|
+
describe_validator('valid', 'invalid', /Must be one of/)
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'hash descriptor' do
|
55
|
+
subject do
|
56
|
+
Description.new('test', Hash, {}) do
|
57
|
+
param :name, String
|
58
|
+
param :address, Hash do
|
59
|
+
param :street, String
|
60
|
+
param :zip, String
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
# TODO: test require and allow_nil
|
65
|
+
describe_validator({:name => "valid"}, {:name => 123}, /Must be a string/)
|
66
|
+
|
67
|
+
describe '#params' do
|
68
|
+
it 'returns param descriptions of all keys' do
|
69
|
+
name_description, address_description = subject.params
|
70
|
+
name_description.name.must_equal :name
|
71
|
+
address_description.name.must_equal :address
|
72
|
+
address_description.params.map(&:name).must_equal([:street, :zip])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#param' do
|
77
|
+
it 'returns param description for a key' do
|
78
|
+
street_description = subject.param(:address).param(:street)
|
79
|
+
street_description.name.must_equal :street
|
80
|
+
street_description.descriptor.must_be_kind_of Descriptor::String
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'array descriptor' do
|
86
|
+
subject do
|
87
|
+
Description.new('test', ::Array, {}) do
|
88
|
+
param :name, String
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe_validator([{:name => "valid"}], [{:name => :invalid}], /Must be a string/)
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'undef descriptor' do
|
96
|
+
subject { Description.new('test', :undef, {}) }
|
97
|
+
|
98
|
+
describe 'valid value' do
|
99
|
+
let(:value) { 'whatever' }
|
100
|
+
|
101
|
+
it 'succeeds' do
|
102
|
+
subject.validate!(value)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
describe 'number descriptor' do
|
109
|
+
subject { Description.new('test', :number, {}) }
|
110
|
+
describe_validator('123', 'a23', /Must be a number/)
|
111
|
+
end
|
112
|
+
|
113
|
+
describe 'boolean descriptor' do
|
114
|
+
subject { Description.new('test', :bool, {}) }
|
115
|
+
describe_validator('true', 'no', /Must be 'true' or 'false'/)
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apipie-params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ivan Necas
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json-schema
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Allows defining structure of data and perform validation against it using
|
79
|
+
json-schema
|
80
|
+
email:
|
81
|
+
- inecas@redhat.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Gemfile
|
88
|
+
- MIT-LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- apipie-params.gemspec
|
92
|
+
- lib/apipie-params.rb
|
93
|
+
- lib/apipie/params.rb
|
94
|
+
- lib/apipie/params/description.rb
|
95
|
+
- lib/apipie/params/descriptor.rb
|
96
|
+
- lib/apipie/params/dsl.rb
|
97
|
+
- lib/apipie/params/errors.rb
|
98
|
+
- lib/apipie/params/version.rb
|
99
|
+
- test/description_test.rb
|
100
|
+
- test/descriptor_test.rb
|
101
|
+
- test/test_helper.rb
|
102
|
+
homepage: http://github.com/iNecas/apipie-params
|
103
|
+
licenses: []
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.25
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: DSL for describing data structures
|
126
|
+
test_files:
|
127
|
+
- test/description_test.rb
|
128
|
+
- test/descriptor_test.rb
|
129
|
+
- test/test_helper.rb
|