All Files ( 100.0% covered at 2.2 hits/line )
16 files in total.
371 relevant lines,
371 lines covered and
0 lines missed.
(
100.0%
)
# frozen_string_literal: true
- 1
Dir.glob(File.join(File.dirname(__FILE__), "extra_validators", "*.rb")).sort.each do |file|
- 8
require file
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class EndWith < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 6
return if !@required && params[attr_name].blank?
- 5
end_with_values = @option.instance_of?(Array) ? @option : [@option]
# `to_s` is for Symbol.
- 5
parameter = params[attr_name].presence.to_s
- 12
is_valid = end_with_values.any? { |value| parameter.ends_with?(value.to_s) }
- 5
return if is_valid
- 5
allow_values = end_with_values.map { |value| "\"#{value}\"" }
- 2
allow_values[allow_values.length - 1] = "or #{allow_values.last}" if allow_values.length > 1
- 2
message = "must end with #{allow_values.join(", ")}"
- 2
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class Length < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 12
return if !@required && params[attr_name].blank?
- 10
if @option.instance_of?(Range)
- 7
return if @option.include?(params[attr_name].length)
- 2
message = "must be #{@option.first} to #{@option.last} characters long"
- 2
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
- 3
return if params[attr_name].length == @option
- 2
unit = "character".pluralize(@option)
- 2
message = "must be #{@option} #{unit} long"
- 2
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class MaximumLength < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 4
return if !@required && params[attr_name].blank?
- 3
return if params[attr_name].length <= @option
- 1
unit = "character".pluralize(@option)
- 1
message = "must be up to #{@option} #{unit} long"
- 1
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class MaximumValue < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 6
return if !@required && params[attr_name].blank?
- 6
maximum_value = @option.instance_of?(Proc) ? @option.call(params) : @option
- 6
return if maximum_value.blank?
- 6
value = params[attr_name].to_i
- 6
return if value <= maximum_value
- 2
message = "must be equal to or below #{maximum_value}"
- 2
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class MinimumLength < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 4
return if !@required && params[attr_name].blank?
- 3
return if params[attr_name].length >= @option
- 1
unit = "character".pluralize(@option)
- 1
message = "must be at least #{@option} #{unit} long"
- 1
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class MinimumValue < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 6
return if !@required && params[attr_name].blank?
- 6
minimum_value = @option.instance_of?(Proc) ? @option.call(params) : @option
- 6
return if minimum_value.blank?
- 6
value = params[attr_name].to_i
- 6
return if value >= minimum_value
- 2
message = "must be equal to or above #{minimum_value}"
- 2
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
require "active_support/all"
- 1
require "grape"
- 1
module Grape
- 1
module ExtraValidators
- 1
class StartWith < Grape::Validations::Base
# ------------------------------------------------------------------------------------------------------------------------
# Methods
# ------------------------------------------------------------------------------------------------------------------------
# Public Methods
# ------------------------------------------------------------------------------------------------------------------------
- 1
def validate_param!(attr_name, params)
- 6
return if !@required && params[attr_name].blank?
- 5
start_with_values = @option.instance_of?(Array) ? @option : [@option]
# `to_s` is for Symbol.
- 5
parameter = params[attr_name].presence.to_s
- 12
is_valid = start_with_values.any? { |value| parameter.starts_with?(value.to_s) }
- 5
return if is_valid
- 5
allow_values = start_with_values.map { |value| "\"#{value}\"" }
- 2
allow_values[allow_values.length - 1] = "or #{allow_values.last}" if allow_values.length > 1
- 2
message = "must start with #{allow_values.join(", ")}"
- 2
fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end
end
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::EndWith do
- 1
module ValidationsSpec
- 1
module EndWithValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :text, type: String, end_with: "JPY"
end
- 1
post "/not-array" do
- 2
body false
end
- 1
params do
- 1
optional :text, type: String, end_with: ["JPY", "USD"]
end
- 1
post "/array" do
- 2
body false
end
end
end
end
- 1
def app
- 5
ValidationsSpec::EndWithValidatorSpec::API
end
- 5
let(:params) { { text: text } }
- 2
let(:text) { nil }
- 5
subject { last_response.status }
- 1
describe "Specify a not array" do
- 4
before { post "/not-array", params }
- 1
context "when the string ends with the specific string" do
- 2
let(:text) { "100JPY" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the string does not end with the specific string" do
- 2
let(:text) { "100Â¥" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the parameter is nil" do
- 2
it { is_expected.to eq(204) }
end
end
- 1
describe "Specify an array" do
- 3
before { |example| post "/array", params unless example.metadata[:skip_before_request_call] }
- 1
context "when the string ends with a string one of the specific strings" do
- 1
it "should not return any errors", skip_before_request_call: true do
- 1
["100JPY", "100USD"].each do |text|
- 2
post "/array", { text: text }
- 2
expect(last_response.status).to eq(204)
end
end
end
- 1
context "when the string does not end with a string one of the specific strings" do
- 2
let(:text) { "100Â¥" }
- 2
it { is_expected.to eq(400) }
end
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::Length do
- 1
module ValidationsSpec
- 1
module LengthValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :text, type: String, length: 6
end
- 1
post "/integer" do
- 2
body false
end
- 1
params do
- 1
optional :text, type: String, length: 4..8
end
- 1
post "/range" do
- 6
body false
end
end
end
end
- 1
def app
- 8
ValidationsSpec::LengthValidatorSpec::API
end
- 8
let(:params) { { text: text } }
- 3
let(:text) { nil }
- 8
subject { last_response.status }
- 1
describe "Specify an integer number" do
- 5
before { post "/integer", params }
- 1
context "when the length is less than the configured length" do
- 2
let(:text) { "12345" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the length is equal to the configured length" do
- 2
let(:text) { "123456" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the length is more than the configured length" do
- 2
let(:text) { "1234567" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the parameter is nil" do
- 2
it { is_expected.to eq(204) }
end
end
- 1
describe "Specify a range" do
- 5
before { |example| post "/range", params unless example.metadata[:skip_before_request_call] }
- 1
context "when the length is less than the configured minimum length" do
- 2
let(:text) { "123" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the length is within the configured length", skip_before_request_call: true do
- 1
it "should pass" do
- 1
post "/range", { text: "1234" }
- 1
expect(last_response.status).to eq(204)
- 1
post "/range", { text: "12345" }
- 1
expect(last_response.status).to eq(204)
- 1
post "/range", { text: "123456" }
- 1
expect(last_response.status).to eq(204)
- 1
post "/range", { text: "1234567" }
- 1
expect(last_response.status).to eq(204)
- 1
post "/range", { text: "12345678" }
- 1
expect(last_response.status).to eq(204)
end
end
- 1
context "when the length is more than the configured maximum length" do
- 2
let(:text) { "123456789" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the parameter is nil" do
- 2
it { is_expected.to eq(204) }
end
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::MaximumLength do
- 1
module ValidationsSpec
- 1
module MaximumLengthValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :text, type: String, maximum_length: 8
end
- 1
post "/" do
- 3
body false
end
end
end
end
- 1
def app
- 4
ValidationsSpec::MaximumLengthValidatorSpec::API
end
- 5
let(:params) { { text: text } }
- 2
let(:text) { nil }
- 5
before { post "/", params }
- 5
subject { last_response.status }
- 1
context "when the length is less than the configured maximum length" do
- 2
let(:text) { "1234567" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the length is equal to the configured maximum length" do
- 2
let(:text) { "12345678" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the length is more than the configured maximum length" do
- 2
let(:text) { "123456789" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the parameter is nil" do
- 2
it { is_expected.to eq(204) }
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::MaximumValue do
- 1
module ValidationsSpec
- 1
module MaximumValueValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :static_number, type: Integer, maximum_value: 10
- 1
optional :maximum_value_for_proc_number, type: Integer, allow_blank: false
- 4
optional :proc_number, type: Integer, maximum_value: ->(params) { params[:maximum_value_for_proc_number] + 1 }
end
- 1
post "/" do
- 5
body false
end
end
end
end
- 1
def app
- 7
ValidationsSpec::MaximumValueValidatorSpec::API
end
- 1
let(:params) do
{
- 6
static_number: static_number,
maximum_value_for_proc_number: maximum_value_for_proc_number,
proc_number: proc_number,
}.compact
end
- 4
let(:static_number) { nil }
- 4
let(:maximum_value_for_proc_number) { nil }
- 4
let(:proc_number) { nil }
- 8
before { post "/", params }
- 8
subject { last_response.status }
- 1
context "when a configured maximum value is a static value" do
- 1
context "the value is less than the maximum value" do
- 2
let(:static_number) { 9 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "the value is equal to the maximum value" do
- 2
let(:static_number) { 10 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "the value is more than the maximum value" do
- 2
let(:static_number) { 11 }
- 2
it { is_expected.to eq(400) }
end
end
- 1
context "when a configured maximum value is a Proc" do
- 1
context "the value is less than the maximum value" do
- 2
let(:maximum_value_for_proc_number) { 10 }
- 2
let(:proc_number) { 10 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "the value is equal to the maximum value" do
- 2
let(:maximum_value_for_proc_number) { 10 }
- 2
let(:proc_number) { 11 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "the value is more than the maximum value" do
- 2
let(:maximum_value_for_proc_number) { 10 }
- 2
let(:proc_number) { 12 }
- 2
it { is_expected.to eq(400) }
end
end
- 1
context "when the parameter is nil" do
- 2
let(:params) { {} }
- 2
it { is_expected.to eq(204) }
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::MinimumLength do
- 1
module ValidationsSpec
- 1
module MinimumLengthValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :text, type: String, minimum_length: 4
end
- 1
post "/" do
- 3
body false
end
end
end
end
- 1
def app
- 4
ValidationsSpec::MinimumLengthValidatorSpec::API
end
- 5
let(:params) { { text: text } }
- 2
let(:text) { nil }
- 5
before { post "/", params }
- 5
subject { last_response.status }
- 1
context "when the length is less than the configured minimum length" do
- 2
let(:text) { "123" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the length is equal to the configured minimum length" do
- 2
let(:text) { "1234" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the length is more than the configured minimum length" do
- 2
let(:text) { "12345" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the parameter is nil" do
- 2
it { is_expected.to eq(204) }
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::MinimumValue do
- 1
module ValidationsSpec
- 1
module MinimumValueValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :static_number, type: Integer, minimum_value: 10
- 1
optional :minimum_value_for_proc_number, type: Integer, allow_blank: false
- 4
optional :proc_number, type: Integer, minimum_value: ->(params) { params[:minimum_value_for_proc_number] - 1 }
end
- 1
post "/" do
- 5
body false
end
end
end
end
- 1
def app
- 7
ValidationsSpec::MinimumValueValidatorSpec::API
end
- 1
let(:params) do
{
- 6
static_number: static_number,
minimum_value_for_proc_number: minimum_value_for_proc_number,
proc_number: proc_number,
}.compact
end
- 4
let(:static_number) { nil }
- 4
let(:minimum_value_for_proc_number) { nil }
- 4
let(:proc_number) { nil }
- 8
before { post "/", params }
- 8
subject { last_response.status }
- 1
context "when a configured minimum value is a static value" do
- 1
context "when the value is less than the minimum value" do
- 2
let(:static_number) { 9 }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the value is equal to the minimum value" do
- 2
let(:static_number) { 10 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the value is more than the minimum value" do
- 2
let(:static_number) { 11 }
- 2
it { is_expected.to eq(204) }
end
end
- 1
context "when a configured minimum value is a Proc" do
- 1
context "the value is less than the minimum value" do
- 2
let(:minimum_value_for_proc_number) { 12 }
- 2
let(:proc_number) { 10 }
- 2
it { is_expected.to eq(400) }
end
- 1
context "the value is equal to the minimum value" do
- 2
let(:minimum_value_for_proc_number) { 12 }
- 2
let(:proc_number) { 11 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "the value is more than the minimum value" do
- 2
let(:minimum_value_for_proc_number) { 12 }
- 2
let(:proc_number) { 12 }
- 2
it { is_expected.to eq(204) }
end
end
- 1
context "when the parameter is nil" do
- 2
let(:params) { {} }
- 2
it { is_expected.to eq(204) }
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators::StartWith do
- 1
module ValidationsSpec
- 1
module StartWithValidatorSpec
- 1
class API < Grape::API
- 1
default_format :json
- 1
params do
- 1
optional :text, type: String, start_with: "https://"
end
- 1
post "/not-array" do
- 2
body false
end
- 1
params do
- 1
optional :text, type: String, start_with: ["http://", "https://"]
end
- 1
post "/array" do
- 2
body false
end
end
end
end
- 1
def app
- 5
ValidationsSpec::StartWithValidatorSpec::API
end
- 5
let(:params) { { text: text } }
- 2
let(:text) { nil }
- 5
subject { last_response.status }
- 1
describe "Specify a not array" do
- 4
before { post "/not-array", params }
- 1
context "when the string starts with the specific string" do
- 2
let(:text) { "https://example.com" }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the string does not start with the specific string" do
- 2
let(:text) { "http://example.com" }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the parameter is nil" do
- 2
it { is_expected.to eq(204) }
end
end
- 1
describe "Specify an array" do
- 3
before { |example| post "/array", params unless example.metadata[:skip_before_request_call] }
- 1
context "when the string starts with a string one of the specific strings" do
- 1
it "should not return any errors", skip_before_request_call: true do
- 1
["http://example.com", "https://example.com"].each do |text|
- 2
post "/array", { text: text }
- 2
expect(last_response.status).to eq(204)
end
end
end
- 1
context "when the string does not start with a string one of the specific strings" do
- 2
let(:text) { "file://example.com" }
- 2
it { is_expected.to eq(400) }
end
end
end
# frozen_string_literal: true
- 1
describe Grape::ExtraValidators, type: :feature do
- 1
example "The version number is 2.0.0" do
- 1
expect(described_class::VERSION).to eq("2.0.0")
end
end