loading
Generated 2020-09-11T11:56:41+09:00

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% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
lib/grape/extra_validators.rb 100.00 % 5 2 2 0 4.50
lib/grape/extra_validators/end_with.rb 100.00 % 31 15 15 0 3.33
lib/grape/extra_validators/length.rb 100.00 % 32 15 15 0 3.20
lib/grape/extra_validators/maximum_length.rb 100.00 % 25 11 11 0 1.45
lib/grape/extra_validators/maximum_value.rb 100.00 % 29 13 13 0 3.08
lib/grape/extra_validators/minimum_length.rb 100.00 % 25 11 11 0 1.45
lib/grape/extra_validators/minimum_value.rb 100.00 % 29 13 13 0 3.08
lib/grape/extra_validators/start_with.rb 100.00 % 31 15 15 0 3.33
spec/grape/extra_validators/end_with_spec.rb 100.00 % 72 38 38 0 1.76
spec/grape/extra_validators/length_spec.rb 100.00 % 98 53 53 0 1.92
spec/grape/extra_validators/maximum_length_spec.rb 100.00 % 49 26 26 0 1.96
spec/grape/extra_validators/maximum_value_spec.rb 100.00 % 86 46 46 0 2.26
spec/grape/extra_validators/minimum_length_spec.rb 100.00 % 49 26 26 0 1.96
spec/grape/extra_validators/minimum_value_spec.rb 100.00 % 86 46 46 0 2.26
spec/grape/extra_validators/start_with_spec.rb 100.00 % 72 38 38 0 1.76
spec/grape/extra_validators/version_spec.rb 100.00 % 7 3 3 0 1.00

lib/grape/extra_validators.rb

100.0% lines covered

2 relevant lines. 2 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 Dir.glob(File.join(File.dirname(__FILE__), "extra_validators", "*.rb")).sort.each do |file|
  3. 8 require file
  4. end

lib/grape/extra_validators/end_with.rb

100.0% lines covered

15 relevant lines. 15 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class EndWith < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 6 return if !@required && params[attr_name].blank?
  14. 5 end_with_values = @option.instance_of?(Array) ? @option : [@option]
  15. # `to_s` is for Symbol.
  16. 5 parameter = params[attr_name].presence.to_s
  17. 12 is_valid = end_with_values.any? { |value| parameter.ends_with?(value.to_s) }
  18. 5 return if is_valid
  19. 5 allow_values = end_with_values.map { |value| "\"#{value}\"" }
  20. 2 allow_values[allow_values.length - 1] = "or #{allow_values.last}" if allow_values.length > 1
  21. 2 message = "must end with #{allow_values.join(", ")}"
  22. 2 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  23. end
  24. end
  25. end
  26. end

lib/grape/extra_validators/length.rb

100.0% lines covered

15 relevant lines. 15 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class Length < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 12 return if !@required && params[attr_name].blank?
  14. 10 if @option.instance_of?(Range)
  15. 7 return if @option.include?(params[attr_name].length)
  16. 2 message = "must be #{@option.first} to #{@option.last} characters long"
  17. 2 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  18. end
  19. 3 return if params[attr_name].length == @option
  20. 2 unit = "character".pluralize(@option)
  21. 2 message = "must be #{@option} #{unit} long"
  22. 2 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  23. end
  24. end
  25. end
  26. end

lib/grape/extra_validators/maximum_length.rb

100.0% lines covered

11 relevant lines. 11 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class MaximumLength < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 4 return if !@required && params[attr_name].blank?
  14. 3 return if params[attr_name].length <= @option
  15. 1 unit = "character".pluralize(@option)
  16. 1 message = "must be up to #{@option} #{unit} long"
  17. 1 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  18. end
  19. end
  20. end
  21. end

lib/grape/extra_validators/maximum_value.rb

100.0% lines covered

13 relevant lines. 13 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class MaximumValue < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 6 return if !@required && params[attr_name].blank?
  14. 6 maximum_value = @option.instance_of?(Proc) ? @option.call(params) : @option
  15. 6 return if maximum_value.blank?
  16. 6 value = params[attr_name].to_i
  17. 6 return if value <= maximum_value
  18. 2 message = "must be equal to or below #{maximum_value}"
  19. 2 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  20. end
  21. end
  22. end
  23. end

lib/grape/extra_validators/minimum_length.rb

100.0% lines covered

11 relevant lines. 11 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class MinimumLength < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 4 return if !@required && params[attr_name].blank?
  14. 3 return if params[attr_name].length >= @option
  15. 1 unit = "character".pluralize(@option)
  16. 1 message = "must be at least #{@option} #{unit} long"
  17. 1 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  18. end
  19. end
  20. end
  21. end

lib/grape/extra_validators/minimum_value.rb

100.0% lines covered

13 relevant lines. 13 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class MinimumValue < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 6 return if !@required && params[attr_name].blank?
  14. 6 minimum_value = @option.instance_of?(Proc) ? @option.call(params) : @option
  15. 6 return if minimum_value.blank?
  16. 6 value = params[attr_name].to_i
  17. 6 return if value >= minimum_value
  18. 2 message = "must be equal to or above #{minimum_value}"
  19. 2 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  20. end
  21. end
  22. end
  23. end

lib/grape/extra_validators/start_with.rb

100.0% lines covered

15 relevant lines. 15 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require "active_support/all"
  3. 1 require "grape"
  4. 1 module Grape
  5. 1 module ExtraValidators
  6. 1 class StartWith < Grape::Validations::Base
  7. # ------------------------------------------------------------------------------------------------------------------------
  8. # Methods
  9. # ------------------------------------------------------------------------------------------------------------------------
  10. # Public Methods
  11. # ------------------------------------------------------------------------------------------------------------------------
  12. 1 def validate_param!(attr_name, params)
  13. 6 return if !@required && params[attr_name].blank?
  14. 5 start_with_values = @option.instance_of?(Array) ? @option : [@option]
  15. # `to_s` is for Symbol.
  16. 5 parameter = params[attr_name].presence.to_s
  17. 12 is_valid = start_with_values.any? { |value| parameter.starts_with?(value.to_s) }
  18. 5 return if is_valid
  19. 5 allow_values = start_with_values.map { |value| "\"#{value}\"" }
  20. 2 allow_values[allow_values.length - 1] = "or #{allow_values.last}" if allow_values.length > 1
  21. 2 message = "must start with #{allow_values.join(", ")}"
  22. 2 fail Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
  23. end
  24. end
  25. end
  26. end

spec/grape/extra_validators/end_with_spec.rb

100.0% lines covered

38 relevant lines. 38 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::EndWith do
  3. 1 module ValidationsSpec
  4. 1 module EndWithValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :text, type: String, end_with: "JPY"
  9. end
  10. 1 post "/not-array" do
  11. 2 body false
  12. end
  13. 1 params do
  14. 1 optional :text, type: String, end_with: ["JPY", "USD"]
  15. end
  16. 1 post "/array" do
  17. 2 body false
  18. end
  19. end
  20. end
  21. end
  22. 1 def app
  23. 5 ValidationsSpec::EndWithValidatorSpec::API
  24. end
  25. 5 let(:params) { { text: text } }
  26. 2 let(:text) { nil }
  27. 5 subject { last_response.status }
  28. 1 describe "Specify a not array" do
  29. 4 before { post "/not-array", params }
  30. 1 context "when the string ends with the specific string" do
  31. 2 let(:text) { "100JPY" }
  32. 2 it { is_expected.to eq(204) }
  33. end
  34. 1 context "when the string does not end with the specific string" do
  35. 2 let(:text) { "100Â¥" }
  36. 2 it { is_expected.to eq(400) }
  37. end
  38. 1 context "when the parameter is nil" do
  39. 2 it { is_expected.to eq(204) }
  40. end
  41. end
  42. 1 describe "Specify an array" do
  43. 3 before { |example| post "/array", params unless example.metadata[:skip_before_request_call] }
  44. 1 context "when the string ends with a string one of the specific strings" do
  45. 1 it "should not return any errors", skip_before_request_call: true do
  46. 1 ["100JPY", "100USD"].each do |text|
  47. 2 post "/array", { text: text }
  48. 2 expect(last_response.status).to eq(204)
  49. end
  50. end
  51. end
  52. 1 context "when the string does not end with a string one of the specific strings" do
  53. 2 let(:text) { "100Â¥" }
  54. 2 it { is_expected.to eq(400) }
  55. end
  56. end
  57. end

spec/grape/extra_validators/length_spec.rb

100.0% lines covered

53 relevant lines. 53 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::Length do
  3. 1 module ValidationsSpec
  4. 1 module LengthValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :text, type: String, length: 6
  9. end
  10. 1 post "/integer" do
  11. 2 body false
  12. end
  13. 1 params do
  14. 1 optional :text, type: String, length: 4..8
  15. end
  16. 1 post "/range" do
  17. 6 body false
  18. end
  19. end
  20. end
  21. end
  22. 1 def app
  23. 8 ValidationsSpec::LengthValidatorSpec::API
  24. end
  25. 8 let(:params) { { text: text } }
  26. 3 let(:text) { nil }
  27. 8 subject { last_response.status }
  28. 1 describe "Specify an integer number" do
  29. 5 before { post "/integer", params }
  30. 1 context "when the length is less than the configured length" do
  31. 2 let(:text) { "12345" }
  32. 2 it { is_expected.to eq(400) }
  33. end
  34. 1 context "when the length is equal to the configured length" do
  35. 2 let(:text) { "123456" }
  36. 2 it { is_expected.to eq(204) }
  37. end
  38. 1 context "when the length is more than the configured length" do
  39. 2 let(:text) { "1234567" }
  40. 2 it { is_expected.to eq(400) }
  41. end
  42. 1 context "when the parameter is nil" do
  43. 2 it { is_expected.to eq(204) }
  44. end
  45. end
  46. 1 describe "Specify a range" do
  47. 5 before { |example| post "/range", params unless example.metadata[:skip_before_request_call] }
  48. 1 context "when the length is less than the configured minimum length" do
  49. 2 let(:text) { "123" }
  50. 2 it { is_expected.to eq(400) }
  51. end
  52. 1 context "when the length is within the configured length", skip_before_request_call: true do
  53. 1 it "should pass" do
  54. 1 post "/range", { text: "1234" }
  55. 1 expect(last_response.status).to eq(204)
  56. 1 post "/range", { text: "12345" }
  57. 1 expect(last_response.status).to eq(204)
  58. 1 post "/range", { text: "123456" }
  59. 1 expect(last_response.status).to eq(204)
  60. 1 post "/range", { text: "1234567" }
  61. 1 expect(last_response.status).to eq(204)
  62. 1 post "/range", { text: "12345678" }
  63. 1 expect(last_response.status).to eq(204)
  64. end
  65. end
  66. 1 context "when the length is more than the configured maximum length" do
  67. 2 let(:text) { "123456789" }
  68. 2 it { is_expected.to eq(400) }
  69. end
  70. 1 context "when the parameter is nil" do
  71. 2 it { is_expected.to eq(204) }
  72. end
  73. end
  74. end

spec/grape/extra_validators/maximum_length_spec.rb

100.0% lines covered

26 relevant lines. 26 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::MaximumLength do
  3. 1 module ValidationsSpec
  4. 1 module MaximumLengthValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :text, type: String, maximum_length: 8
  9. end
  10. 1 post "/" do
  11. 3 body false
  12. end
  13. end
  14. end
  15. end
  16. 1 def app
  17. 4 ValidationsSpec::MaximumLengthValidatorSpec::API
  18. end
  19. 5 let(:params) { { text: text } }
  20. 2 let(:text) { nil }
  21. 5 before { post "/", params }
  22. 5 subject { last_response.status }
  23. 1 context "when the length is less than the configured maximum length" do
  24. 2 let(:text) { "1234567" }
  25. 2 it { is_expected.to eq(204) }
  26. end
  27. 1 context "when the length is equal to the configured maximum length" do
  28. 2 let(:text) { "12345678" }
  29. 2 it { is_expected.to eq(204) }
  30. end
  31. 1 context "when the length is more than the configured maximum length" do
  32. 2 let(:text) { "123456789" }
  33. 2 it { is_expected.to eq(400) }
  34. end
  35. 1 context "when the parameter is nil" do
  36. 2 it { is_expected.to eq(204) }
  37. end
  38. end

spec/grape/extra_validators/maximum_value_spec.rb

100.0% lines covered

46 relevant lines. 46 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::MaximumValue do
  3. 1 module ValidationsSpec
  4. 1 module MaximumValueValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :static_number, type: Integer, maximum_value: 10
  9. 1 optional :maximum_value_for_proc_number, type: Integer, allow_blank: false
  10. 4 optional :proc_number, type: Integer, maximum_value: ->(params) { params[:maximum_value_for_proc_number] + 1 }
  11. end
  12. 1 post "/" do
  13. 5 body false
  14. end
  15. end
  16. end
  17. end
  18. 1 def app
  19. 7 ValidationsSpec::MaximumValueValidatorSpec::API
  20. end
  21. 1 let(:params) do
  22. {
  23. 6 static_number: static_number,
  24. maximum_value_for_proc_number: maximum_value_for_proc_number,
  25. proc_number: proc_number,
  26. }.compact
  27. end
  28. 4 let(:static_number) { nil }
  29. 4 let(:maximum_value_for_proc_number) { nil }
  30. 4 let(:proc_number) { nil }
  31. 8 before { post "/", params }
  32. 8 subject { last_response.status }
  33. 1 context "when a configured maximum value is a static value" do
  34. 1 context "the value is less than the maximum value" do
  35. 2 let(:static_number) { 9 }
  36. 2 it { is_expected.to eq(204) }
  37. end
  38. 1 context "the value is equal to the maximum value" do
  39. 2 let(:static_number) { 10 }
  40. 2 it { is_expected.to eq(204) }
  41. end
  42. 1 context "the value is more than the maximum value" do
  43. 2 let(:static_number) { 11 }
  44. 2 it { is_expected.to eq(400) }
  45. end
  46. end
  47. 1 context "when a configured maximum value is a Proc" do
  48. 1 context "the value is less than the maximum value" do
  49. 2 let(:maximum_value_for_proc_number) { 10 }
  50. 2 let(:proc_number) { 10 }
  51. 2 it { is_expected.to eq(204) }
  52. end
  53. 1 context "the value is equal to the maximum value" do
  54. 2 let(:maximum_value_for_proc_number) { 10 }
  55. 2 let(:proc_number) { 11 }
  56. 2 it { is_expected.to eq(204) }
  57. end
  58. 1 context "the value is more than the maximum value" do
  59. 2 let(:maximum_value_for_proc_number) { 10 }
  60. 2 let(:proc_number) { 12 }
  61. 2 it { is_expected.to eq(400) }
  62. end
  63. end
  64. 1 context "when the parameter is nil" do
  65. 2 let(:params) { {} }
  66. 2 it { is_expected.to eq(204) }
  67. end
  68. end

spec/grape/extra_validators/minimum_length_spec.rb

100.0% lines covered

26 relevant lines. 26 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::MinimumLength do
  3. 1 module ValidationsSpec
  4. 1 module MinimumLengthValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :text, type: String, minimum_length: 4
  9. end
  10. 1 post "/" do
  11. 3 body false
  12. end
  13. end
  14. end
  15. end
  16. 1 def app
  17. 4 ValidationsSpec::MinimumLengthValidatorSpec::API
  18. end
  19. 5 let(:params) { { text: text } }
  20. 2 let(:text) { nil }
  21. 5 before { post "/", params }
  22. 5 subject { last_response.status }
  23. 1 context "when the length is less than the configured minimum length" do
  24. 2 let(:text) { "123" }
  25. 2 it { is_expected.to eq(400) }
  26. end
  27. 1 context "when the length is equal to the configured minimum length" do
  28. 2 let(:text) { "1234" }
  29. 2 it { is_expected.to eq(204) }
  30. end
  31. 1 context "when the length is more than the configured minimum length" do
  32. 2 let(:text) { "12345" }
  33. 2 it { is_expected.to eq(204) }
  34. end
  35. 1 context "when the parameter is nil" do
  36. 2 it { is_expected.to eq(204) }
  37. end
  38. end

spec/grape/extra_validators/minimum_value_spec.rb

100.0% lines covered

46 relevant lines. 46 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::MinimumValue do
  3. 1 module ValidationsSpec
  4. 1 module MinimumValueValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :static_number, type: Integer, minimum_value: 10
  9. 1 optional :minimum_value_for_proc_number, type: Integer, allow_blank: false
  10. 4 optional :proc_number, type: Integer, minimum_value: ->(params) { params[:minimum_value_for_proc_number] - 1 }
  11. end
  12. 1 post "/" do
  13. 5 body false
  14. end
  15. end
  16. end
  17. end
  18. 1 def app
  19. 7 ValidationsSpec::MinimumValueValidatorSpec::API
  20. end
  21. 1 let(:params) do
  22. {
  23. 6 static_number: static_number,
  24. minimum_value_for_proc_number: minimum_value_for_proc_number,
  25. proc_number: proc_number,
  26. }.compact
  27. end
  28. 4 let(:static_number) { nil }
  29. 4 let(:minimum_value_for_proc_number) { nil }
  30. 4 let(:proc_number) { nil }
  31. 8 before { post "/", params }
  32. 8 subject { last_response.status }
  33. 1 context "when a configured minimum value is a static value" do
  34. 1 context "when the value is less than the minimum value" do
  35. 2 let(:static_number) { 9 }
  36. 2 it { is_expected.to eq(400) }
  37. end
  38. 1 context "when the value is equal to the minimum value" do
  39. 2 let(:static_number) { 10 }
  40. 2 it { is_expected.to eq(204) }
  41. end
  42. 1 context "when the value is more than the minimum value" do
  43. 2 let(:static_number) { 11 }
  44. 2 it { is_expected.to eq(204) }
  45. end
  46. end
  47. 1 context "when a configured minimum value is a Proc" do
  48. 1 context "the value is less than the minimum value" do
  49. 2 let(:minimum_value_for_proc_number) { 12 }
  50. 2 let(:proc_number) { 10 }
  51. 2 it { is_expected.to eq(400) }
  52. end
  53. 1 context "the value is equal to the minimum value" do
  54. 2 let(:minimum_value_for_proc_number) { 12 }
  55. 2 let(:proc_number) { 11 }
  56. 2 it { is_expected.to eq(204) }
  57. end
  58. 1 context "the value is more than the minimum value" do
  59. 2 let(:minimum_value_for_proc_number) { 12 }
  60. 2 let(:proc_number) { 12 }
  61. 2 it { is_expected.to eq(204) }
  62. end
  63. end
  64. 1 context "when the parameter is nil" do
  65. 2 let(:params) { {} }
  66. 2 it { is_expected.to eq(204) }
  67. end
  68. end

spec/grape/extra_validators/start_with_spec.rb

100.0% lines covered

38 relevant lines. 38 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators::StartWith do
  3. 1 module ValidationsSpec
  4. 1 module StartWithValidatorSpec
  5. 1 class API < Grape::API
  6. 1 default_format :json
  7. 1 params do
  8. 1 optional :text, type: String, start_with: "https://"
  9. end
  10. 1 post "/not-array" do
  11. 2 body false
  12. end
  13. 1 params do
  14. 1 optional :text, type: String, start_with: ["http://", "https://"]
  15. end
  16. 1 post "/array" do
  17. 2 body false
  18. end
  19. end
  20. end
  21. end
  22. 1 def app
  23. 5 ValidationsSpec::StartWithValidatorSpec::API
  24. end
  25. 5 let(:params) { { text: text } }
  26. 2 let(:text) { nil }
  27. 5 subject { last_response.status }
  28. 1 describe "Specify a not array" do
  29. 4 before { post "/not-array", params }
  30. 1 context "when the string starts with the specific string" do
  31. 2 let(:text) { "https://example.com" }
  32. 2 it { is_expected.to eq(204) }
  33. end
  34. 1 context "when the string does not start with the specific string" do
  35. 2 let(:text) { "http://example.com" }
  36. 2 it { is_expected.to eq(400) }
  37. end
  38. 1 context "when the parameter is nil" do
  39. 2 it { is_expected.to eq(204) }
  40. end
  41. end
  42. 1 describe "Specify an array" do
  43. 3 before { |example| post "/array", params unless example.metadata[:skip_before_request_call] }
  44. 1 context "when the string starts with a string one of the specific strings" do
  45. 1 it "should not return any errors", skip_before_request_call: true do
  46. 1 ["http://example.com", "https://example.com"].each do |text|
  47. 2 post "/array", { text: text }
  48. 2 expect(last_response.status).to eq(204)
  49. end
  50. end
  51. end
  52. 1 context "when the string does not start with a string one of the specific strings" do
  53. 2 let(:text) { "file://example.com" }
  54. 2 it { is_expected.to eq(400) }
  55. end
  56. end
  57. end

spec/grape/extra_validators/version_spec.rb

100.0% lines covered

3 relevant lines. 3 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 describe Grape::ExtraValidators, type: :feature do
  3. 1 example "The version number is 2.0.0" do
  4. 1 expect(described_class::VERSION).to eq("2.0.0")
  5. end
  6. end