cmdx 1.0.0 → 1.1.0
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/.cursor/prompts/rspec.md +20 -0
- data/.cursor/prompts/yardoc.md +8 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +101 -49
- data/README.md +2 -1
- data/docs/ai_prompts.md +10 -0
- data/docs/basics/call.md +11 -2
- data/docs/basics/chain.md +10 -1
- data/docs/basics/context.md +9 -0
- data/docs/basics/setup.md +9 -0
- data/docs/callbacks.md +14 -37
- data/docs/configuration.md +68 -27
- data/docs/getting_started.md +11 -0
- data/docs/internationalization.md +148 -0
- data/docs/interruptions/exceptions.md +10 -1
- data/docs/interruptions/faults.md +11 -2
- data/docs/interruptions/halt.md +9 -0
- data/docs/logging.md +14 -4
- data/docs/middlewares.md +53 -43
- data/docs/outcomes/result.md +9 -0
- data/docs/outcomes/states.md +9 -0
- data/docs/outcomes/statuses.md +9 -0
- data/docs/parameters/coercions.md +58 -38
- data/docs/parameters/defaults.md +10 -1
- data/docs/parameters/definitions.md +9 -0
- data/docs/parameters/namespacing.md +9 -0
- data/docs/parameters/validations.md +8 -67
- data/docs/testing.md +22 -13
- data/docs/tips_and_tricks.md +9 -0
- data/docs/workflows.md +14 -4
- data/lib/cmdx/.DS_Store +0 -0
- data/lib/cmdx/callback.rb +36 -56
- data/lib/cmdx/callback_registry.rb +82 -73
- data/lib/cmdx/chain.rb +65 -122
- data/lib/cmdx/chain_inspector.rb +22 -115
- data/lib/cmdx/chain_serializer.rb +17 -148
- data/lib/cmdx/coercion.rb +49 -0
- data/lib/cmdx/coercion_registry.rb +94 -0
- data/lib/cmdx/coercions/array.rb +18 -36
- data/lib/cmdx/coercions/big_decimal.rb +21 -33
- data/lib/cmdx/coercions/boolean.rb +21 -40
- data/lib/cmdx/coercions/complex.rb +18 -31
- data/lib/cmdx/coercions/date.rb +20 -39
- data/lib/cmdx/coercions/date_time.rb +22 -39
- data/lib/cmdx/coercions/float.rb +19 -32
- data/lib/cmdx/coercions/hash.rb +22 -41
- data/lib/cmdx/coercions/integer.rb +20 -33
- data/lib/cmdx/coercions/rational.rb +20 -32
- data/lib/cmdx/coercions/string.rb +23 -31
- data/lib/cmdx/coercions/time.rb +24 -40
- data/lib/cmdx/coercions/virtual.rb +14 -31
- data/lib/cmdx/configuration.rb +57 -171
- data/lib/cmdx/context.rb +22 -165
- data/lib/cmdx/core_ext/hash.rb +42 -67
- data/lib/cmdx/core_ext/module.rb +35 -79
- data/lib/cmdx/core_ext/object.rb +63 -98
- data/lib/cmdx/correlator.rb +40 -156
- data/lib/cmdx/error.rb +37 -202
- data/lib/cmdx/errors.rb +165 -202
- data/lib/cmdx/fault.rb +55 -158
- data/lib/cmdx/faults.rb +26 -137
- data/lib/cmdx/immutator.rb +22 -109
- data/lib/cmdx/lazy_struct.rb +103 -187
- data/lib/cmdx/log_formatters/json.rb +14 -40
- data/lib/cmdx/log_formatters/key_value.rb +14 -40
- data/lib/cmdx/log_formatters/line.rb +14 -48
- data/lib/cmdx/log_formatters/logstash.rb +14 -57
- data/lib/cmdx/log_formatters/pretty_json.rb +14 -50
- data/lib/cmdx/log_formatters/pretty_key_value.rb +13 -46
- data/lib/cmdx/log_formatters/pretty_line.rb +16 -54
- data/lib/cmdx/log_formatters/raw.rb +19 -49
- data/lib/cmdx/logger.rb +20 -82
- data/lib/cmdx/logger_ansi.rb +18 -75
- data/lib/cmdx/logger_serializer.rb +24 -114
- data/lib/cmdx/middleware.rb +38 -60
- data/lib/cmdx/middleware_registry.rb +81 -77
- data/lib/cmdx/middlewares/correlate.rb +41 -226
- data/lib/cmdx/middlewares/timeout.rb +46 -185
- data/lib/cmdx/parameter.rb +120 -198
- data/lib/cmdx/parameter_evaluator.rb +231 -0
- data/lib/cmdx/parameter_inspector.rb +25 -56
- data/lib/cmdx/parameter_registry.rb +59 -84
- data/lib/cmdx/parameter_serializer.rb +23 -74
- data/lib/cmdx/railtie.rb +24 -107
- data/lib/cmdx/result.rb +254 -260
- data/lib/cmdx/result_ansi.rb +19 -85
- data/lib/cmdx/result_inspector.rb +27 -68
- data/lib/cmdx/result_logger.rb +18 -81
- data/lib/cmdx/result_serializer.rb +28 -132
- data/lib/cmdx/rspec/matchers.rb +28 -0
- data/lib/cmdx/rspec/result_matchers/be_executed.rb +42 -0
- data/lib/cmdx/rspec/result_matchers/be_failed_task.rb +94 -0
- data/lib/cmdx/rspec/result_matchers/be_skipped_task.rb +94 -0
- data/lib/cmdx/rspec/result_matchers/be_state_matchers.rb +59 -0
- data/lib/cmdx/rspec/result_matchers/be_status_matchers.rb +57 -0
- data/lib/cmdx/rspec/result_matchers/be_successful_task.rb +87 -0
- data/lib/cmdx/rspec/result_matchers/have_bad_outcome.rb +51 -0
- data/lib/cmdx/rspec/result_matchers/have_caused_failure.rb +58 -0
- data/lib/cmdx/rspec/result_matchers/have_chain_index.rb +59 -0
- data/lib/cmdx/rspec/result_matchers/have_context.rb +86 -0
- data/lib/cmdx/rspec/result_matchers/have_empty_metadata.rb +54 -0
- data/lib/cmdx/rspec/result_matchers/have_good_outcome.rb +52 -0
- data/lib/cmdx/rspec/result_matchers/have_metadata.rb +114 -0
- data/lib/cmdx/rspec/result_matchers/have_preserved_context.rb +66 -0
- data/lib/cmdx/rspec/result_matchers/have_received_thrown_failure.rb +64 -0
- data/lib/cmdx/rspec/result_matchers/have_runtime.rb +78 -0
- data/lib/cmdx/rspec/result_matchers/have_thrown_failure.rb +76 -0
- data/lib/cmdx/rspec/task_matchers/be_well_formed_task.rb +62 -0
- data/lib/cmdx/rspec/task_matchers/have_callback.rb +85 -0
- data/lib/cmdx/rspec/task_matchers/have_cmd_setting.rb +68 -0
- data/lib/cmdx/rspec/task_matchers/have_executed_callbacks.rb +92 -0
- data/lib/cmdx/rspec/task_matchers/have_middleware.rb +46 -0
- data/lib/cmdx/rspec/task_matchers/have_parameter.rb +181 -0
- data/lib/cmdx/task.rb +213 -425
- data/lib/cmdx/task_deprecator.rb +55 -0
- data/lib/cmdx/task_processor.rb +245 -0
- data/lib/cmdx/task_serializer.rb +22 -70
- data/lib/cmdx/utils/ansi_color.rb +13 -89
- data/lib/cmdx/utils/log_timestamp.rb +13 -42
- data/lib/cmdx/utils/monotonic_runtime.rb +13 -63
- data/lib/cmdx/utils/name_affix.rb +21 -71
- data/lib/cmdx/validator.rb +48 -0
- data/lib/cmdx/validator_registry.rb +86 -0
- data/lib/cmdx/validators/exclusion.rb +55 -94
- data/lib/cmdx/validators/format.rb +31 -85
- data/lib/cmdx/validators/inclusion.rb +65 -110
- data/lib/cmdx/validators/length.rb +117 -133
- data/lib/cmdx/validators/numeric.rb +123 -130
- data/lib/cmdx/validators/presence.rb +38 -79
- data/lib/cmdx/version.rb +1 -7
- data/lib/cmdx/workflow.rb +46 -339
- data/lib/cmdx.rb +1 -1
- data/lib/generators/cmdx/install_generator.rb +14 -31
- data/lib/generators/cmdx/task_generator.rb +39 -55
- data/lib/generators/cmdx/templates/install.rb +61 -11
- data/lib/generators/cmdx/workflow_generator.rb +41 -66
- data/lib/locales/ar.yml +35 -0
- data/lib/locales/cs.yml +35 -0
- data/lib/locales/da.yml +35 -0
- data/lib/locales/de.yml +35 -0
- data/lib/locales/el.yml +35 -0
- data/lib/locales/en.yml +19 -20
- data/lib/locales/es.yml +19 -20
- data/lib/locales/fi.yml +35 -0
- data/lib/locales/fr.yml +35 -0
- data/lib/locales/he.yml +35 -0
- data/lib/locales/hi.yml +35 -0
- data/lib/locales/it.yml +35 -0
- data/lib/locales/ja.yml +35 -0
- data/lib/locales/ko.yml +35 -0
- data/lib/locales/nl.yml +35 -0
- data/lib/locales/no.yml +35 -0
- data/lib/locales/pl.yml +35 -0
- data/lib/locales/pt.yml +35 -0
- data/lib/locales/ru.yml +35 -0
- data/lib/locales/sv.yml +35 -0
- data/lib/locales/th.yml +35 -0
- data/lib/locales/tr.yml +35 -0
- data/lib/locales/vi.yml +35 -0
- data/lib/locales/zh.yml +35 -0
- metadata +57 -8
- data/lib/cmdx/parameter_validator.rb +0 -81
- data/lib/cmdx/parameter_value.rb +0 -244
- data/lib/cmdx/parameters_inspector.rb +0 -72
- data/lib/cmdx/parameters_serializer.rb +0 -115
- data/lib/cmdx/rspec/result_matchers.rb +0 -917
- data/lib/cmdx/rspec/task_matchers.rb +0 -570
- data/lib/cmdx/validators/custom.rb +0 -102
@@ -2,102 +2,48 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module Validators
|
5
|
-
#
|
5
|
+
# Validator class for format validation using regular expressions.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
# patterns. It supports both positive matching (with)
|
9
|
-
# (without) patterns,
|
10
|
-
#
|
11
|
-
|
12
|
-
# class ProcessUserTask < CMDx::Task
|
13
|
-
# required :email, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
|
14
|
-
# required :phone, format: { with: /\A\d{3}-\d{3}-\d{4}\z/ }
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# @example Format validation with negative pattern
|
18
|
-
# class ProcessContentTask < CMDx::Task
|
19
|
-
# required :username, format: { without: /\A(admin|root|system)\z/i }
|
20
|
-
# required :content, format: { without: /spam|viagra/i }
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# @example Combined positive and negative patterns
|
24
|
-
# class ProcessUserTask < CMDx::Task
|
25
|
-
# required :password, format: {
|
26
|
-
# with: /\A(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}\z/, # Strong password
|
27
|
-
# without: /password|123456/i # Common weak patterns
|
28
|
-
# }
|
29
|
-
# end
|
30
|
-
#
|
31
|
-
# @example Custom error message
|
32
|
-
# class ProcessUserTask < CMDx::Task
|
33
|
-
# required :email, format: {
|
34
|
-
# with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i,
|
35
|
-
# message: "must be a valid email address"
|
36
|
-
# }
|
37
|
-
# end
|
38
|
-
#
|
39
|
-
# @example Format validation behavior
|
40
|
-
# # Positive pattern matching
|
41
|
-
# Format.call("user@example.com", format: { with: /@/ }) # passes
|
42
|
-
# Format.call("invalid-email", format: { with: /@/ }) # raises ValidationError
|
43
|
-
#
|
44
|
-
# # Negative pattern matching
|
45
|
-
# Format.call("username", format: { without: /admin/ }) # passes
|
46
|
-
# Format.call("admin", format: { without: /admin/ }) # raises ValidationError
|
47
|
-
#
|
48
|
-
# @see CMDx::Parameter Parameter validation integration
|
49
|
-
# @see CMDx::ValidationError Raised when validation fails
|
50
|
-
module Format
|
7
|
+
# This validator ensures that a value matches or doesn't match specified
|
8
|
+
# regular expression patterns. It supports both positive matching (with)
|
9
|
+
# and negative matching (without) patterns, which can be used independently
|
10
|
+
# or in combination.
|
11
|
+
class Format < Validator
|
51
12
|
|
52
|
-
|
53
|
-
|
54
|
-
# Validates that a parameter value matches the specified format patterns.
|
55
|
-
#
|
56
|
-
# Validates the value against the provided regular expression patterns.
|
57
|
-
# Supports positive matching (with), negative matching (without), or both.
|
58
|
-
# The value must match all specified conditions to pass validation.
|
13
|
+
# Validates that the given value matches the specified format pattern(s).
|
59
14
|
#
|
60
|
-
# @param value [
|
61
|
-
# @param options [Hash]
|
62
|
-
# @option options [Hash] :format
|
63
|
-
# @option options [Regexp] :format.with
|
64
|
-
# @option options [Regexp] :format.without
|
65
|
-
# @option options [String] :format.message
|
15
|
+
# @param value [Object] the value to validate
|
16
|
+
# @param options [Hash] validation options containing format configuration
|
17
|
+
# @option options [Hash] :format format validation configuration
|
18
|
+
# @option options [Regexp] :format.with pattern the value must match
|
19
|
+
# @option options [Regexp] :format.without pattern the value must not match
|
20
|
+
# @option options [String] :format.message custom error message
|
66
21
|
#
|
67
22
|
# @return [void]
|
68
|
-
# @raise [ValidationError] If value doesn't match the format requirements
|
69
23
|
#
|
70
|
-
# @
|
71
|
-
# Format.call("user@example.com", format: { with: /@/ })
|
72
|
-
# # => passes without error
|
24
|
+
# @raise [ValidationError] if the value doesn't match the format requirements
|
73
25
|
#
|
74
|
-
# @example
|
75
|
-
# Format.call("
|
76
|
-
# # =>
|
26
|
+
# @example Validating with a positive pattern
|
27
|
+
# Validators::Format.call("user123", format: { with: /\A[a-z]+\d+\z/ })
|
28
|
+
# # => nil (no error raised)
|
77
29
|
#
|
78
|
-
# @example
|
79
|
-
# Format.call("
|
80
|
-
# #
|
30
|
+
# @example Validating with a negative pattern
|
31
|
+
# Validators::Format.call("admin", format: { without: /admin|root/ })
|
32
|
+
# # raises ValidationError: "is an invalid format"
|
81
33
|
#
|
82
|
-
# @example
|
83
|
-
# Format.call("
|
84
|
-
# # =>
|
34
|
+
# @example Validating with both patterns
|
35
|
+
# Validators::Format.call("user123", format: { with: /\A[a-z]+\d+\z/, without: /admin|root/ })
|
36
|
+
# # => nil (no error raised)
|
85
37
|
#
|
86
|
-
# @example
|
87
|
-
# Format.call("
|
88
|
-
#
|
89
|
-
# without: /password/i
|
90
|
-
# })
|
91
|
-
# # => passes without error
|
38
|
+
# @example Invalid format with positive pattern
|
39
|
+
# Validators::Format.call("123abc", format: { with: /\A[a-z]+\d+\z/ })
|
40
|
+
# # raises ValidationError: "is an invalid format"
|
92
41
|
#
|
93
|
-
# @example
|
94
|
-
# Format.call("
|
95
|
-
#
|
96
|
-
# message: "must be at least 8 characters"
|
97
|
-
# })
|
98
|
-
# # => raises ValidationError: "must be at least 8 characters"
|
42
|
+
# @example Using a custom message
|
43
|
+
# Validators::Format.call("123abc", format: { with: /\A[a-z]+\d+\z/, message: "Username must start with letters" })
|
44
|
+
# # raises ValidationError: "Username must start with letters"
|
99
45
|
def call(value, options = {})
|
100
|
-
valid = case options
|
46
|
+
valid = case options
|
101
47
|
in { with: with, without: without }
|
102
48
|
value.match?(with) && !value.match?(without)
|
103
49
|
in { with: with }
|
@@ -110,7 +56,7 @@ module CMDx
|
|
110
56
|
|
111
57
|
return if valid
|
112
58
|
|
113
|
-
raise ValidationError, options
|
59
|
+
raise ValidationError, options[:message] || I18n.t(
|
114
60
|
"cmdx.validators.format",
|
115
61
|
default: "is an invalid format"
|
116
62
|
)
|
@@ -2,106 +2,50 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module Validators
|
5
|
-
#
|
5
|
+
# Validator class for including values within a specified set.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
|
11
|
-
# @example Basic inclusion validation with array
|
12
|
-
# class ProcessOrderTask < CMDx::Task
|
13
|
-
# required :status, inclusion: { in: ['pending', 'processing', 'completed'] }
|
14
|
-
# required :priority, inclusion: { in: [1, 2, 3, 4, 5] }
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# @example Range-based inclusion
|
18
|
-
# class ProcessUserTask < CMDx::Task
|
19
|
-
# required :age, inclusion: { in: 18..120 } # Valid age range
|
20
|
-
# required :score, inclusion: { within: 0..100 } # Percentage score
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# @example Custom error messages
|
24
|
-
# class ProcessOrderTask < CMDx::Task
|
25
|
-
# required :status, inclusion: {
|
26
|
-
# in: ['pending', 'processing', 'completed'],
|
27
|
-
# of_message: "must be a valid order status"
|
28
|
-
# }
|
29
|
-
# required :age, inclusion: {
|
30
|
-
# in: 18..120,
|
31
|
-
# in_message: "must be between %{min} and %{max} years old"
|
32
|
-
# }
|
33
|
-
# end
|
34
|
-
#
|
35
|
-
# @example Boolean field validation
|
36
|
-
# class ProcessUserTask < CMDx::Task
|
37
|
-
# required :active, inclusion: { in: [true, false] } # Proper boolean validation
|
38
|
-
# required :role, inclusion: { in: ['admin', 'user', 'guest'] }
|
39
|
-
# end
|
40
|
-
#
|
41
|
-
# @example Inclusion validation behavior
|
42
|
-
# # Array inclusion
|
43
|
-
# Inclusion.call("pending", inclusion: { in: ['pending', 'active'] }) # passes
|
44
|
-
# Inclusion.call("cancelled", inclusion: { in: ['pending', 'active'] }) # raises ValidationError
|
45
|
-
#
|
46
|
-
# # Range inclusion
|
47
|
-
# Inclusion.call(25, inclusion: { in: 18..65 }) # passes
|
48
|
-
# Inclusion.call(15, inclusion: { in: 18..65 }) # raises ValidationError
|
49
|
-
#
|
50
|
-
# @see CMDx::Validators::Exclusion For validating values must not be in a set
|
51
|
-
# @see CMDx::Parameter Parameter validation integration
|
52
|
-
# @see CMDx::ValidationError Raised when validation fails
|
53
|
-
module Inclusion
|
54
|
-
|
55
|
-
extend self
|
7
|
+
# This validator ensures that a value is included in a given array or range
|
8
|
+
# of allowed values. It supports both discrete value inclusion and range-based
|
9
|
+
# inclusion validation.
|
10
|
+
class Inclusion < Validator
|
56
11
|
|
57
|
-
# Validates that
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
# @
|
64
|
-
# @
|
65
|
-
# @option options [
|
66
|
-
# @option options [
|
67
|
-
# @option options [
|
68
|
-
# @option options [String] :inclusion.of_message Error message for array inclusion
|
69
|
-
# @option options [String] :inclusion.in_message Error message for range inclusion
|
70
|
-
# @option options [String] :inclusion.within_message Alias for :in_message
|
71
|
-
# @option options [String] :inclusion.message General error message override
|
12
|
+
# Validates that the given value is included in the inclusion set.
|
13
|
+
#
|
14
|
+
# @param value [Object] the value to validate
|
15
|
+
# @param options [Hash] validation options containing inclusion configuration
|
16
|
+
# @option options [Hash] :inclusion inclusion validation configuration
|
17
|
+
# @option options [Array, Range] :inclusion.in the values to include
|
18
|
+
# @option options [Array, Range] :inclusion.within alias for :in
|
19
|
+
# @option options [String] :inclusion.message custom error message
|
20
|
+
# @option options [String] :inclusion.of_message custom error message for array inclusion
|
21
|
+
# @option options [String] :inclusion.in_message custom error message for range inclusion
|
22
|
+
# @option options [String] :inclusion.within_message alias for :in_message
|
72
23
|
#
|
73
24
|
# @return [void]
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# @example Custom error messages
|
97
|
-
# Inclusion.call("invalid", inclusion: {
|
98
|
-
# in: ['valid', 'pending'],
|
99
|
-
# of_message: "status must be valid or pending"
|
100
|
-
# })
|
101
|
-
# # => raises ValidationError: "status must be valid or pending"
|
25
|
+
#
|
26
|
+
# @raise [ValidationError] if the value is not found in the inclusion set
|
27
|
+
#
|
28
|
+
# @example Including from an array
|
29
|
+
# Validators::Inclusion.call("user", inclusion: { in: ["user", "admin"] })
|
30
|
+
# # => nil (no error raised)
|
31
|
+
#
|
32
|
+
# @example Including from a range
|
33
|
+
# Validators::Inclusion.call(5, inclusion: { in: 1..10 })
|
34
|
+
# # => nil (no error raised)
|
35
|
+
#
|
36
|
+
# @example Invalid inclusion from array
|
37
|
+
# Validators::Inclusion.call("guest", inclusion: { in: ["user", "admin"] })
|
38
|
+
# # raises ValidationError: "must be one of: \"user\", \"admin\""
|
39
|
+
#
|
40
|
+
# @example Invalid inclusion from range
|
41
|
+
# Validators::Inclusion.call(15, inclusion: { in: 1..10 })
|
42
|
+
# # raises ValidationError: "must be within 1 and 10"
|
43
|
+
#
|
44
|
+
# @example Using a custom message
|
45
|
+
# Validators::Inclusion.call("guest", inclusion: { in: ["user", "admin"], message: "Invalid role selected" })
|
46
|
+
# # raises ValidationError: "Invalid role selected"
|
102
47
|
def call(value, options = {})
|
103
|
-
values = options
|
104
|
-
options.dig(:inclusion, :within)
|
48
|
+
values = options[:in] || options[:within]
|
105
49
|
|
106
50
|
if values.is_a?(Range)
|
107
51
|
raise_within_validation_error!(values.begin, values.end, options) unless values.cover?(value)
|
@@ -112,15 +56,21 @@ module CMDx
|
|
112
56
|
|
113
57
|
private
|
114
58
|
|
115
|
-
# Raises validation error for array-based inclusion
|
59
|
+
# Raises a validation error for array-based inclusion.
|
60
|
+
#
|
61
|
+
# @param values [Array] the allowed values
|
62
|
+
# @param options [Hash] validation options
|
63
|
+
#
|
64
|
+
# @return [void]
|
116
65
|
#
|
117
|
-
# @
|
118
|
-
#
|
119
|
-
# @
|
66
|
+
# @raise [ValidationError] always raised with appropriate message
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# raise_of_validation_error!(["user", "admin"], {})
|
70
|
+
# # raises ValidationError: "must be one of: \"user\", \"admin\""
|
120
71
|
def raise_of_validation_error!(values, options)
|
121
|
-
values = values.map(&:inspect).join(", ")
|
122
|
-
message = options
|
123
|
-
options.dig(:inclusion, :message)
|
72
|
+
values = values.map(&:inspect).join(", ") unless values.nil?
|
73
|
+
message = options[:of_message] || options[:message]
|
124
74
|
message %= { values: } unless message.nil?
|
125
75
|
|
126
76
|
raise ValidationError, message || I18n.t(
|
@@ -130,16 +80,21 @@ module CMDx
|
|
130
80
|
)
|
131
81
|
end
|
132
82
|
|
133
|
-
# Raises validation error for range-based inclusion
|
83
|
+
# Raises a validation error for range-based inclusion.
|
84
|
+
#
|
85
|
+
# @param min [Object] the minimum value of the range
|
86
|
+
# @param max [Object] the maximum value of the range
|
87
|
+
# @param options [Hash] validation options
|
88
|
+
#
|
89
|
+
# @return [void]
|
90
|
+
#
|
91
|
+
# @raise [ValidationError] always raised with appropriate message
|
134
92
|
#
|
135
|
-
# @
|
136
|
-
#
|
137
|
-
#
|
138
|
-
# @raise [ValidationError] With formatted error message
|
93
|
+
# @example
|
94
|
+
# raise_within_validation_error!(1, 10, {})
|
95
|
+
# # raises ValidationError: "must be within 1 and 10"
|
139
96
|
def raise_within_validation_error!(min, max, options)
|
140
|
-
message = options
|
141
|
-
options.dig(:inclusion, :within_message) ||
|
142
|
-
options.dig(:inclusion, :message)
|
97
|
+
message = options[:in_message] || options[:within_message] || options[:message]
|
143
98
|
message %= { min:, max: } unless message.nil?
|
144
99
|
|
145
100
|
raise ValidationError, message || I18n.t(
|