Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
:required => true now uses the not_null validator (not presence)
Fixes #110
- Loading branch information
Showing
8 changed files
with
109 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module NoBrainer::Document::Validation::NotNull | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def validates_not_null(*attr_names) | ||
validates_with(NotNullValidator, _merge_attributes(attr_names)) | ||
end | ||
end | ||
|
||
class NotNullValidator < ActiveModel::EachValidator | ||
def validate_each(doc, attr, value) | ||
doc.errors.add(attr, :undefined, options) if value.nil? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ en: | |
messages: | ||
taken: "is already taken" | ||
invalid_type: "should be a %{type}" | ||
undefined: "must be defined" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'spec_helper' | ||
|
||
describe 'definition validator' do | ||
before { load_simple_document } | ||
|
||
shared_examples_for "not_null validation" do | ||
it 'cannot save without setting a value' do | ||
doc = SimpleDocument.new | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
doc.errors.full_messages.first.should == 'Field1 must be defined' | ||
end | ||
|
||
it 'cannot save a nil value' do | ||
doc = SimpleDocument.new(:field1 => nil) | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
end | ||
|
||
it 'can save an empty string' do | ||
doc = SimpleDocument.new(:field1 => '') | ||
doc.valid?.should == true | ||
end | ||
|
||
it 'can save a false value' do | ||
doc = SimpleDocument.new(:field1 => false) | ||
doc.valid?.should == true | ||
end | ||
|
||
it 'can save an empty array' do | ||
doc = SimpleDocument.new(:field1 => []) | ||
doc.valid?.should == true | ||
end | ||
end | ||
|
||
shared_examples_for "presence validation" do | ||
it 'cannot save without setting a value' do | ||
doc = SimpleDocument.new | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
doc.errors.full_messages.first.should == "Field1 can't be blank" | ||
end | ||
|
||
it 'cannot save a nil value' do | ||
doc = SimpleDocument.new(:field1 => nil) | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
end | ||
|
||
it 'cannot save an empty string' do | ||
doc = SimpleDocument.new(:field1 => '') | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
end | ||
|
||
it 'cannot save a false value' do | ||
doc = SimpleDocument.new(:field1 => false) | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
end | ||
|
||
it 'cannot save an empty array' do | ||
doc = SimpleDocument.new(:field1 => []) | ||
doc.valid?.should == false | ||
expect { doc.save }.to raise_error(NoBrainer::Error::DocumentInvalid) | ||
end | ||
end | ||
|
||
context 'with validates_not_null' do | ||
before { SimpleDocument.validates_not_null :field1 } | ||
it_behaves_like "not_null validation" | ||
end | ||
|
||
context 'with validates_presence_of' do | ||
before { SimpleDocument.validates_presence_of :field1 } | ||
it_behaves_like "presence validation" | ||
end | ||
|
||
context 'with :required => true' do | ||
before { SimpleDocument.field :field1, :required => :not_null } | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
spec/integration/uniqueness_spec.rb → ...integration/validation/uniqueness_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters