explicit 0.2.4 → 0.2.5
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/README.md +65 -13
- data/app/views/explicit/documentation/_attribute.html.erb +2 -2
- data/app/views/explicit/documentation/_page.html.erb +7 -82
- data/app/views/explicit/documentation/_request.html.erb +3 -6
- data/app/views/explicit/documentation/type/_array.html.erb +1 -1
- data/app/views/explicit/documentation/type/_date.html.erb +1 -0
- data/app/views/explicit/documentation/type/_date_range.html.erb +6 -0
- data/app/views/explicit/documentation/type/_date_time_iso8601_range.html.erb +8 -0
- data/app/views/explicit/documentation/type/_hash.html.erb +2 -2
- data/app/views/explicit/documentation/type/_string.html.erb +4 -4
- data/config/locales/en.yml +34 -7
- data/lib/explicit/documentation/builder.rb +3 -0
- data/lib/explicit/documentation/output/swagger.rb +4 -0
- data/lib/explicit/documentation/output/webpage.rb +5 -0
- data/lib/explicit/request/invalid_response_error.rb +1 -1
- data/lib/explicit/request.rb +18 -16
- data/lib/explicit/test_helper.rb +13 -0
- data/lib/explicit/type/agreement.rb +5 -5
- data/lib/explicit/type/array.rb +12 -13
- data/lib/explicit/type/big_decimal.rb +14 -14
- data/lib/explicit/type/boolean.rb +4 -5
- data/lib/explicit/type/date.rb +66 -0
- data/lib/explicit/type/date_range.rb +95 -0
- data/lib/explicit/type/date_time_iso8601.rb +33 -8
- data/lib/explicit/type/date_time_iso8601_range.rb +108 -0
- data/lib/explicit/type/date_time_unix_epoch.rb +69 -0
- data/lib/explicit/type/enum.rb +4 -5
- data/lib/explicit/type/file.rb +7 -7
- data/lib/explicit/type/hash.rb +14 -14
- data/lib/explicit/type/integer.rb +9 -9
- data/lib/explicit/type/literal.rb +4 -5
- data/lib/explicit/type/modifiers/default.rb +1 -1
- data/lib/explicit/type/modifiers/description.rb +1 -1
- data/lib/explicit/type/modifiers/nilable.rb +1 -1
- data/lib/explicit/type/one_of.rb +59 -1
- data/lib/explicit/type/record.rb +5 -6
- data/lib/explicit/type/string.rb +19 -19
- data/lib/explicit/type.rb +58 -23
- data/lib/explicit/version.rb +1 -1
- data/lib/explicit.rb +4 -1
- metadata +10 -4
- data/lib/explicit/type/date_time_posix.rb +0 -44
- /data/app/views/explicit/documentation/type/{_date_time_posix.html.erb → _date_time_unix_epoch.html.erb} +0 -0
@@ -9,23 +9,23 @@ class Explicit::Type::BigDecimal < Explicit::Type
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def validate(value)
|
12
|
-
|
13
|
-
return
|
12
|
+
if !value.is_a?(::BigDecimal) && !value.is_a?(::String) && !value.is_a?(::Integer)
|
13
|
+
return error_i18n("big_decimal")
|
14
14
|
end
|
15
15
|
|
16
|
-
|
16
|
+
decimal_value = BigDecimal(value)
|
17
17
|
|
18
|
-
if min &&
|
19
|
-
return
|
18
|
+
if min && decimal_value < min
|
19
|
+
return error_i18n("min", min:)
|
20
20
|
end
|
21
21
|
|
22
|
-
if max &&
|
23
|
-
return
|
22
|
+
if max && decimal_value > max
|
23
|
+
return error_i18n("max", max:)
|
24
24
|
end
|
25
25
|
|
26
|
-
[:ok,
|
26
|
+
[:ok, decimal_value]
|
27
27
|
rescue ArgumentError
|
28
|
-
return
|
28
|
+
return error_i18n("big_decimal")
|
29
29
|
end
|
30
30
|
|
31
31
|
concerning :Webpage do
|
@@ -44,15 +44,15 @@ class Explicit::Type::BigDecimal < Explicit::Type
|
|
44
44
|
|
45
45
|
concerning :Swagger do
|
46
46
|
def swagger_schema
|
47
|
-
{
|
47
|
+
merge_base_swagger_schema({
|
48
48
|
type: "string",
|
49
|
-
pattern: /^\d*\.?\d*$/.inspect,
|
49
|
+
pattern: /^\d*\.?\d*$/.inspect[1..-2],
|
50
50
|
format: "decimal number",
|
51
|
-
|
51
|
+
description_topics: [
|
52
52
|
min&.then { swagger_i18n("big_decimal_min", min: _1) },
|
53
53
|
max&.then { swagger_i18n("big_decimal_max", max: _1) }
|
54
|
-
]
|
55
|
-
}
|
54
|
+
]
|
55
|
+
})
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -17,7 +17,7 @@ class Explicit::Type::Boolean < Explicit::Type
|
|
17
17
|
def validate(value)
|
18
18
|
value = VALUES[value]
|
19
19
|
|
20
|
-
return
|
20
|
+
return error_i18n("boolean") if value.nil?
|
21
21
|
|
22
22
|
[:ok, value]
|
23
23
|
end
|
@@ -38,10 +38,9 @@ class Explicit::Type::Boolean < Explicit::Type
|
|
38
38
|
|
39
39
|
concerning :Swagger do
|
40
40
|
def swagger_schema
|
41
|
-
{
|
42
|
-
type: "boolean"
|
43
|
-
|
44
|
-
}
|
41
|
+
merge_base_swagger_schema({
|
42
|
+
type: "boolean"
|
43
|
+
})
|
45
44
|
end
|
46
45
|
end
|
47
46
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Explicit::Type::Date < Explicit::Type
|
4
|
+
attr_reader :min, :max
|
5
|
+
|
6
|
+
Eval = ->(expr) { expr.respond_to?(:call) ? expr.call : expr }
|
7
|
+
|
8
|
+
def initialize(min: nil, max: nil)
|
9
|
+
@min = min
|
10
|
+
@max = max
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate(value)
|
14
|
+
return [:ok, value] if value.is_a?(::Date)
|
15
|
+
return error_i18n("string") if !value.is_a?(::String)
|
16
|
+
|
17
|
+
date = ::Date.parse(value, false)
|
18
|
+
|
19
|
+
if min
|
20
|
+
min_value = Eval[min]
|
21
|
+
|
22
|
+
if date.before?(min_value)
|
23
|
+
return error_i18n("date_min", min: min_value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if max
|
28
|
+
max_value = Eval[max]
|
29
|
+
|
30
|
+
if date.after?(max_value)
|
31
|
+
return error_i18n("date_max", max: max_value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
[:ok, date]
|
36
|
+
rescue ::Date::Error
|
37
|
+
error_i18n("date_format")
|
38
|
+
end
|
39
|
+
|
40
|
+
concerning :Webpage do
|
41
|
+
def summary
|
42
|
+
"string"
|
43
|
+
end
|
44
|
+
|
45
|
+
def partial
|
46
|
+
"explicit/documentation/type/date"
|
47
|
+
end
|
48
|
+
|
49
|
+
def has_details?
|
50
|
+
true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
concerning :Swagger do
|
55
|
+
def swagger_schema
|
56
|
+
{
|
57
|
+
type: "string",
|
58
|
+
pattern: /\d{4}-\d{2}-\d{2}/.inspect[1..-2],
|
59
|
+
format: "date",
|
60
|
+
description_topics: [
|
61
|
+
swagger_i18n("date_format")
|
62
|
+
]
|
63
|
+
}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Explicit::Type::DateRange < Explicit::Type
|
4
|
+
attr_reader :min_range, :max_range, :min_date, :max_date
|
5
|
+
|
6
|
+
FORMAT = /^(\d{4}-\d{2}-\d{2})\.\.(\d{4}-\d{2}-\d{2})$/.freeze
|
7
|
+
|
8
|
+
Eval = ->(value) do
|
9
|
+
value.respond_to?(:call) ? value.call : value
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(min_range: nil, max_range: nil, min_date: nil, max_date: nil)
|
13
|
+
@min_range = min_range
|
14
|
+
@max_range = max_range
|
15
|
+
@min_date = min_date
|
16
|
+
@max_date = max_date
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate(value)
|
20
|
+
return error_i18n("string") if !value.is_a?(::String)
|
21
|
+
|
22
|
+
match = FORMAT.match(value)
|
23
|
+
|
24
|
+
return error_i18n("date_range_format") if !match
|
25
|
+
|
26
|
+
date_1, date_2 = match.captures.map(&:to_date)
|
27
|
+
|
28
|
+
if date_1.after?(date_2)
|
29
|
+
return error_i18n("date_range_inverted")
|
30
|
+
end
|
31
|
+
|
32
|
+
if min_date
|
33
|
+
min_date_value = Eval[min_date]
|
34
|
+
|
35
|
+
if date_1 < min_date_value
|
36
|
+
return error_i18n("date_range_min_date", min_date: min_date_value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if max_date
|
41
|
+
max_date_value = Eval[max_date]
|
42
|
+
|
43
|
+
if date_2 > max_date_value
|
44
|
+
return error_i18n("date_range_max_date", max_date: max_date_value)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
if min_range
|
49
|
+
diff_in_days = date_2 - date_1 + 1
|
50
|
+
|
51
|
+
if diff_in_days < min_range.in_days
|
52
|
+
return error_i18n("date_range_min_range", min_range: min_range.inspect)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
if max_range
|
57
|
+
diff_in_days = date_2 - date_1 + 1
|
58
|
+
|
59
|
+
if diff_in_days > max_range.in_days
|
60
|
+
return error_i18n("date_range_max_range", max_range: max_range.inspect)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
[:ok, Range.new(date_1, date_2)]
|
65
|
+
end
|
66
|
+
|
67
|
+
concerning :Webpage do
|
68
|
+
def summary
|
69
|
+
"string"
|
70
|
+
end
|
71
|
+
|
72
|
+
def partial
|
73
|
+
"explicit/documentation/type/date_range"
|
74
|
+
end
|
75
|
+
|
76
|
+
def has_details?
|
77
|
+
true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
concerning :Swagger do
|
82
|
+
def swagger_schema
|
83
|
+
merge_base_swagger_schema({
|
84
|
+
type: "string",
|
85
|
+
pattern: FORMAT.inspect[1..-2],
|
86
|
+
format: "date range",
|
87
|
+
description_topics: [
|
88
|
+
swagger_i18n("date_range"),
|
89
|
+
min_range&.then { swagger_i18n("date_range_min_range", min_range: _1.inspect) },
|
90
|
+
max_range&.then { swagger_i18n("date_range_max_range", max_range: _1.inspect) },
|
91
|
+
]
|
92
|
+
})
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -3,14 +3,39 @@
|
|
3
3
|
require "time"
|
4
4
|
|
5
5
|
class Explicit::Type::DateTimeISO8601 < Explicit::Type
|
6
|
+
attr_reader :min, :max
|
7
|
+
|
8
|
+
Eval = ->(expr) { expr.respond_to?(:call) ? expr.call : expr }
|
9
|
+
|
10
|
+
def initialize(min: nil, max: nil)
|
11
|
+
@min = min
|
12
|
+
@max = max
|
13
|
+
end
|
14
|
+
|
6
15
|
def validate(value)
|
7
|
-
return
|
16
|
+
return error_i18n("date_time_iso8601") if !value.is_a?(::String)
|
17
|
+
|
18
|
+
datetime = Time.iso8601(value)
|
8
19
|
|
9
|
-
|
20
|
+
if min
|
21
|
+
min_value = Eval[min]
|
22
|
+
|
23
|
+
if datetime.before?(min_value)
|
24
|
+
return error_i18n("date_time_iso8601_min", min: min_value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
if max
|
29
|
+
max_value = Eval[max]
|
30
|
+
|
31
|
+
if datetime.after?(max_value)
|
32
|
+
return error_i18n("date_time_iso8601_max", max: max_value)
|
33
|
+
end
|
34
|
+
end
|
10
35
|
|
11
|
-
[:ok,
|
36
|
+
[:ok, datetime]
|
12
37
|
rescue ArgumentError
|
13
|
-
|
38
|
+
error_i18n("date_time_iso8601")
|
14
39
|
end
|
15
40
|
|
16
41
|
concerning :Webpage do
|
@@ -29,13 +54,13 @@ class Explicit::Type::DateTimeISO8601 < Explicit::Type
|
|
29
54
|
|
30
55
|
concerning :Swagger do
|
31
56
|
def swagger_schema
|
32
|
-
{
|
57
|
+
merge_base_swagger_schema({
|
33
58
|
type: "string",
|
34
59
|
format: "date-time",
|
35
|
-
|
60
|
+
description_topics: [
|
36
61
|
swagger_i18n("date_time_iso8601")
|
37
|
-
]
|
38
|
-
}
|
62
|
+
]
|
63
|
+
})
|
39
64
|
end
|
40
65
|
end
|
41
66
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Explicit::Type::DateTimeISO8601Range < Explicit::Type
|
4
|
+
attr_reader :min_range, :max_range, :min_date_time, :max_date_time
|
5
|
+
|
6
|
+
def initialize(min_range: nil, max_range: nil, min_date_time: nil, max_date_time: nil)
|
7
|
+
@min_range = min_range
|
8
|
+
@max_range = max_range
|
9
|
+
@min_date_time = min_date_time
|
10
|
+
@max_date_time = max_date_time
|
11
|
+
end
|
12
|
+
|
13
|
+
Parse = ->(str) do
|
14
|
+
Time.iso8601(str)
|
15
|
+
rescue ArgumentError
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
Eval = ->(value) do
|
20
|
+
value.respond_to?(:call) ? value.call : value
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate(value)
|
24
|
+
return error_i18n("string") if !value.is_a?(::String)
|
25
|
+
|
26
|
+
parts = value.split("..")
|
27
|
+
|
28
|
+
if parts.size != 2
|
29
|
+
return error_i18n("date_time_iso8601_range_format")
|
30
|
+
end
|
31
|
+
|
32
|
+
date_time_1, date_time_2 = parts.map(&Parse)
|
33
|
+
|
34
|
+
if date_time_1.nil? || date_time_2.nil?
|
35
|
+
return error_i18n("date_time_iso8601_range_format")
|
36
|
+
end
|
37
|
+
|
38
|
+
if date_time_1.after?(date_time_2)
|
39
|
+
return error_i18n("date_time_iso8601_range_inverted")
|
40
|
+
end
|
41
|
+
|
42
|
+
if min_date_time
|
43
|
+
min_date_time_value = Eval[min_date_time]
|
44
|
+
|
45
|
+
if date_time_1 < min_date_time_value
|
46
|
+
return error_i18n("date_time_iso8601_range_min_date_time", min_date_time: min_date_time_value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
if max_date_time
|
51
|
+
max_date_time_value = Eval[max_date_time]
|
52
|
+
|
53
|
+
if date_time_2 > max_date_time_value
|
54
|
+
return error_i18n("date_time_iso8601_range_max_date_time", max_date_time: max_date_time_value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if min_range
|
59
|
+
min_range_value = Eval[min_range]
|
60
|
+
|
61
|
+
diff_in_seconds = date_time_2 - date_time_1
|
62
|
+
|
63
|
+
if diff_in_seconds < min_range_value.in_seconds
|
64
|
+
return error_i18n("date_time_iso8601_range_min_range", min_range: min_range_value.inspect)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if max_range
|
69
|
+
max_range_value = Eval[max_range]
|
70
|
+
|
71
|
+
diff_in_seconds = date_time_2 - date_time_1
|
72
|
+
|
73
|
+
if diff_in_seconds > max_range_value.in_seconds
|
74
|
+
return error_i18n("date_time_iso8601_range_max_range", max_range: max_range_value.inspect)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
[:ok, Range.new(date_time_1, date_time_2)]
|
79
|
+
end
|
80
|
+
|
81
|
+
concerning :Webpage do
|
82
|
+
def summary
|
83
|
+
"string"
|
84
|
+
end
|
85
|
+
|
86
|
+
def partial
|
87
|
+
"explicit/documentation/type/date_time_iso8601_range"
|
88
|
+
end
|
89
|
+
|
90
|
+
def has_details?
|
91
|
+
true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
concerning :Swagger do
|
96
|
+
def swagger_schema
|
97
|
+
merge_base_swagger_schema({
|
98
|
+
type: "string",
|
99
|
+
format: "date time range",
|
100
|
+
description_topics: [
|
101
|
+
swagger_i18n("date_time_iso8601_range"),
|
102
|
+
min_range&.then { swagger_i18n("date_time_iso8601_range_min_range", min_range: _1.inspect) },
|
103
|
+
max_range&.then { swagger_i18n("date_time_iso8601_range_max_range", max_range: _1.inspect) },
|
104
|
+
]
|
105
|
+
})
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "time"
|
4
|
+
|
5
|
+
class Explicit::Type::DateTimeUnixEpoch < Explicit::Type
|
6
|
+
attr_reader :min, :max
|
7
|
+
|
8
|
+
Eval = ->(expr) { expr.respond_to?(:call) ? expr.call : expr }
|
9
|
+
|
10
|
+
def initialize(min: nil, max: nil)
|
11
|
+
@min = min
|
12
|
+
@max = max
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate(value)
|
16
|
+
if !value.is_a?(::Integer) && !value.is_a?(::String)
|
17
|
+
return error_i18n("date_time_unix_epoch")
|
18
|
+
end
|
19
|
+
|
20
|
+
datetime = DateTime.strptime(value.to_s, "%s")
|
21
|
+
|
22
|
+
if min
|
23
|
+
min_value = Eval[min]
|
24
|
+
|
25
|
+
if datetime.before?(min_value)
|
26
|
+
return error_i18n("date_time_unix_epoch_min", min: min_value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if max
|
31
|
+
max_value = Eval[max]
|
32
|
+
|
33
|
+
if datetime.after?(max_value)
|
34
|
+
return error_i18n("date_time_unix_epoch_max", max: max_value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
[:ok, datetime]
|
39
|
+
rescue ::Date::Error
|
40
|
+
return error_i18n("date_time_unix_epoch")
|
41
|
+
end
|
42
|
+
|
43
|
+
concerning :Webpage do
|
44
|
+
def summary
|
45
|
+
"integer"
|
46
|
+
end
|
47
|
+
|
48
|
+
def partial
|
49
|
+
"explicit/documentation/type/date_time_unix_epoch"
|
50
|
+
end
|
51
|
+
|
52
|
+
def has_details?
|
53
|
+
true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
concerning :Swagger do
|
58
|
+
def swagger_schema
|
59
|
+
merge_base_swagger_schema({
|
60
|
+
type: "integer",
|
61
|
+
minimum: 1,
|
62
|
+
format: "POSIX time",
|
63
|
+
description_topics: [
|
64
|
+
swagger_i18n("date_time_unix_epoch")
|
65
|
+
]
|
66
|
+
})
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/explicit/type/enum.rb
CHANGED
@@ -11,7 +11,7 @@ class Explicit::Type::Enum < Explicit::Type
|
|
11
11
|
if allowed_values.include?(value)
|
12
12
|
[:ok, value]
|
13
13
|
else
|
14
|
-
|
14
|
+
error_i18n("enum", allowed_values: allowed_values.inspect)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -31,11 +31,10 @@ class Explicit::Type::Enum < Explicit::Type
|
|
31
31
|
|
32
32
|
concerning :Swagger do
|
33
33
|
def swagger_schema
|
34
|
-
{
|
34
|
+
merge_base_swagger_schema({
|
35
35
|
type: "string",
|
36
|
-
enum: allowed_values
|
37
|
-
|
38
|
-
}
|
36
|
+
enum: allowed_values
|
37
|
+
})
|
39
38
|
end
|
40
39
|
end
|
41
40
|
end
|
data/lib/explicit/type/file.rb
CHANGED
@@ -17,15 +17,15 @@ class Explicit::Type::File < Explicit::Type
|
|
17
17
|
|
18
18
|
def validate(value)
|
19
19
|
if !FILE_CLASSES.any? { |klass| value.is_a?(klass) }
|
20
|
-
return
|
20
|
+
return error_i18n("file")
|
21
21
|
end
|
22
22
|
|
23
23
|
if max_size && value.size > max_size
|
24
|
-
return
|
24
|
+
return error_i18n("file_max_size", max_size: number_to_human_size(max_size))
|
25
25
|
end
|
26
26
|
|
27
27
|
if content_types.any? && !content_types.include?(value.content_type)
|
28
|
-
return
|
28
|
+
return error_i18n("file_content_type", allowed_content_types: content_types.inspect)
|
29
29
|
end
|
30
30
|
|
31
31
|
[:ok, value]
|
@@ -47,14 +47,14 @@ class Explicit::Type::File < Explicit::Type
|
|
47
47
|
|
48
48
|
concerning :Swagger do
|
49
49
|
def swagger_schema
|
50
|
-
{
|
50
|
+
merge_base_swagger_schema({
|
51
51
|
type: "string",
|
52
52
|
format: "binary",
|
53
|
-
|
53
|
+
description_topics: [
|
54
54
|
max_size&.then { swagger_i18n("file_max_size", max_size: number_to_human_size(_1)) },
|
55
55
|
content_types.any? ? swagger_i18n("file_content_types", content_types: content_types.join(', ')) : nil
|
56
|
-
]
|
57
|
-
}
|
56
|
+
]
|
57
|
+
})
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
data/lib/explicit/type/hash.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Explicit::Type::Hash < Explicit::Type
|
4
|
-
attr_reader :
|
4
|
+
attr_reader :key_type, :value_type, :empty
|
5
5
|
|
6
|
-
def initialize(
|
7
|
-
@
|
8
|
-
@
|
6
|
+
def initialize(key_type:, value_type:, empty: true)
|
7
|
+
@key_type = Explicit::Type.build(key_type)
|
8
|
+
@value_type = Explicit::Type.build(value_type)
|
9
9
|
@empty = empty
|
10
10
|
end
|
11
11
|
|
12
12
|
def validate(value)
|
13
|
-
return
|
14
|
-
return
|
13
|
+
return error_i18n("hash") if !value.respond_to?(:[])
|
14
|
+
return error_i18n("empty") if value.empty? && empty == false
|
15
15
|
|
16
16
|
validated_hash = {}
|
17
17
|
|
18
18
|
value.each do |key, value|
|
19
|
-
case [
|
19
|
+
case [key_type.validate(key), value_type.validate(value)]
|
20
20
|
in [[:ok, validated_key], [:ok, validated_value]]
|
21
21
|
validated_hash[validated_key] = validated_value
|
22
22
|
in [[:error, error], _]
|
23
|
-
return
|
23
|
+
return error_i18n("hash_key", key:, error:)
|
24
24
|
in [_, [:error, error]]
|
25
|
-
return
|
25
|
+
return error_i18n("hash_value", key:, error:)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -45,13 +45,13 @@ class Explicit::Type::Hash < Explicit::Type
|
|
45
45
|
|
46
46
|
concerning :Swagger do
|
47
47
|
def swagger_schema
|
48
|
-
{
|
48
|
+
merge_base_swagger_schema({
|
49
49
|
type: "object",
|
50
|
-
additionalProperties:
|
51
|
-
|
50
|
+
additionalProperties: value_type.swagger_schema,
|
51
|
+
description_topics: [
|
52
52
|
empty == false ? swagger_i18n("hash_not_empty") : nil
|
53
|
-
]
|
54
|
-
}
|
53
|
+
]
|
54
|
+
})
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
@@ -26,22 +26,22 @@ class Explicit::Type::Integer < Explicit::Type
|
|
26
26
|
nil
|
27
27
|
end
|
28
28
|
|
29
|
-
return
|
29
|
+
return error_i18n("integer") if value.nil?
|
30
30
|
|
31
31
|
if min && value < min
|
32
|
-
return
|
32
|
+
return error_i18n("min", min:)
|
33
33
|
end
|
34
34
|
|
35
35
|
if max && value > max
|
36
|
-
return
|
36
|
+
return error_i18n("max", max:)
|
37
37
|
end
|
38
38
|
|
39
39
|
if negative == false && value < 0
|
40
|
-
return
|
40
|
+
return error_i18n("not_negative")
|
41
41
|
end
|
42
42
|
|
43
43
|
if positive == false && value > 0
|
44
|
-
return
|
44
|
+
return error_i18n("not_positive")
|
45
45
|
end
|
46
46
|
|
47
47
|
[:ok, value]
|
@@ -63,17 +63,17 @@ class Explicit::Type::Integer < Explicit::Type
|
|
63
63
|
|
64
64
|
concerning :Swagger do
|
65
65
|
def swagger_schema
|
66
|
-
{
|
66
|
+
merge_base_swagger_schema({
|
67
67
|
type: "integer",
|
68
68
|
minimum: min,
|
69
69
|
maximum: max,
|
70
|
-
|
70
|
+
description_topics: [
|
71
71
|
positive == false ? swagger_i18n("integer_not_positive") : nil,
|
72
72
|
positive == true ? swagger_i18n("integer_only_positive") : nil,
|
73
73
|
negative == false ? swagger_i18n("integer_not_negative") : nil,
|
74
74
|
negative == true ? swagger_i18n("integer_only_negative") : nil
|
75
|
-
]
|
76
|
-
}
|
75
|
+
]
|
76
|
+
})
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -15,7 +15,7 @@ class Explicit::Type::Literal < Explicit::Type
|
|
15
15
|
if value == @value
|
16
16
|
[:ok, value]
|
17
17
|
else
|
18
|
-
|
18
|
+
error_i18n("literal", value: @value.inspect)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -35,11 +35,10 @@ class Explicit::Type::Literal < Explicit::Type
|
|
35
35
|
|
36
36
|
concerning :Swagger do
|
37
37
|
def swagger_schema
|
38
|
-
{
|
38
|
+
merge_base_swagger_schema({
|
39
39
|
type: @value.is_a?(::String) ? "string" : "integer",
|
40
|
-
enum: [@value]
|
41
|
-
|
42
|
-
}
|
40
|
+
enum: [@value]
|
41
|
+
})
|
43
42
|
end
|
44
43
|
end
|
45
44
|
end
|