-
Notifications
You must be signed in to change notification settings - Fork 0
Validator
The validator is an helper class used from commands to normalize and validates parameters.
validation = Evnt::Validator.new('hello world', presence: true, type: :string)
puts validation.passed? # -> true
puts validation.value # -> 'my value'The presence option should check that the value is nil or not.
Examples:
Evnt::Validator.new(nil, presence: true).passed? # -> false
Evnt::Validator.new('', presence: true).passed? # -> true
Evnt::Validator.new('hello world', presence: true).passed? # -> true
Evnt::Validator.new(nil, presence: false).passed? # -> true
Evnt::Validator.new('', presence: false).passed? # -> false
Evnt::Validator.new('hello world', presence: false).passed? # -> falseThe type option should check that the value has a specific type or can be normalized to that type.
The default supported types are boolean, string, integer, symbol, float, hash, array, date, datetime, time.
Examples:
# Normal usage
Evnt::Validator.new(true, type: :boolean).passed? # -> true
Evnt::Validator.new('hello world', type: :string).passed? # -> true
Evnt::Validator.new(34, type: :integer).passed? # -> true
# Normalization usage
validation = Evnt::Validator.new(234, type: :string)
validation.passed? # -> true
validation.value # -> '234'
validation = Evnt::Validator.new('true', type: :boolean)
validation.passed? # -> true
validation.value # -> true
# Nil usage
validation = Evnt::Validator.new(nil, type: :string)
validation.passed? # -> true
validation.value # -> nilThat type option can be also used to theck that the value belongs to a custom class. In this case you should pass the type option value as a string.
Examples:
user = User.new
Evnt::Validator.new(user, type: 'User').passed? # -> trueCustom options are options that should be used only with specific value types.
Global options are used for all supported types.
Evnt::Validator.new(1, type: :integer, equal: 1).passed? # -> true
Evnt::Validator.new(1, type: :integer, equal: 2).passed? # -> falseEvnt::Validator.new(1, type: :integer, in: [1, 2, 3]).passed? # -> true
Evnt::Validator.new(1, type: :integer, in: [2, 3]).passed? # -> falseEvnt::Validator.new(1, type: :integer, out: [1, 2, 3]).passed? # -> false
Evnt::Validator.new(1, type: :integer, out: [2, 3]).passed? # -> trueString options are used for string type.
Evnt::Validator.new('', type: :string, blank: false).passed? # -> false
Evnt::Validator.new('hello world', type: :string, blank: false).passed? # -> true
Evnt::Validator.new('', type: :string, blank: true).passed? # -> true
Evnt::Validator.new('hello world', type: :string, blank: true).passed? # -> falseEvnt::Validator.new('a', type: :string, length: 1).passed? # -> true
Evnt::Validator.new('a', type: :string, length: 4).passed? # -> falseEvnt::Validator.new('a', type: :string, min_length: 2).passed? # -> false
Evnt::Validator.new('hello', type: :string, min_length: 2).passed? # -> trueEvnt::Validator.new('a', type: :string, max_length: 2).passed? # -> true
Evnt::Validator.new('hello', type: :string, max_length: 2).passed? # -> falseEvnt::Validator.new('Hello1', type: :string, regex: /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,20}$/).passed? # -> true
Evnt::Validator.new('hello', type: :string, regex: /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,20}$/).passed? # -> falseNumber options are used for integer and float types.
Evnt::Validator.new(3, type: :integer, min: 1).passed? # -> true
Evnt::Validator.new(3, type: :integer, min: 4).passed? # -> falseEvnt::Validator.new(3, type: :integer, max: 1).passed? # -> false
Evnt::Validator.new(3, type: :integer, max: 4).passed? # -> trueTime options are used for date, datetime and time types.
Evnt::Validator.new(Date.today, type: :date, min: Date.today - 1).passed? # -> true
Evnt::Validator.new(Date.today, type: :date, min: Date.today + 1).passed? # -> falseEvnt::Validator.new(Date.today, type: :date, max: Date.today + 1).passed? # -> true
Evnt::Validator.new(Date.today, type: :date, max: Date.today - 1).passed? # -> false