Class: Webhookdb::API::Helpers::Prompt
- Inherits:
-
Grape::Validations::Validators::Base
- Object
- Grape::Validations::Validators::Base
- Webhookdb::API::Helpers::Prompt
- Defined in:
- lib/webhookdb/api/helpers.rb
Overview
Prompt for input given some criteria.
NOTE: You cannot use :prompt with ‘requires’, or ‘allow_blank’. You MUST use ‘optional’, and not specify allow_blank (validation will error if these conditions are not met). The semantics around prompts are too subtle to use these built-in validators; we must use our own. Note also, while :default will not error (because we cannot detect it easily), it should not be used since it defeats the purpose of :prompt.
The main complexity is around optional params that will take a given default if blank (but we need to prompt if not supplied), or ‘enter to confirm’ type prompts, where we want to prompt if not supplied.
To handle these cases, use the :optional and :confirm options, as below. On the Go client, use string pointers for these values, with omitempty; they will still submit an empty string, but will not include the key if nil.
Examples:
requires :param, prompt: "Enter a value:"
This is by far the most common usage. If :param is not present (ie, ‘params.present?` is falsy), a 422 is returned with a StateMachineStep with a prompt value of ’Enter a value:‘.
All other forms use a Hash instead of a string as the argument.
requires :param, prompt: {message: 'Enter secret:', secret: true}
Same as above, but the prompt is set to be a secret.
requires :param, prompt: {message: 'Press Enter to confirm, or Ctrl+C to cancel:', confirm: true}
This will 422 if :param is not provided or is nil; it will pass otherwise (so empty values, like ” or false, are valid). This is used to guard against actions that need confirmation. Note that in many situations, we don’t know about the need to confirm until we’re into the body of the endpoint. In these cases, use ‘Webhookdb::API::Helpers.prompt_for_required_param!` directly, along with something like `optional :param`.
requires :param, prompt: {message: 'This will default', optional: true}
This will 422 if not provided, but will pass otherwise (so nil, ”, and false are all valid values).
optional :param, prompt: {message: 'Bypassable', disable: ->(req) { req.env['HTTP_NOPROMPT']} }
This will disable the prompt behavior if the proc given to :disable returns true. This is mostly useful when we want to avoid prompting for something because the endpoint is going to have some particular behavior that will avoid the purpose of the prompt.
Instance Method Summary collapse
Instance Method Details
#validate(request) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/webhookdb/api/helpers.rb', line 64 def validate(request) raise "allow_blank must not be set" unless @allow_blank.nil? attr_name = @attrs.first if @option.is_a?(String) = {message: @option} else = @option raise "Missing :message key in prompt args" unless [:message] end raise "must use optional for #{attr_name}" if @required return unless self.needs_prompt?(attr_name, request, ) Webhookdb::API::Helpers.prompt_for_required_param!( request, attr_name, [:message], secret: [:secret] || false, ) end |