Validate length of some product fields

We know if the values are too long, so let's provide a useful message rather than generating an unhandled database error.
This code seems rather repetetive, it would be good to use a shared module. I wonder if there's a gem for that.

Note that the existing /products/*/edit screen doesn't even handle validation errors yet, but that's something for another day..
This commit is contained in:
David Cook
2023-09-20 14:31:45 +10:00
committed by Rachel Arnould
parent fee126d6e1
commit 3ec6386e1c
2 changed files with 6 additions and 1 deletions

View File

@@ -54,7 +54,8 @@ module Spree
has_many :variant_images, -> { order(:position) }, source: :images,
through: :variants
validates :name, presence: true
validates :name, presence: true, length: { maximum: columns_hash['name'].limit }
validates :sku, length: { maximum: columns_hash['sku'].limit }
validates :variant_unit, presence: true
validates :unit_value, numericality: {

View File

@@ -163,6 +163,10 @@ module Spree
expect(build(:product)).to be_valid
end
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_length_of(:name).is_at_most(255) }
it { is_expected.to validate_length_of(:sku).is_at_most(255) }
it "requires a primary taxon" do
expect(build(:simple_product, primary_taxon: nil)).not_to be_valid
end