Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend AM validate_length_of matcher to support array attributes #1560

Merged
Prev Previous commit
Next Next commit
Extending validate_length_of_spec for array column recognizition and …
…force validation as_array
jarenas9539 committed May 31, 2023

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 7fc8937cd8f1bf3b4f8fd73b22438ac1992bde46
Original file line number Diff line number Diff line change
@@ -375,28 +375,28 @@ def configure_validation_matcher(matcher)
context 'an attribute with a non-zero minimum length validation' do
it 'accepts ensuring the correct minimum length' do
expect(validating_array_length(minimum: 4)).
to validate_length_of(:attr).as_array.is_at_least(4)
to validate_length_of(:attr).is_at_least(4)
end

it 'rejects ensuring a lower minimum length with any message' do
expect(validating_array_length(minimum: 4)).
not_to validate_length_of(:attr).as_array.is_at_least(3).with_short_message(/.*/)
not_to validate_length_of(:attr).is_at_least(3).with_short_message(/.*/)
end

it 'rejects ensuring a higher minimum length with any message' do
expect(validating_array_length(minimum: 4)).
not_to validate_length_of(:attr).as_array.is_at_least(5).with_short_message(/.*/)
not_to validate_length_of(:attr).is_at_least(5).with_short_message(/.*/)
end

it 'does not override the default message with a blank' do
expect(validating_array_length(minimum: 4)).
to validate_length_of(:attr).as_array.is_at_least(4).with_short_message(nil)
to validate_length_of(:attr).is_at_least(4).with_short_message(nil)
end

it 'fails when used in the negative' do
assertion = lambda do
expect(validating_array_length(minimum: 4)).
not_to validate_length_of(:attr).as_array.is_at_least(4)
not_to validate_length_of(:attr).is_at_least(4)
end

message = <<-MESSAGE
@@ -413,50 +413,141 @@ def configure_validation_matcher(matcher)
context 'an attribute with a minimum length validation of 0' do
it 'accepts ensuring the correct minimum length' do
expect(validating_array_length(minimum: 0)).
to validate_length_of(:attr).as_array.is_at_least(0)
to validate_length_of(:attr).is_at_least(0)
end
end

context 'an attribute with a maximum length' do
it 'accepts ensuring the correct maximum length' do
expect(validating_array_length(maximum: 4)).
to validate_length_of(:attr).as_array.is_at_most(4)
to validate_length_of(:attr).is_at_most(4)
end

it 'rejects ensuring a lower maximum length with any message' do
expect(validating_array_length(maximum: 4)).
not_to validate_length_of(:attr).as_array.is_at_most(3).with_long_message(/.*/)
not_to validate_length_of(:attr).is_at_most(3).with_long_message(/.*/)
end

it 'rejects ensuring a higher maximum length with any message' do
expect(validating_array_length(maximum: 4)).
not_to validate_length_of(:attr).as_array.is_at_most(5).with_long_message(/.*/)
not_to validate_length_of(:attr).is_at_most(5).with_long_message(/.*/)
end

it 'does not override the default message with a blank' do
expect(validating_array_length(maximum: 4)).
to validate_length_of(:attr).as_array.is_at_most(4).with_long_message(nil)
to validate_length_of(:attr).is_at_most(4).with_long_message(nil)
end
end

context 'an attribute with a required exact length' do
it 'accepts ensuring the correct length' do
expect(validating_array_length(is: 4)).
to validate_length_of(:attr).as_array.is_equal_to(4)
to validate_length_of(:attr).is_equal_to(4)
end

it 'rejects ensuring a lower maximum length with any message' do
expect(validating_array_length(is: 4)).
not_to validate_length_of(:attr).as_array.is_equal_to(3).with_message(/.*/)
not_to validate_length_of(:attr).is_equal_to(3).with_message(/.*/)
end

it 'rejects ensuring a higher maximum length with any message' do
expect(validating_array_length(is: 4)).
not_to validate_length_of(:attr).as_array.is_equal_to(5).with_message(/.*/)
not_to validate_length_of(:attr).is_equal_to(5).with_message(/.*/)
end

it 'does not override the default message with a blank' do
expect(validating_array_length(is: 4)).
to validate_length_of(:attr).is_equal_to(4).with_message(nil)
end
end
end

context 'when column is validated as array' do
context 'an attribute with a non-zero minimum length validation' do
it 'accepts ensuring the correct minimum length' do
expect(validating_length(type: :jsonb, minimum: 4)).
to validate_length_of(:attr).as_array.is_at_least(4)
end

it 'rejects ensuring a lower minimum length with any message' do
expect(validating_length(type: :jsonb, minimum: 4)).
not_to validate_length_of(:attr).as_array.is_at_least(3).with_short_message(/.*/)
end

it 'rejects ensuring a higher minimum length with any message' do
expect(validating_length(type: :jsonb, minimum: 4)).
not_to validate_length_of(:attr).as_array.is_at_least(5).with_short_message(/.*/)
end

it 'does not override the default message with a blank' do
expect(validating_length(type: :jsonb, minimum: 4)).
to validate_length_of(:attr).as_array.is_at_least(4).with_short_message(nil)
end

it 'fails when used in the negative' do
assertion = lambda do
expect(validating_length(type: :jsonb, minimum: 4)).
not_to validate_length_of(:attr).as_array.is_at_least(4)
end

message = <<-MESSAGE
Expected Example not to validate that the length of :attr is at least 4,
but this could not be proved.
After setting :attr to ‹["x", "x", "x", "x"]›, the matcher expected
the Example to be invalid, but it was valid instead.
MESSAGE

expect(&assertion).to fail_with_message(message)
end
end

context 'an attribute with a minimum length validation of 0' do
it 'accepts ensuring the correct minimum length' do
expect(validating_length(type: :jsonb, minimum: 0)).
to validate_length_of(:attr).as_array.is_at_least(0)
end
end

context 'an attribute with a maximum length' do
it 'accepts ensuring the correct maximum length' do
expect(validating_length(type: :jsonb, maximum: 4)).
to validate_length_of(:attr).as_array.is_at_most(4)
end

it 'rejects ensuring a lower maximum length with any message' do
expect(validating_length(type: :jsonb, maximum: 4)).
not_to validate_length_of(:attr).as_array.is_at_most(3).with_long_message(/.*/)
end

it 'rejects ensuring a higher maximum length with any message' do
expect(validating_length(type: :jsonb, maximum: 4)).
not_to validate_length_of(:attr).as_array.is_at_most(5).with_long_message(/.*/)
end

it 'does not override the default message with a blank' do
expect(validating_length(type: :jsonb, maximum: 4)).
to validate_length_of(:attr).as_array.is_at_most(4).with_long_message(nil)
end
end

context 'an attribute with a required exact length' do
it 'accepts ensuring the correct length' do
expect(validating_length(type: :jsonb, is: 4)).
to validate_length_of(:attr).as_array.is_equal_to(4)
end

it 'rejects ensuring a lower maximum length with any message' do
expect(validating_length(type: :jsonb, is: 4)).
not_to validate_length_of(:attr).as_array.is_equal_to(3).with_message(/.*/)
end

it 'rejects ensuring a higher maximum length with any message' do
expect(validating_length(type: :jsonb, is: 4)).
not_to validate_length_of(:attr).as_array.is_equal_to(5).with_message(/.*/)
end

it 'does not override the default message with a blank' do
expect(validating_length(type: :jsonb, is: 4)).
to validate_length_of(:attr).as_array.is_equal_to(4).with_message(nil)
end
end
@@ -466,9 +557,10 @@ def configure_validation_matcher(matcher)
def define_model_validating_length(options = {})
options = options.dup
attribute_name = options.delete(:attribute_name) { :attr }
type = options.delete(:type) || :varchar
array = options.delete(:array)

define_model(:example, attribute_name => { type: :varchar, options: { array: array } }) do |model|
define_model(:example, attribute_name => { type: type, options: { array: array } }) do |model|
model.validates_length_of(attribute_name, options)
end
end