Skip to content
dashkb edited this page May 4, 2012 · 3 revisions

Parameters

We support an ever-growing number of ways to validate parameters. See parameter blocks for how to create and juggle parameters. Parameter definitions take this basic form:

get '/foo' do
  required :bar do
    type String
    length == 4
  end
end

Validation

  • type(klass): currently supported are String, Array, Date, Time, Integer/Fixnum, and Hash. The parameter will be converted to this type as the first step in validation.
  • default(value): if the parameter is optional and omitted from the request, it will be set to the provided value.
  • allowed_values(ary) and disallowed_values(ary): specify an array of allowed or forbidden values for this parameter.
  • matches(regex): adds 'regex' to a list of regexes (may be called many times); the input may match any of these.
  • validate_with(&block): Adds the provided block to a list of blocks (may be called multiple times); during validation, the provided blocks will be yielded the input. If any block returns false or nil, the input is considered invalid.

Transformation

  • prevalidate_transform(&block): Adds the provided block to a list of blocks (may be called multiple times); before validation, the provided blocks will be (in order of declaration) yielded the input. The input will be assigned to the return value of the block before continuing with other transformations or validations.
Clone this wiki locally