Skip to content
This repository has been archived by the owner on Feb 7, 2018. It is now read-only.

Add error when attribute is present but value is nil and has no default #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

* No longer raises `Pavlov::ValidationError, "Missing arguments:` when an argument is nil but adds an error.

# 0.1.8

* Let custom validations use errors.add instead of raising exceptions
Expand Down
10 changes: 6 additions & 4 deletions lib/pavlov/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ def raise_unauthorized(message = 'Unauthorized')
end

def check_validation
fail Pavlov::ValidationError, "Missing arguments: #{missing_arguments.inspect}" if missing_arguments.any?
check_missing_arguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want a line in UPGRADING to inform people that we will no longer raise this exact exception.

validate
end

def missing_arguments
attribute_set.select do |attribute|
!attribute.options.key?(:default) && send(attribute.name).nil?
def check_missing_arguments
attribute_set.each do |attribute|
if !attribute.options.key?(:default) && send(attribute.name).nil?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't there a way which checks whether the argument was passed in? This way you cannot pass in nil anywhere (except when this is an explicit default). That's no missing argument. We might want to check for nils as well, but I'm not totally sure. If there is no other way to check missing arguments this is fine though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I originally wrote this code I couldn't find any way, but maybe the newer Virtus has more support for this. If nothing else, we could hook into the initializer now, and set all non-passed-in attributes to a AttributeNotGiven constant or something and then check for that here.

errors.add(attribute.name.to_s, "can't be blank")
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/regression/case2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def list
def members_to_remove
list.zrange(0, -1).select do |id|
activity = Activity[id]
! valid(activity)
!valid(activity)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/regression/case4_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Interactors::ExampleModule::FollowUser
arguments :user_name, :user_to_follow_user_name

def authorized?
(!! pavlov_options[:current_user]) && (pavlov_options[:current_user].username == user_name)
(!!pavlov_options[:current_user]) && (pavlov_options[:current_user].username == user_name)
end

def user
Expand Down
2 changes: 1 addition & 1 deletion spec/regression/case5_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Interactors::ExampleModule::Followers
arguments :user_name, :skip, :take

def authorized?
!! pavlov_options[:current_user]
!!pavlov_options[:current_user]
end

def validate
Expand Down