Skip to content

Commit 113c2d5

Browse files
authored
Merge pull request #281 from rubocop/custom-validation-method-naming
Add Custom Validation Methods naming guideline
2 parents bbcf3ac + d2a9a38 commit 113c2d5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,32 @@ validates_length_of :email, maximum: 100
552552
validates :email, presence: true, length: { maximum: 100 }
553553
----
554554

555+
=== Custom Validation Methods
556+
557+
When naming custom validation methods, adhere to the simple rules:
558+
559+
- `validate :method_name` reads like a natural statement
560+
- the method name explains what it checks
561+
- the method is recognizable as a validation method by its name, not a predicate method
562+
563+
[source,ruby]
564+
----
565+
# good
566+
validate :expiration_date_cannot_be_in_the_past
567+
validate :discount_cannot_be_greater_than_total_value
568+
validate :ensure_same_topic_is_chosen
569+
570+
# also good - explicit prefix
571+
validate :validate_birthday_in_past
572+
validate :validate_sufficient_quantity
573+
validate :must_have_owner_with_no_other_items
574+
validate :must_have_shipping_units
575+
576+
# bad
577+
validate :birthday_in_past
578+
validate :owner_has_no_other_items
579+
----
580+
555581
=== Single-attribute Validations [[single-attribute-validations]]
556582

557583
To make validations easy to read, don't list multiple attributes per validation.

0 commit comments

Comments
 (0)