Sha256: c79663d7cfb74712efdfba6e35bdca6170b51fa46a6492f9c642aecd4e53d219

Contents?: true

Size: 1.92 KB

Versions: 25

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "spec_helper"

describe Mongoid::Validatable::FormatValidator do

  describe "#validate_each" do

    let(:product) do
      Product.new
    end

    context "when the field is not localized" do

      let(:validator) do
        described_class.new(attributes: [:brand_name], with: /\A[a-z]*\z/i)
      end

      context "when the value is valid" do

        before do
          validator.validate_each(product, :brand_name, "Apple")
        end

        it "adds no errors" do
          expect(product.errors[:brand_name]).to be_empty
        end
      end

      context "when the value is invalid" do

        before do
          validator.validate_each(product, :brand_name, "123")
        end

        it "adds errors" do
          expect(product.errors[:brand_name]).to eq(["is invalid"])
        end
      end
    end

    context "when the field is localized" do

      let(:validator) do
        described_class.new(attributes: [:website], with: URI.regexp)
      end

      context "when the localized value is valid" do

        before do
          validator.validate_each(product, :website, { "en" => "http://www.apple.com" })
        end

        it "adds no errors" do
          expect(product.errors[:website]).to be_empty
        end
      end

      context "when one of the localized values is invalid" do

        before do
          validator.validate_each(
            product,
            :website, { "en" => "http://www.apple.com", "fr" => "not_a_website" }
          )
        end

        it "adds errors" do
          expect(product.errors[:website]).to eq(["is invalid"])
        end
      end

      context "when the localized value is invalid" do

        before do
          validator.validate_each(product, :website, { "en" => "not_a_website" })
        end

        it "adds errors" do
          expect(product.errors[:website]).to eq(["is invalid"])
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
mongoid-8.1.8 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.7 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.6 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.8 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.5 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.4 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.7 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.3 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.2 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.6 spec/mongoid/validatable/format_spec.rb
mongoid-7.5.4 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.1 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.5 spec/mongoid/validatable/format_spec.rb
mongoid-8.1.0 spec/mongoid/validatable/format_spec.rb
mongoid-7.5.3 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.4 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.3 spec/mongoid/validatable/format_spec.rb
mongoid-7.5.2 spec/mongoid/validatable/format_spec.rb
mongoid-8.0.2 spec/mongoid/validatable/format_spec.rb
mongoid-7.5.1 spec/mongoid/validatable/format_spec.rb