Class: Mayak::Validations::Rule
- Inherits:
-
Object
- Object
- Mayak::Validations::Rule
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Defined in:
- lib/mayak/validations/rule.rb
Constant Summary collapse
- Value =
type_member
- Error =
type_member
Class Method Summary collapse
- .equal_to(value) ⇒ Object
- .greater_than(value) ⇒ Object
- .greater_than_or_equal_to(value) ⇒ Object
- .less_than(value) ⇒ Object
- .less_than_or_equal_to(value) ⇒ Object
- .negative(value) ⇒ Object
- .not_empty ⇒ Object
- .not_nil(rule) ⇒ Object
- .positive(value) ⇒ Object
- .with_keys_aggregated(rule) ⇒ Object
Instance Method Summary collapse
- #any(another) ⇒ Object (also: #|)
- #both(another) ⇒ Object (also: #&)
- #check(value) ⇒ Object
- #error(new_error) ⇒ Object
- #error_from_value(&blk) ⇒ Object
-
#initialize(&blk) ⇒ Rule
constructor
A new instance of Rule.
- #with_key(key) ⇒ Object
Constructor Details
#initialize(&blk) ⇒ Rule
Returns a new instance of Rule.
19 20 21 |
# File 'lib/mayak/validations/rule.rb', line 19 def initialize(&blk) @blk = T.let(@blk, T.proc.params(arg0: Value).returns(::Mayak::ValidationResult[Error])) end |
Class Method Details
.equal_to(value) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mayak/validations/rule.rb', line 160 def self.equal_to(value) Rule[T.any(Float, Integer), String].new do |checked| if checked == value ::Mayak::ValidationResult::Valid.new else ::Mayak::ValidationResult::Invalid.new( errors: ["Value #{checked} should equal to #{value}"] ) end end end |
.greater_than(value) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mayak/validations/rule.rb', line 126 def self.greater_than(value) Rule[T.any(Float, Integer), String].new do |checked| if checked > value ::Mayak::ValidationResult::Valid.new else ::Mayak::ValidationResult::Invalid.new( errors: ["Value #{checked} should be greater than the #{value}"] ) end end end |
.greater_than_or_equal_to(value) ⇒ Object
177 178 179 180 181 |
# File 'lib/mayak/validations/rule.rb', line 177 def self.greater_than_or_equal_to(value) (greater_than(value) | equal_to(value)).error_from_value do |checked| "Value #{checked} should greater than or equal to #{value}" end end |
.less_than(value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mayak/validations/rule.rb', line 143 def self.less_than(value) Rule[T.any(Float, Integer), String].new do |checked| if checked < value ::Mayak::ValidationResult::Valid.new else ::Mayak::ValidationResult::Invalid.new( errors: ["Value #{checked} should be less than the #{value}"] ) end end end |
.less_than_or_equal_to(value) ⇒ Object
188 189 190 191 192 |
# File 'lib/mayak/validations/rule.rb', line 188 def self.less_than_or_equal_to(value) (less_than(value) | equal_to(value)).error_from_value do |checked| "Value #{checked} should less than or equal to #{value}" end end |
.negative(value) ⇒ Object
208 209 210 |
# File 'lib/mayak/validations/rule.rb', line 208 def self.negative(value) less_than(0) end |
.not_empty ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/mayak/validations/rule.rb', line 215 def self.not_empty Rule[T.any(String, Array, Hash, String), String].new do |checked| if checked.empty? ::Mayak::ValidationResult::Invalid.new( errors: ["Value #{checked} should not be empty equal"] ) else ::Mayak::ValidationResult::Valid.new end end end |
.not_nil(rule) ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/mayak/validations/rule.rb', line 232 def self.not_nil(rule) Rule[T.nilable(T.type_parameter(:Value)), T.type_parameter(:Error)].new do |checked| case checked when NilClass ::Mayak::ValidationResult::Invalid.new(errors: ["Should not be nil"]) else rule.check(checked) end end end |
.positive(value) ⇒ Object
199 200 201 |
# File 'lib/mayak/validations/rule.rb', line 199 def self.positive(value) greater_than(0) end |
.with_keys_aggregated(rule) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/mayak/validations/rule.rb', line 248 def self.with_keys_aggregated(rule) Rule[T.type_parameter(:Value), T::Hash[Symbol, T.type_parameter(:Error)]].new do |checked| result = rule.check(checked) case result when ::Mayak::ValidationResult::Valid ::Mayak::ValidationResult::Valid[T::Hash[Symbol, T.type_parameter(:Error)]].new when ::Mayak::ValidationResult::Invalid accumulated = result.errors.reduce({}) do |acc, tuple| key, error = tuple if acc.key?(key) acc[key] << error else acc[key] = [error] end end ::Mayak::ValidationResult::Invalid[T::Hash[Symbol, T.type_parameter(:Error)]].new( accumulated ) else T.absurd(result) end end end |
Instance Method Details
#any(another) ⇒ Object Also known as: |
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mayak/validations/rule.rb', line 60 def any(another) Rule[Value, Error].new do |checked| first_result = check(checked) case first_result when ::Mayak::ValidationResult::Valid another.check(checked) when ::Mayak::ValidationResult::Invalid first_result else T.absurd(first_result) end end end |
#both(another) ⇒ Object Also known as: &
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mayak/validations/rule.rb', line 79 def both(another) Rule[Value, Error].new do |checked| first_result = check(checked) case first_result when ::Mayak::ValidationResult::Valid another.check(checked) when ::Mayak::ValidationResult::Invalid second_result = another.check(checked) case second_result when ::Mayak::ValidationResult::Valid first_result when ::Mayak::ValidationResult::Invalid ::Mayak::ValidationResult::Invalid.new(errors: first_result.errors + second_result.errors) else T.absurd(first_result) end else T.absurd(first_result) end end end |
#check(value) ⇒ Object
24 25 26 |
# File 'lib/mayak/validations/rule.rb', line 24 def check(value) @blk.call(value) end |
#error(new_error) ⇒ Object
53 54 55 |
# File 'lib/mayak/validations/rule.rb', line 53 def error(new_error) error_from_value { |_| new_error } end |
#error_from_value(&blk) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mayak/validations/rule.rb', line 33 def error_from_value(&blk) Rule[Value, T.type_parameter(:NewError)].new do |checked| result = check(checked) case result when ::Mayak::ValidationResult::Valid ::Mayak::ValidationResult::Valid[T.type_parameter(:NewError)].new when ::Mayak::ValidationResult::Invalid ::Mayak::ValidationResult::Invalid[T.type_parameter(:NewError)].new(errors: [blk.call(checked)]) else T.absurd(result) end end end |
#with_key(key) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mayak/validations/rule.rb', line 104 def with_key(key) Rule[Value, [Symbol, Error]].new do |checked| result = check(checked) case result when ::Mayak::ValidationResult::Valid ::Mayak::ValidationResult::Valid[[Symbol, Error]].new when ::Mayak::ValidationResult::Invalid ::Mayak::ValidationResult::Invalid[[Symbol, Error]].new( errors: result.errors.map { |error| [key, error] } ) else T.absurd(result) end end end |