Sha256: 33f954b2622fb68d8ae3b861bf6e9aa4b5a84f902da33689ce54f147be6a9d23

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Dsu
  module Validators
    class DescriptionValidator < ActiveModel::Validator
      def validate(record)
        description = record.description

        if description.blank?
          record.errors.add(:description, :blank)
          return
        end

        unless description.is_a?(String)
          record.errors.add(field, 'is the wrong object type. ' \
                                   "\"String\" was expected, but \"#{description.class}\" was received.")
          return
        end

        validate_description record
      end

      private

      def validate_description(record)
        description = record.description

        return if description.length.between?(2, 256)

        if description.length < 2
          record.errors.add(:description, "is too short: \"#{record.short_description}\" (minimum is 2 characters).")
        elsif description.length > 256
          record.errors.add(:description, "is too long: \"#{record.short_description}\" (maximum is 256 characters).")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dsu-1.1.1 lib/dsu/validators/description_validator.rb
dsu-1.1.0.alpha.2 lib/dsu/validators/description_validator.rb
dsu-1.1.0.alpha.1 lib/dsu/validators/description_validator.rb
dsu-1.0.0 lib/dsu/validators/description_validator.rb