/
validation.rb
72 lines (67 loc) · 1.82 KB
/
validation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true
module TTY
class Prompt
class Question
# A class representing question validation.
class Validation
# Available validator names
VALIDATORS = {
email: /^[a-z0-9._%+-]+@([a-z0-9-]+\.)+[a-z]{2,6}$/i
}.freeze
attr_reader :pattern
# Initialize a Validation
#
# @param [Object] pattern
#
# @return [undefined]
#
# @api private
def initialize(pattern)
@pattern = coerce(pattern)
end
# Convert validation into known type.
#
# @param [Object] pattern
#
# @raise [TTY::ValidationCoercion]
# raised when failed to convert validation
#
# @api private
def coerce(pattern)
case pattern
when String, Symbol, Proc
pattern
when Regexp
Regexp.new(pattern.to_s)
else
raise ValidationCoercion, "Wrong type, got #{pattern.class}"
end
end
# Test if the input passes the validation
#
# @example
# Validation.new(/pattern/)
# validation.call(input) # => true
#
# @param [Object] input
# the input to validate
#
# @return [Boolean]
#
# @api public
def call(input)
if pattern.is_a?(String) || pattern.is_a?(Symbol)
VALIDATORS.key?(pattern.to_sym)
!VALIDATORS[pattern.to_sym].match(input).nil?
elsif pattern.is_a?(Regexp)
!pattern.match(input).nil?
elsif pattern.is_a?(Proc)
result = pattern.call(input)
result.nil? ? false : result
else false
end
end
end # Validation
end # Question
end # Prompt
end # TTY