diff --git a/spec/avram/validations_spec.cr b/spec/avram/validations_spec.cr index 159504275..0baedb80c 100644 --- a/spec/avram/validations_spec.cr +++ b/spec/avram/validations_spec.cr @@ -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 @@ -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 diff --git a/src/avram/i18n_backend.cr b/src/avram/i18n_backend.cr index 4db8caad2..4246e1aa1 100644 --- a/src/avram/i18n_backend.cr +++ b/src/avram/i18n_backend.cr @@ -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", diff --git a/src/avram/validations.cr b/src/avram/validations.cr index 5f22ca020..3cd6b4693 100644 --- a/src/avram/validations.cr +++ b/src/avram/validations.cr @@ -239,25 +239,37 @@ module Avram::Validations no_errors end - # Validate a number is `greater_than` and/or `less_than` + @[Deprecated("Use validate_numeric with at_least/no_more_than instead of greater_than/less_than")] + def validate_numeric( + attribute : Avram::Attribute(Number), + greater_than = nil, + less_than = nil, + message = nil, + allow_nil : Bool = false + ) : Bool + validate_numeric(attribute, at_least: greater_than, no_more_than: less_than, message: message, allow_nil: allow_nil) + end + + # 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 @@ -272,16 +284,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