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

renaming validate_numeric args for clarity. #867

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 17 additions & 12 deletions spec/avram/validations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe Avram::Validations do
it "validates custom message for validate_numeric" do
too_small_attribute = attribute(1)

result = Avram::Validations.validate_numeric too_small_attribute, greater_than: 2, message: "number is too small"
result = Avram::Validations.validate_numeric too_small_attribute, at_least: 2, message: "number is too small"
result.should eq(false)
too_small_attribute.errors.should eq(["number is too small"])
end
Expand Down Expand Up @@ -343,52 +343,57 @@ describe Avram::Validations do
describe "validate_numeric" do
it "validates" do
too_small_attribute = attribute(1)
result = Avram::Validations.validate_numeric(too_small_attribute, greater_than: 2)
result = Avram::Validations.validate_numeric(too_small_attribute, at_least: 2)
result.should eq(false)
too_small_attribute.errors.should eq(["must be greater than 2"])
too_small_attribute.errors.should eq(["must be at least 2"])

too_large_attribute = attribute(38)
result = Avram::Validations.validate_numeric(too_large_attribute, less_than: 32)
result = Avram::Validations.validate_numeric(too_large_attribute, no_more_than: 32)
result.should eq(false)
too_large_attribute.errors.should eq(["must be less than 32"])
too_large_attribute.errors.should eq(["must be no more than 32"])

just_right_attribute = attribute(10)
result = Avram::Validations.validate_numeric(just_right_attribute, greater_than: 9, less_than: 11)
result = Avram::Validations.validate_numeric(just_right_attribute, at_least: 9, no_more_than: 11)
result.should eq(true)
just_right_attribute.valid?.should be_true

exactly = attribute(4)
result = Avram::Validations.validate_numeric(exactly, at_least: 4)
result.should eq(true)
exactly.valid?.should be_true
end

it "raises an error for an impossible condition" do
expect_raises(Avram::ImpossibleValidation) do
Avram::Validations.validate_numeric attribute(100), greater_than: 4, less_than: 1
Avram::Validations.validate_numeric attribute(100), at_least: 4, no_more_than: 1
end
end

it "can allow nil" do
just_nil = nil_attribute(Int32)
result = Avram::Validations.validate_numeric(just_nil, greater_than: 1, less_than: 2, allow_nil: true)
result = Avram::Validations.validate_numeric(just_nil, at_least: 1, no_more_than: 2, allow_nil: true)
result.should eq(true)
just_nil.valid?.should be_true

just_nil = nil_attribute(Int32)
result = Avram::Validations.validate_numeric(just_nil, greater_than: 1, less_than: 2)
result = Avram::Validations.validate_numeric(just_nil, at_least: 1, no_more_than: 2)
result.should eq(false)
just_nil.valid?.should be_false
end

it "handles different types of numbers" do
attribute = attribute(10.9)
result = Avram::Validations.validate_numeric(attribute, greater_than: 9, less_than: 11)
result = Avram::Validations.validate_numeric(attribute, at_least: 9, no_more_than: 11)
result.should eq(true)
attribute.valid?.should be_true

attribute = attribute(10)
result = Avram::Validations.validate_numeric(attribute, greater_than: 9.8, less_than: 10.9)
result = Avram::Validations.validate_numeric(attribute, at_least: 9.8, no_more_than: 10.9)
result.should eq(true)
attribute.valid?.should be_true

attribute = attribute(10_i64)
result = Avram::Validations.validate_numeric(attribute, greater_than: 9, less_than: 11)
result = Avram::Validations.validate_numeric(attribute, at_least: 9, no_more_than: 11)
result.should eq(true)
attribute.valid?.should be_true
end
Expand Down
4 changes: 2 additions & 2 deletions src/avram/i18n_backend.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ struct Avram::I18n < Avram::I18nBackend
validate_inclusion_of: "is not included in the list",
validate_max_size_of: "must not have more than %d characters",
validate_min_size_of: "must have at least %d characters",
validate_numeric_max: "must be less than %d",
validate_numeric_min: "must be greater than %d",
validate_numeric_max: "must be no more than %d",
validate_numeric_min: "must be at least %d",
validate_numeric_nil: "must not be nil",
validate_required: "is required",
validate_uniqueness_of: "is already taken",
Expand Down
22 changes: 11 additions & 11 deletions src/avram/validations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,25 @@ module Avram::Validations
no_errors
end

# Validate a number is `greater_than` and/or `less_than`
# Validate a number is `at_least` and/or `no_more_than`
#
# ```
# validate_numeric age, greater_than: 18
# validate_numeric count, greater_than: 0, less_than: 1200
# validate_numeric age, at_least: 18
# validate_numeric count, at_least: 0, no_more_than: 1200
# ```
# ameba:disable Metrics/CyclomaticComplexity
def validate_numeric(
attribute : Avram::Attribute(Number),
greater_than = nil,
less_than = nil,
at_least = nil,
no_more_than = nil,
message = nil,
allow_nil : Bool = false
) : Bool
no_errors = true
if greater_than && less_than && greater_than > less_than
if at_least && no_more_than && at_least > no_more_than
raise ImpossibleValidation.new(
attribute: attribute.name,
message: "number greater than #{greater_than} but less than #{less_than}")
message: "number at least #{at_least} but no more than #{no_more_than}")
end

number = attribute.value
Expand All @@ -272,16 +272,16 @@ module Avram::Validations
return no_errors
end

if greater_than && number < greater_than
if at_least && number < at_least
attribute.add_error(
(message || Avram.settings.i18n_backend.get(:validate_numeric_min)) % greater_than
(message || Avram.settings.i18n_backend.get(:validate_numeric_min)) % at_least
)
no_errors = false
end

if less_than && number > less_than
if no_more_than && number > no_more_than
attribute.add_error(
(message || Avram.settings.i18n_backend.get(:validate_numeric_max)) % less_than
(message || Avram.settings.i18n_backend.get(:validate_numeric_max)) % no_more_than
)
no_errors = false
end
Expand Down