Class: AmazonFnskuValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.valid?(fnsku) ⇒ Boolean

Checks whether a value is a valid FNSKU.

Parameters:

  • fnsku (String, nil)

    the value to check

Returns:

  • (Boolean)

    true when the value matches the FNSKU format



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.

Parameters:

  • record (ActiveModel::Model)

    the record being validated

  • attribute (Symbol)

    the attribute name

  • value (String, nil)

    the value to validate



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? && options[:allow_nil]

  return if self.class.valid?(value)

  record.errors.add(attribute, options[:message] || MESSAGE)
end