Skip to content

Commit

Permalink
Update README to raise custom validation exception
Browse files Browse the repository at this point in the history
  • Loading branch information
duffn committed Jun 13, 2022
1 parent 5cc3226 commit 5b159ab
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 5b159ab

Please sign in to comment.