sinatra-validations 1.0.0 → 1.0.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 +4 -4
- data/lib/sinatra/validations.rb +12 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e6130bb489a8c9e2cc9ae99c18a0b3b12dc8b37
|
4
|
+
data.tar.gz: 21fad3aafdfd111b05db808c452b313cf9121383
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14bf5a2348ff73a40909e0007f070e359907fc974f146a38f561635c938580e0dbb7da25ba5d69b778761d38fa6f55badb8a0a768170adee89362e67ea04db0a
|
7
|
+
data.tar.gz: 1923be00285f6e8d90e161308cd722d8653b0abe0a3d452b2966100e02db29babd8a8106399e13305fdfd665d084d5c9f0f3768fbe80f75d826d6647ae59ec41
|
data/lib/sinatra/validations.rb
CHANGED
@@ -6,7 +6,7 @@ module Sinatra
|
|
6
6
|
|
7
7
|
InvalidParameter = Class.new(RuntimeError)
|
8
8
|
|
9
|
-
def validate(parameters,
|
9
|
+
def validate(parameters, key, options = {})
|
10
10
|
default = options[:default]
|
11
11
|
type = options[:type]
|
12
12
|
cast = true
|
@@ -15,46 +15,46 @@ module Sinatra
|
|
15
15
|
cast = options[:cast]
|
16
16
|
end
|
17
17
|
|
18
|
-
param = parameters[
|
18
|
+
param = parameters[key]
|
19
19
|
|
20
20
|
if !param
|
21
21
|
if options.key?(:default)
|
22
|
-
return param = parameters[
|
22
|
+
return param = parameters[key] = default
|
23
23
|
end
|
24
24
|
|
25
|
-
raise InvalidParameter.new("Missing required parameter #{
|
25
|
+
raise InvalidParameter.new("Missing required parameter #{key}")
|
26
26
|
end
|
27
27
|
|
28
28
|
if cast
|
29
29
|
if type == Integer
|
30
|
-
param = parameters[
|
30
|
+
param = parameters[key] = cast_into_integer(param, key)
|
31
31
|
elsif type == String
|
32
|
-
param = parameters[
|
32
|
+
param = parameters[key] = cast_into_string(param, key)
|
33
33
|
else
|
34
|
-
raise InvalidParameter.new("Unsupported casting for #{
|
34
|
+
raise InvalidParameter.new("Unsupported casting for #{key}")
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
if type && !param.is_a?(type)
|
39
|
-
raise InvalidParameter.new("Wrong type for #{
|
39
|
+
raise InvalidParameter.new("Wrong type for #{key}. Expected #{type}, got #{param.class}")
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
private
|
44
44
|
|
45
|
-
def cast_into_integer(param)
|
45
|
+
def cast_into_integer(param, key)
|
46
46
|
begin
|
47
47
|
Integer(param)
|
48
48
|
rescue
|
49
|
-
raise InvalidParameter.new("Unable to convert #{
|
49
|
+
raise InvalidParameter.new("Unable to convert #{key} into Integer")
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
def cast_into_string(param)
|
53
|
+
def cast_into_string(param, key)
|
54
54
|
begin
|
55
55
|
String(param)
|
56
56
|
rescue
|
57
|
-
raise InvalidParameter.new("Unable to convert #{
|
57
|
+
raise InvalidParameter.new("Unable to convert #{key} into String")
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Gimenez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|