summaryrefslogtreecommitdiff
path: root/app/validators
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-12-01 18:53:44 -0500
committerRobert Speicher <rspeicher@gmail.com>2015-12-07 16:57:26 -0500
commite48391b813d3e5079238aa3f0662e7a46e1b4a54 (patch)
treebc7abec1eb4b27a16dcf47d09483b967bffdd3a8 /app/validators
parentb3200c8c44f2351d88d5d78d7ded3ac06001bd7c (diff)
downloadgitlab-ce-e48391b813d3e5079238aa3f0662e7a46e1b4a54.tar.gz
Add custom ColorValidator
Diffstat (limited to 'app/validators')
-rw-r--r--app/validators/color_validator.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/validators/color_validator.rb b/app/validators/color_validator.rb
new file mode 100644
index 00000000000..571d0007aa2
--- /dev/null
+++ b/app/validators/color_validator.rb
@@ -0,0 +1,20 @@
+# ColorValidator
+#
+# Custom validator for web color codes. It requires the leading hash symbol and
+# will accept RGB triplet or hexadecimal formats.
+#
+# Example:
+#
+# class User < ActiveRecord::Base
+# validates :background_color, allow_blank: true, color: true
+# end
+#
+class ColorValidator < ActiveModel::EachValidator
+ PATTERN = /\A\#[0-9A-Fa-f]{3}{1,2}+\Z/.freeze
+
+ def validate_each(record, attribute, value)
+ unless value =~ PATTERN
+ record.errors.add(attribute, "must be a valid color code")
+ end
+ end
+end