Grape Extra Validators

🛡 Extra validators for Grape. 🍇

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
gem CircleCI license @jagaapple_tech

Table of Contents

Quick Start

Requirements

  • Ruby 2.5 or higher
  • Grape 1.0.0 or higher

Installation

$ gem install grape-extra_validators

If you are using Bundler, add the gem to Gemfile.

gem "grape-extra_validators"

Validators

For String

Length

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
end

Maximum Length

The 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
end

Minimum Length

The 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
end

Start With

The 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://)
end

End With

The 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)
end

For Numeric

Value

This 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
end

Maximum Value

The 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
end

You 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 }
end

Minimum Value

The 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
end

You 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 }
end

Contributing to grape-extra_validators

Bug 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.

License

The library is available as open source under the terms of the MIT License.

Copyright 2020 Jaga Apple. All rights reserved.