Skip to content

Commit

Permalink
Create input_validator.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Sep 20, 2024
1 parent 05f5e29 commit c942cc0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions global_business/business/lib/input_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class InputValidator
def self.validate(input, rules)
errors = {}
rules.each do |field, rule|
value = input[field]
if value.nil? || value.empty?
errors[field] = "cannot be blank"
elsif rule[:type] == :string
if value.length < rule[:min_length] || value.length > rule[:max_length]
errors[field] = "must be between #{rule[:min_length]} and #{rule[:max_length]} characters"
end
elsif rule[:type] == :email
if !value.match?(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/)
errors[field] = "must be a valid email address"
end
elsif rule[:type] == :password
if value.length < rule[:min_length]
errors[field] = "must be at least #{rule[:min_length]} characters"
end
end
end
errors
end
end

0 comments on commit c942cc0

Please sign in to comment.