spec/compel/validation_spec.rb in compel-0.1.3 vs spec/compel/validation_spec.rb in compel-0.2.0
- old
+ new
@@ -3,39 +3,39 @@
context 'Param validation' do
context 'required' do
it 'should validate without errors' do
- errors = Compel::Validation.validate(123, { required: true })
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { required: true })
expect(errors.empty?).to eq(true)
end
it 'should validate with error' do
- errors = Compel::Validation.validate(nil, { required: true })
+ errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, { required: true })
expect(errors.empty?).to eq(false)
expect(errors).to eq(['is required'])
end
end
context 'length' do
it 'should validate without errors' do
- errors = Compel::Validation.validate(123, { length: 3 })
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { length: 3 })
expect(errors.empty?).to eq(true)
end
end
context 'in, within, range' do
def expect_be_in_within_range(range, value)
[:in, :within].each do |key|
- errors = Compel::Validation.validate(value, { key => range })
+ errors = Compel::Validation.validate(value, Compel::Coercion::String, { key => range })
yield errors
end
end
it 'should validate without errors' do
@@ -44,27 +44,141 @@
end
end
it 'should validate with errors' do
expect_be_in_within_range(['PT', 'UK'], 'US') do |errors|
- expect(errors).to eq(['must be within ["PT", "UK"]'])
+ expect(errors).to include('must be within ["PT", "UK"]')
end
end
context 'range' do
it 'should validate without errors' do
- errors = Compel::Validation.validate(2, range: (1..3))
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, range: (1..3))
expect(errors.empty?).to eq(true)
end
it 'should validate with errors' do
- errors = Compel::Validation.validate(4, range: (1..3))
+ errors = Compel::Validation.validate(4, Compel::Coercion::Integer, range: (1..3))
- expect(errors).to eq(['must be within 1..3'])
+ expect(errors).to include('must be within 1..3')
end
+ end
+
+ end
+
+ context 'format' do
+
+ it 'should validate with errors' do
+ format = /^abcd/
+ errors = Compel::Validation.validate('acb', Compel::Coercion::String, format: format)
+
+ expect(errors).to include("must match format ^abcd")
+ end
+
+ it 'should validate with errors 1' do
+ format = /^abcd/
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, format: format)
+
+ expect(errors).to include('must be a string if using the format validation')
+ end
+
+ it 'should validate with errors 2' do
+ format = /^abcd/
+ errors = Compel::Validation.validate(nil, Compel::Coercion::String, format: format)
+
+ expect(errors).to include('must be a string if using the format validation')
+ end
+
+ it 'should validate without errors' do
+ format = /abcd/
+ errors = Compel::Validation.validate('abcd', Compel::Coercion::String, format: format)
+ expect(errors.empty?).to eq(true)
+ end
+
+ end
+
+ context 'is' do
+
+ it 'should validate with errors' do
+ errors = Compel::Validation.validate('abcd', Compel::Coercion::Integer, is: 123)
+ expect(errors).to include('must be 123')
+ end
+
+ it 'should validate with errors 1' do
+ errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, is: 123)
+ expect(errors).to include('must be 123')
+ end
+
+ it 'should validate without errors' do
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, is: 123)
+ expect(errors.empty?).to eq(true)
+ end
+
+ end
+
+ context 'min' do
+
+ it 'should validate with errors' do
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, min: 3)
+
+ expect(errors).to include('cannot be less than 3')
+ end
+
+ end
+
+ context 'max' do
+
+ it 'should validate with errors' do
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, max: 2)
+
+ expect(errors).to include('cannot be greater than 2')
+ end
+
+ end
+
+ context 'min_length' do
+
+ it 'should validate with errors' do
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, min_length: 2)
+
+ expect(errors).to include('must be a string if using the min_length validation')
+ end
+
+ it 'should validate with errors 1' do
+ errors = Compel::Validation.validate('a', Compel::Coercion::String, min_length: 2)
+
+ expect(errors).to include('cannot have length less than 2')
+ end
+
+ it 'should validate without errors' do
+ errors = Compel::Validation.validate('ab', Compel::Coercion::String, min_length: 2)
+
+ expect(errors.empty?).to eq(true)
+ end
+
+ end
+
+ context 'max_length' do
+
+ it 'should validate with errors' do
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, max_length: 2)
+
+ expect(errors).to include('must be a string if using the max_length validation')
+ end
+
+ it 'should validate with errors 1' do
+ errors = Compel::Validation.validate('abcdef', Compel::Coercion::String, max_length: 5)
+
+ expect(errors).to include('cannot have length greater than 5')
+ end
+
+ it 'should validate without errors' do
+ errors = Compel::Validation.validate('abcde', Compel::Coercion::String, max_length: 5)
+
+ expect(errors.empty?).to eq(true)
end
end
end