params do
requires :name, type: String, length: 4..32
requires :age, type: Integer, minimum_value: 0
requires :website, type: String, start_with: %w(http:// https://)
end- Ruby 2.5 or higher
- Grape 1.0.0 or higher
$ gem install grape-extra_validatorsIf you are using Bundler, add the gem to Gemfile.
gem "grape-extra_validators"The length validator checks whether the parameter is a specified characters length or within a specified characters length range. You can specify an Integer or Range object to this.
params do
requires :name, type: String, length: 4
requires :bio, type: String, length: 4..512
endThe maximum length validator checks whether the parameter is up to a specified characters length. You can specify an Integer object to this.
params do
requires :username, type: String, maximum_length: 20
endThe minimum length validator checks whether the parameter is at least a specified characters length. You can specify an Integer object to this.
params do
requires :username, type: String, minimum_length: 20
endThe start with length validator checks whether the parameter starts with a specified string. You can specify a String, Symbol, or Array which has strings object to this.
params do
requires :website, type: String, start_with: "https://"
requires :website, type: String, start_with: %w(http:// https://)
endThe end with length validator checks whether the parameter ends with a specified string. You can specify a String, Symbol, or Array which has strings object to this.
params do
requires :price, type: String, end_with: "JPY"
requires :price, type: String, end_with: %w(JPY USD)
endThis gem does not support a validator which checks whether the parameter is within a specified range. You can use
values built-in validator instead.
params do
requires :point, type: Integer, values: 0..100
endThe maximum value validator checks whether the parameter is equal to or below a specified value. You can specify a Numeric object to this.
params do
requires :level, type: Integer, maximum_value: 5
endYou can pass a Proc object and request parameters are given as the first argument.
params do
requires :level, type: Integer, maximum_value: ->(params) { params[:foo] + 1 }
endThe minimum value validator checks whether the parameter is equal to or above a specified value. You can specify a Numeric object to this.
params do
requires :age, type: Integer, minimum_value: 0
endYou can pass a Proc object and request parameters are given as the first argument.
params do
requires :level, type: Integer, minimum_value: ->(params) { params[:bar] - 1 }
endBug reports and pull requests are welcome on GitHub at https://github.com/jagaapple/grape-extra_validators. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Please read Contributing Guidelines before development and contributing.
The library is available as open source under the terms of the MIT License.
Copyright 2020 Jaga Apple. All rights reserved.