Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: added validation for custom and additional attribute (#4260)
- Loading branch information
1 parent
14c53b8
commit 60a0709
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class JsonbAttributesLengthValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
return if value.empty? | ||
|
||
@attribute = attribute | ||
@record = record | ||
|
||
value.each do |key, attribute_value| | ||
validate_keys(key, attribute_value) | ||
end | ||
end | ||
|
||
def validate_keys(key, attribute_value) | ||
case attribute_value.class.name | ||
when 'String' | ||
@record.errors.add @attribute, "#{key} length should be < 1500" if attribute_value.length > 1500 | ||
when 'Integer' | ||
@record.errors.add @attribute, "#{key} value should be < 9999999999" if attribute_value > 9_999_999_999 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters