Skip to content

Commit

Permalink
Merge 407100c into 66775ff
Browse files Browse the repository at this point in the history
  • Loading branch information
blanquer committed Dec 17, 2015
2 parents 66775ff + 407100c commit 955a9ca
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/attributor/hash_dsl_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def of( *args)
self
end

def validate( object,context=Attributor::DEFAULT_ROOT_CONTEXT,_attribute)
def validate( object,context=Attributor::DEFAULT_ROOT_CONTEXT,_attribute=nil)
result = []
case type
when :all
Expand Down
9 changes: 8 additions & 1 deletion lib/attributor/types/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)

context = [context] if context.is_a? ::String

self.class.attributes.each_with_object(Array.new) do |(sub_attribute_name, sub_attribute), errors|
ret = self.class.attributes.each_with_object(Array.new) do |(sub_attribute_name, sub_attribute), errors|
sub_context = self.class.generate_subcontext(context,sub_attribute_name)

value = self.__send__(sub_attribute_name)
Expand All @@ -141,6 +141,13 @@ def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)

errors.push *sub_attribute.validate(value, sub_context)
end
unless self.class.requirements.empty?
self.class.requirements.each_with_object(ret) do |req, errors|
validation_errors = req.validate( @contents , context)
errors.push *validation_errors unless validation_errors.empty?
end
end
ret
ensure
@validating = false
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Address < Attributor::Model
attribute :name, String, example: /\w+/
attribute :state, String, values: %w{OR CA}
attribute :person, Person, example: proc { |address, context| Person.example(context, address: address) }
requires :name
end
end

Expand Down
13 changes: 9 additions & 4 deletions spec/types/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
context 'loading with default values' do
let(:reference) { Post }
let(:options) { {reference: reference} }

let(:attribute_definition) do
proc do
attribute :title
Expand All @@ -255,10 +255,10 @@
end

let(:struct) { Attributor::Struct.construct(attribute_definition, options)}

let(:data) { {title: 'my post'} }
subject(:loaded) { struct.load(data) }

subject(:loaded) { struct.load(data) }


it 'validates' do
Expand Down Expand Up @@ -372,6 +372,11 @@
end
end

context 'for models using the "requires" DSL' do
subject(:address) { Address.load(state: 'CA') }
its(:validate) { should_not be_empty }
its(:validate) { should include "Key name is required for $." }
end
context 'for models with circular sub-attributes' do
context 'that are valid' do
subject(:person) { Person.example }
Expand Down

0 comments on commit 955a9ca

Please sign in to comment.