Class: AmazonFnskuValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- AmazonFnskuValidator
- Defined in:
- app/validators/amazon_fnsku_validator.rb
Overview
Validates that an Amazon FNSKU (Fulfillment Network Stock Keeping Unit)
starts with 'X' followed by 9 alphanumeric characters.
Constant Summary collapse
- FNSKU_REGEX =
Regex pattern matching fnsku.
/\AX[A-Z0-9]{9}\z/- MESSAGE =
Message.
"must start with 'X' and be followed by 9 alphanumeric characters"
Class Method Summary collapse
-
.valid?(fnsku) ⇒ Boolean
Checks whether a value is a valid FNSKU.
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ void
Validates a single attribute value against the FNSKU format.
Class Method Details
.valid?(fnsku) ⇒ Boolean
Checks whether a value is a valid FNSKU.
29 30 31 |
# File 'app/validators/amazon_fnsku_validator.rb', line 29 def self.valid?(fnsku) fnsku.present? && fnsku.match?(FNSKU_REGEX) end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ void
This method returns an undefined value.
Validates a single attribute value against the FNSKU format.
17 18 19 20 21 22 23 |
# File 'app/validators/amazon_fnsku_validator.rb', line 17 def validate_each(record, attribute, value) return if value.blank? && [:allow_nil] return if self.class.valid?(value) record.errors.add(attribute, [:message] || MESSAGE) end |