All Files (100.0% covered at 2.06 hits/line)
16 files in total.
327 relevant lines.
327 lines covered and
0 lines missed
# 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)
- 4
return if !@required && params[attr_name].blank?
- 3
value = params[attr_name].to_i
- 3
return if value <= @option
- 1
message = "must be equal to or below #{@option}"
- 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 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)
- 4
return if !@required && params[attr_name].blank?
- 3
value = params[attr_name].to_i
- 3
return if value >= @option
- 1
message = "must be equal to or above #{@option}"
- 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 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 :number, type: Integer, maximum_value: 10
end
- 1
post "/" do
- 3
body false
end
end
end
end
- 1
def app
- 4
ValidationsSpec::MaximumValueValidatorSpec::API
end
- 5
let(:params) { { number: number } }
- 2
let(:number) { nil }
- 5
before { post "/", params }
- 5
subject { last_response.status }
- 1
context "when the value is less than the configured minimum value" do
- 2
let(:number) { 9 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the value is equal to the configured minimum value" do
- 2
let(:number) { 10 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the value is more than the configured minimum value" do
- 2
let(:number) { 11 }
- 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::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 :number, type: Integer, minimum_value: 10
end
- 1
post "/" do
- 3
body false
end
end
end
end
- 1
def app
- 4
ValidationsSpec::MinimumValueValidatorSpec::API
end
- 5
let(:params) { { number: number } }
- 2
let(:number) { nil }
- 5
before { post "/", params }
- 5
subject { last_response.status }
- 1
context "when the value is less than the configured minimum value" do
- 2
let(:number) { 9 }
- 2
it { is_expected.to eq(400) }
end
- 1
context "when the value is equal to the configured minimum value" do
- 2
let(:number) { 10 }
- 2
it { is_expected.to eq(204) }
end
- 1
context "when the value is more than the configured minimum value" do
- 2
let(:number) { 11 }
- 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::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 1.0.0" do
- 1
expect(described_class::VERSION).to eq("1.0.0")
end
end