Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password Rule and Locale #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mini_defender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require_relative 'mini_defender/extensions/hash'

module MiniDefender
I18n.load_path += Dir[File.join(__dir__, 'mini_defender', 'locales', '*.yml')]
end

# Register Library Rules
Expand Down
30 changes: 30 additions & 0 deletions lib/mini_defender/locales/ar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ar:
mini_defender:
email:
invalid: "البريد الإلكتروني غير صالح"

filled: "يجب ألا يكون %{field} فارغًا"
required: "%{field} مطلوب"
accepted: "يجب قبول %{attribute}."
alpha: "يجب أن يحتوي %{attribute} على الأحرف فقط."
alpha_dash: "يجب أن يحتوي %{attribute} على الأحرف والشرطات والشرطات السفلية فقط."
alpha_num: "يجب أن يحتوي %{attribute} على الأحرف والأرقام فقط."
array: "يجب أن يكون %{attribute} مصفوفة."
between: "يجب أن يكون %{attribute} بين %{min} و %{max}."
boolean: "يجب أن يكون %{attribute} نعم، لا، 0 أو 1."
confirmed:
found: "لا يتطابق تأكيد %{attribute}."
not_found: "يجب تأكيد %{attribute}."
country_code: "يجب أن يكون %{attribute} رمز دولة صالح حسب ISO3166."
credit_card: "يجب أن يكون %{attribute} رقم بطاقة ائتمان/خصم صحيح."
date: "يجب أن يكون %{attribute} تاريخًا صالحًا."
date_format: "يجب أن يكون %{attribute} في الصيغة %{format}."
date_eq: "يجب أن يكون %{attribute} مساويًا لـ %{target_date}."
date_gt: "يجب أن يكون %{attribute} أكبر من %{target_date}."
date_gte: "يجب أن يكون %{attribute} أكبر من أو يساوي %{target_date}."
date_lt: "يجب أن يكون %{attribute} أقل من %{target_date}."
date_lte: "يجب أن يكون %{attribute} أقل من أو يساوي %{target_date}."
declined: "يجب رفض %{attribute}."
numeric: "يجب أن يكون %{attribute} رقمًا."
gt: "يجب أن يكون %{attribute} أكبر من %{value}."
gte: "يجب أن يكون %{attribute} أكبر من أو يساوي %{value}."
37 changes: 37 additions & 0 deletions lib/mini_defender/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
en:
mini_defender:
password:
special_characters: "Password must contain at least one special character"
upper_case: "Password must contain at least one upper case character"
lower_case: "Password must contain at least one lower case character"
digit: "Password must contain at least one digit"
length: "Password must be at least 8 characters long"

email:
invalid: "Email is invalid"

filled: "%{field} must not be empty"
required: "%{field} is required"
accepted: "%{attribute} Must be accepted."
alpha: "%{attribute} Must only contain letters."
alpha_dash: "%{attribute} Must only contain letters, dashes and underscores."
alpha_num: "%{attribute} Must only contain letters and numbers."
array: "%{attribute} Must be an array."
between: "%{attribute} Must be between %{min} and %{max}."
boolean: "%{attribute} must be a boolean, yes, no, on, off, 0 or 1."
confirmed:
found: "%{attribute} confirmation does not match."
not_found: "%{attribute} must be confirmed."
country_code: "%{attribute} must be a valid ISO3166 country code."
credit_card: "%{attribute} must be a proper credit/debit card number."
date: "%{attribute} must be a valid date."
date_format: "%{attribute} must be in the format %{format}."
date_eq: "%{attribute} must be equal to %{target_date}."
date_gt: "%{attribute} must be greater than %{target_date}."
date_gte: "%{attribute} must be greater than or equal to %{target_date}."
date_lt: "%{attribute} must be less than %{target_date}."
date_lte: "%{attribute} must be less than or equal to %{target_date}."
declined: "%{attribute} must be declined."
numeric: "%{attribute} must be a number."
gt: "%{attribute} must be greater than %{value}."
gte: "%{attribute} must be greater than or equal to %{value}."
5 changes: 3 additions & 2 deletions lib/mini_defender/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def passes?(attribute, value, validator)
# @param [Object] attribute
# @param [Object] value
# @param [MiniDefender::Validator] validator
def message(attribute, value, validator)
def message(attribute, value, validator, translation = nil)
raise NotImplementedError, 'Use a concrete implementation.'
end

Expand All @@ -83,7 +83,8 @@ def with_message(message)
# @param [Object] attribute
# @param [Object] value
# @param [MiniDefender::Validator] validator
def error_message(attribute, value, validator)
def error_message(attribute, value, validator, translation)
attribute = translation || attribute.humanize
@message || message(attribute, value, validator)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/accepted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"Must be accepted."
I18n.t('mini_defender.accepted', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/alpha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The field must only contain alphabetical characters.'
I18n.t('mini_defender.alpha', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/alpha_dash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The field must only contain alphabetical characters, dashes and underscores.'
I18n.t('mini_defender.alpha_dash', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/alpha_num.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The field must only contain alpha-num characters.'
I18n.t('mini_defender.alpha_num', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"The field must be an array."
I18n.t('mini_defender.array', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/between.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"The value length must be between #{@min} and #{@max}."
I18n.t('mini_defender.between', attribute: attribute.humanize, min: @min, max: @max)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The value must be a boolean, yes, no, on, off, 0 or 1.'
I18n.t('mini_defender.boolean', attribute: attribute.humanize)
end
end
4 changes: 2 additions & 2 deletions lib/mini_defender/rules/confirmed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def passes?(attribute, value, validator)

def message(attribute, value, validator)
if @found
'The value confirmation does not match.'
I18n.t('mini_defender.confirmed.found', attribute: attribute.humanize)
else
'The value must be confirmed.'
I18n.t('mini_defender.confirmed.not_found', attribute: attribute.humanize)
end
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/country_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The value must be a valid ISO3166 country code.'
I18n.t('mini_defender.country_code', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The value must be a proper credit/debit card number.'
I18n.t('mini_defender.credit_card', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The given value is not a valid date.'
I18n.t('mini_defender.date', attribute: attribute.humanize)
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/mini_defender/rules/date_eq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
return "The given value is not a valid date." unless @valid_value
return I18n.t('mini_defender.date') unless @valid_value

"The value must be equal to #{@target_date}."
I18n.t('mini_defender.date_eq', attribute: attribute.humanize, target_date: @target_date)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/date_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The given value is not a valid date.'
I18n.t('mini_defender.date_format', attribute: attribute.humanize, format: @format)
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/mini_defender/rules/date_gt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
return "The given value is not a valid date." unless @valid_value
return I18n.t('mini_defender.date') unless @valid_value

"The value must be greater than #{@target_date}."
I18n.t('mini_defender.date_gt', attribute: attribute.humanize, target_date: @target_date)
end
end
4 changes: 2 additions & 2 deletions lib/mini_defender/rules/date_gte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
return "The given value is not a valid date." unless @valid_value
return I18n.t('mini_defender.date') unless @valid_value

"The value must be greater than or equal to #{@target_date}."
I18n.t('mini_defender.date_gte', attribute: attribute.humanize, target_date: @target_date)
end
end
4 changes: 2 additions & 2 deletions lib/mini_defender/rules/date_lt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
return "The given value is not a valid date." unless @valid_value
return I18n.t('mini_defender.date') unless @valid_value

"The value must be less than #{@target_date}."
I18n.t('mini_defender.date_lt', attribute: attribute.humanize, target_date: @target_date)
end
end
4 changes: 2 additions & 2 deletions lib/mini_defender/rules/date_lte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
return "The given value is not a valid date." unless @valid_value
return I18n.t('mini_defender.date') unless @valid_value

"The value must be less than or equal to #{@target_date}."
I18n.t('mini_defender.date_lte', attribute: attribute.humanize, target_date: @target_date)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/declined.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"Must be declined."
I18n.t('mini_defender.declined', attribute: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"The value should be a valid email address."
I18n.t('mini_defender.email.invalid')
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/filled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
'The field should not be empty.'
I18n.t('mini_defender.filled', field: attribute.humanize)
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/greater_than.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def message(attribute, value, validator)
when Numeric
"The value must be greater than #{@size}."
else
"The value length must be greater than #{@size}."
I18n.t('mini_defender.gt', attribute: attribute, value: @size)
end
end
end
28 changes: 14 additions & 14 deletions lib/mini_defender/rules/greater_than_or_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ def self.signature

def passes?(attribute, value, validator)
case value
when String, Array, Hash
value.length >= @size
when ActionDispatch::Http::UploadedFile
value.size >= @size
when Numeric
value >= @size
else
false
when String, Array, Hash
value.length >= @size
when ActionDispatch::Http::UploadedFile
value.size >= @size
when Numeric
value >= @size
else
false
end
end

def message(attribute, value, validator)
case value
when ActionDispatch::Http::UploadedFile
"The file size must be greater than or equal to #{@size} bytes."
when Numeric
"The value must be greater than or equal to #{@size}."
else
"The value length must be greater than or equal to #{@size}."
when ActionDispatch::Http::UploadedFile
"The file size must be greater than or equal to #{@size} bytes."
when Numeric
"The value must be greater than or equal to #{@size}."
else
I18n.t('mini_defender.gte', attribute: attribute, value: @size)
end
end
end
2 changes: 1 addition & 1 deletion lib/mini_defender/rules/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"The field must contain a numeric value."
I18n.t('mini_defender.numeric', attribute: attribute)
end
end
24 changes: 24 additions & 0 deletions lib/mini_defender/rules/password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class MiniDefender::Rules::Password < MiniDefender::Rule
def self.signature
'password'
end

def passes?(attribute, value, validator)
@errors = []

# Password requirements
@errors << I18n.t('mini_defender.password.upper_case') unless /[A-Z]/.match?(value)
@errors << I18n.t('mini_defender.password.lower_case') unless /[a-z]/.match?(value)
@errors << I18n.t('mini_defender.password.digit') unless /\d/.match?(value)
@errors << I18n.t('mini_defender.password.special_characters') unless /[#?!@$%^&*-]+/.match?(value)
@errors << I18n.t('mini_defender.password.length', min: 8) unless value.length >= 8

@errors.empty?
end

def message(attribute, value, validator)
@errors.join(', ')
end
end
3 changes: 2 additions & 1 deletion lib/mini_defender/rules/required.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def implicit?(validator)
end

def passes?(attribute, value, validator)
p value
case value
when nil
false
Expand All @@ -27,6 +28,6 @@ def passes?(attribute, value, validator)
end

def message(attribute, value, validator)
"This field is required."
I18n.t('mini_defender.required', field: attribute.humanize)
end
end
4 changes: 2 additions & 2 deletions lib/mini_defender/validates_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module MiniDefender::ValidatesInput
extend ActiveSupport::Concern

def validate!(rules, coerced = false)
def validate!(rules, coerced = false, translations: nil)
data = cleanse_data(params.to_unsafe_hash.deep_stringify_keys)
validator = MiniDefender::Validator.new(rules, data)
validator = MiniDefender::Validator.new(rules, data, translations)
validator.validate!
coerced ? validator.coerced : validator.data
end
Expand Down
Loading