Skip to content

Commit

Permalink
Merge pull request #2262 from duffn/duffn/custom-validator
Browse files Browse the repository at this point in the history
Update documentation to use raise instead of fail for custom validation exception
  • Loading branch information
dblock authored Jun 13, 2022
2 parents 5cc3226 + 5b159ab commit 6528a12
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ end
class AlphaNumeric < Grape::Validations::Validators::Base
def validate_param!(attr_name, params)
unless params[attr_name] =~ /\A[[:alnum:]]+\z/
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'must consist of alpha-numeric characters'
raise Grape::Exceptions::Validation.new params: [@scope.full_name(attr_name)], message: 'must consist of alpha-numeric characters'
end
end
end
Expand All @@ -1758,7 +1758,7 @@ You can also create custom classes that take parameters.
class Length < Grape::Validations::Validators::Base
def validate_param!(attr_name, params)
unless params[attr_name].length <= @option
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
raise Grape::Exceptions::Validation.new params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
end
end
end
Expand All @@ -1784,7 +1784,7 @@ class Admin < Grape::Validations::Validators::Base
return unless @option
# check if user is admin or not
# as an example get a token from request and check if it's admin or not
fail Grape::Exceptions::Validation, params: @attrs, message: 'Can not set admin-only field.' unless request.headers['X-Access-Token'] == 'admin'
raise Grape::Exceptions::Validation.new params: @attrs, message: 'Can not set admin-only field.' unless request.headers['X-Access-Token'] == 'admin'
end
end
```
Expand Down

0 comments on commit 6528a12

Please sign in to comment.