-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom types can set a message to be used in the response when invalid
Just return an instance of `Grape::Types::InvalidValue` with the message: class Color def self.parse(value) return value if %w[blue red green].include?(value) Grape::Types::InvalidValue.new('Invalid color') end end Any raised exception will be treated as an invalid value as it was before.
- Loading branch information
1 parent
e7f771e
commit 54b2e11
Showing
8 changed files
with
83 additions
and
22 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
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
module Validations | ||
module Types | ||
# Instances of this class may be used as tokens to denote that a parameter value could not be | ||
# coerced. The given message will be used as a validation error. | ||
class InvalidValue | ||
attr_reader :message | ||
|
||
def initialize(message = nil) | ||
@message = message | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# only exists to make it shorter for external use | ||
module Grape | ||
module Types | ||
InvalidValue = Class.new(Grape::Validations::Types::InvalidValue) | ||
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