Module: Clack::Validators
- Defined in:
- lib/clack/validators.rb
Overview
Built-in validators for common validation patterns. Use these with the ‘validate:` option on prompts.
Validation procs can perform any operation including slow I/O (database lookups, API calls, etc.) - they simply block until complete.
Class Method Summary collapse
-
.as_warning(validator) ⇒ Proc
Convert any validator to return a warning instead of an error.
-
.combine(*validators) ⇒ Proc
Combines multiple validators.
-
.date_range(min:, max:, message: nil) ⇒ Proc
Validates that the date is within a given range.
-
.directory_exists(message = "Directory does not exist") ⇒ Proc
Validates directory path exists.
-
.email(message = "Must be a valid email address") ⇒ Proc
Common email format validator.
-
.file_exists_warning(message = "File already exists. Overwrite?") ⇒ Proc
Warning if file exists.
-
.format(pattern, message = "Invalid format") ⇒ Proc
Validates that input matches a regular expression.
-
.future_date(message = "Date must be in the future") ⇒ Proc
Validates that the date is strictly after today.
-
.in_range(range, message = nil) ⇒ Proc
Validates that input is within a numeric range.
-
.integer(message = "Must be a number") ⇒ Proc
Validates that input is a valid integer.
-
.max_length(length, message = nil) ⇒ Proc
Validates maximum length.
-
.min_length(length, message = nil) ⇒ Proc
Validates minimum length.
-
.one_of(allowed, message = nil) ⇒ Proc
Validates that input is in a list of allowed values.
-
.past_date(message = "Date must be in the past") ⇒ Proc
Validates that the date is strictly before today.
-
.path_exists(message = "Path does not exist") ⇒ Proc
Validates file path exists.
-
.required(message = "This field is required") ⇒ Proc
Validates that the input is not empty.
-
.url(message = "Must be a valid URL") ⇒ Proc
Common URL format validator.
Class Method Details
.as_warning(validator) ⇒ Proc
Convert any validator to return a warning instead of an error. Warnings allow the user to proceed with confirmation.
199 200 201 202 203 204 205 206 |
# File 'lib/clack/validators.rb', line 199 def as_warning(validator) lambda do |value| result = validator.call(value) next if result.nil? result.is_a?(Clack::Warning) ? result : Clack::Warning.new(result) end end |
.combine(*validators) ⇒ Proc
Combines multiple validators. Returns the first error message, or nil if all pass.
109 110 111 |
# File 'lib/clack/validators.rb', line 109 def combine(*validators) ->(value) { first_failing_validation(validators, value) } end |
.date_range(min:, max:, message: nil) ⇒ Proc
Validates that the date is within a given range.
169 170 171 172 |
# File 'lib/clack/validators.rb', line 169 def date_range(min:, max:, message: nil) msg = || "Date must be between #{min} and #{max}" ->(date) { msg unless (min..max).cover?(date) } end |
.directory_exists(message = "Directory does not exist") ⇒ Proc
Validates directory path exists.
141 142 143 |
# File 'lib/clack/validators.rb', line 141 def directory_exists( = "Directory does not exist") ->(value) { unless File.directory?(value.to_s) } end |
.email(message = "Must be a valid email address") ⇒ Proc
Common email format validator.
117 118 119 |
# File 'lib/clack/validators.rb', line 117 def email( = "Must be a valid email address") format(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/, ) end |
.file_exists_warning(message = "File already exists. Overwrite?") ⇒ Proc
Warning if file exists. Allows user to confirm overwrite.
181 182 183 |
# File 'lib/clack/validators.rb', line 181 def file_exists_warning( = "File already exists. Overwrite?") ->(value) { Clack::Warning.new() if File.exist?(value.to_s) } end |
.format(pattern, message = "Invalid format") ⇒ Proc
Validates that input matches a regular expression.
69 70 71 |
# File 'lib/clack/validators.rb', line 69 def format(pattern, = "Invalid format") ->(value) { unless pattern.match?(value.to_s) } end |
.future_date(message = "Date must be in the future") ⇒ Proc
Validates that the date is strictly after today. Today itself is not considered “future” and will fail validation.
150 151 152 |
# File 'lib/clack/validators.rb', line 150 def future_date( = "Date must be in the future") ->(date) { if date <= Date.today } end |
.in_range(range, message = nil) ⇒ Proc
Validates that input is within a numeric range. Note: Parses value as integer for comparison.
97 98 99 100 101 102 103 |
# File 'lib/clack/validators.rb', line 97 def in_range(range, = nil) msg = || "Must be between #{range.first} and #{range.last}" lambda do |value| int_val = value.to_s.to_i msg unless range.cover?(int_val) && value.to_s.match?(/\A-?\d+\z/) end end |
.integer(message = "Must be a number") ⇒ Proc
Validates that input is a valid integer.
87 88 89 |
# File 'lib/clack/validators.rb', line 87 def integer( = "Must be a number") ->(value) { unless value.to_s.match?(/\A-?\d+\z/) } end |
.max_length(length, message = nil) ⇒ Proc
Validates maximum length.
59 60 61 62 |
# File 'lib/clack/validators.rb', line 59 def max_length(length, = nil) msg = || "Must be at most #{length} characters" ->(value) { msg if value.to_s.length > length } end |
.min_length(length, message = nil) ⇒ Proc
Validates minimum length.
49 50 51 52 |
# File 'lib/clack/validators.rb', line 49 def min_length(length, = nil) msg = || "Must be at least #{length} characters" ->(value) { msg if value.to_s.length < length } end |
.one_of(allowed, message = nil) ⇒ Proc
Validates that input is in a list of allowed values.
78 79 80 81 |
# File 'lib/clack/validators.rb', line 78 def one_of(allowed, = nil) msg = || "Must be one of: #{allowed.join(", ")}" ->(value) { msg unless allowed.include?(value) } end |
.past_date(message = "Date must be in the past") ⇒ Proc
Validates that the date is strictly before today. Today itself is not considered “past” and will fail validation.
159 160 161 |
# File 'lib/clack/validators.rb', line 159 def past_date( = "Date must be in the past") ->(date) { if date >= Date.today } end |
.path_exists(message = "Path does not exist") ⇒ Proc
Validates file path exists.
133 134 135 |
# File 'lib/clack/validators.rb', line 133 def path_exists( = "Path does not exist") ->(value) { unless File.exist?(value.to_s) } end |
.required(message = "This field is required") ⇒ Proc
Validates that the input is not empty.
40 41 42 |
# File 'lib/clack/validators.rb', line 40 def required( = "This field is required") ->(value) { if value.to_s.strip.empty? } end |
.url(message = "Must be a valid URL") ⇒ Proc
Common URL format validator.
125 126 127 |
# File 'lib/clack/validators.rb', line 125 def url( = "Must be a valid URL") format(%r{\Ahttps?://\S+\z}, ) end |