Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to disable email format validation. #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/openapi_parser/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,25 @@ def validate_header
@config.fetch(:validate_header, true)
end

def validate_email_format
@config.fetch(:validate_email_format, true)
end

# @return [OpenAPIParser::SchemaValidator::Options]
def request_validator_options
@request_validator_options ||= OpenAPIParser::SchemaValidator::Options.new(coerce_value: coerce_value,
datetime_coerce_class: datetime_coerce_class,
validate_header: validate_header)
validate_header: validate_header,
validate_email_format: validate_email_format)
end

alias_method :request_body_options, :request_validator_options
alias_method :path_params_options, :request_validator_options

# @return [OpenAPIParser::SchemaValidator::ResponseValidateOptions]
def response_validate_options
@response_validate_options ||= OpenAPIParser::SchemaValidator::ResponseValidateOptions.
new(strict: strict_response_validation, validate_header: validate_header)
@response_validate_options ||= OpenAPIParser::SchemaValidator::ResponseValidateOptions.new(strict: strict_response_validation,
validate_header: validate_header,
validate_email_format: validate_email_format)
end
end
3 changes: 2 additions & 1 deletion lib/openapi_parser/schema_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def initialize(value, schema, options)
@schema = schema
@coerce_value = options.coerce_value
@datetime_coerce_class = options.datetime_coerce_class
@validate_email_format = options.validate_email_format
end

# execute validate data
Expand Down Expand Up @@ -119,7 +120,7 @@ def validator(value, schema)
end

def string_validator
@string_validator ||= OpenAPIParser::SchemaValidator::StringValidator.new(self, @coerce_value, @datetime_coerce_class)
@string_validator ||= OpenAPIParser::SchemaValidator::StringValidator.new(self, @coerce_value, @datetime_coerce_class, @validate_email_format)
end

def integer_validator
Expand Down
12 changes: 8 additions & 4 deletions lib/openapi_parser/schema_validators/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ class Options
# @return [Object, nil] coerce datetime string by this Object class
# @!attribute [r] validate_header
# @return [Boolean] validate header or not
attr_reader :coerce_value, :datetime_coerce_class, :validate_header
# @!attribute [r] validate_email_format
# @return [Boolean] validate email format or not
attr_reader :coerce_value, :datetime_coerce_class, :validate_header, :validate_email_format

def initialize(coerce_value: nil, datetime_coerce_class: nil, validate_header: true)
def initialize(coerce_value: nil, datetime_coerce_class: nil, validate_header: true, validate_email_format: true)
@coerce_value = coerce_value
@datetime_coerce_class = datetime_coerce_class
@validate_header = validate_header
@validate_email_format = validate_email_format
end
end

# response body validation option
class ResponseValidateOptions
# @!attribute [r] strict
# @return [Boolean] validate by strict (when not exist definition, raise error)
attr_reader :strict, :validate_header
attr_reader :strict, :validate_header, :validate_email_format

def initialize(strict: false, validate_header: true)
def initialize(strict: false, validate_header: true, validate_email_format: true)
@strict = strict
@validate_header = validate_header
@validate_email_format = validate_email_format
end
end
end
5 changes: 3 additions & 2 deletions lib/openapi_parser/schema_validators/string_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ class OpenAPIParser::SchemaValidator
class StringValidator < Base
include ::OpenAPIParser::SchemaValidator::Enumable

def initialize(validator, coerce_value, datetime_coerce_class)
def initialize(validator, coerce_value, datetime_coerce_class, validate_email_format)
super(validator, coerce_value)
@datetime_coerce_class = datetime_coerce_class
@validate_email_format = validate_email_format
end

def coerce_and_validate(value, schema, **_keyword_args)
Expand Down Expand Up @@ -53,7 +54,7 @@ def validate_max_min_length(value, schema)
end

def validate_email_format(value, schema)
return [value, nil] unless schema.format == 'email'
return [value, nil] unless @validate_email_format && schema.format == 'email'

# match? method is good performance.
# So when we drop ruby 2.3 support we use match? method because this method add ruby 2.4
Expand Down
1 change: 1 addition & 0 deletions sig/openapi_parser/config.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module OpenAPIParser
def expand_reference: -> bool
def strict_response_validation: -> bool
def validate_header: -> bool
def validate_email_format: -> bool
def request_validator_options: -> OpenAPIParser::SchemaValidator::Options
def response_validate_options: -> OpenAPIParser::SchemaValidator::ResponseValidateOptions
end
Expand Down
6 changes: 4 additions & 2 deletions sig/openapi_parser/schema_validators/options.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ module OpenAPIParser
attr_reader coerce_value: bool | nil
attr_reader datetime_coerce_class: singleton(Object) | nil
attr_reader validate_header: bool
def initialize: (?coerce_value: bool | nil, ?datetime_coerce_class: singleton(Object) | nil, ?validate_header: bool) -> untyped
attr_reader validate_email_format: bool
def initialize: (?coerce_value: bool | nil, ?datetime_coerce_class: singleton(Object) | nil, ?validate_header: bool, ?validate_email_format: bool) -> untyped
end

class ResponseValidateOptions
attr_reader strict: bool
attr_reader validate_header: bool
def initialize: (?strict: bool, ?validate_header: bool) -> untyped
attr_reader validate_email_format: bool
def initialize: (?strict: bool, ?validate_header: bool, ?validate_email_format: bool) -> untyped
end
end
end
7 changes: 7 additions & 0 deletions spec/openapi_parser/schema_validator/string_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@
end
end
end

context "disabled" do
let(:options) { ::OpenAPIParser::SchemaValidator::Options.new validate_email_format: false }
let(:params) { { 'email_str' => "Hello (RFC2822 address) <[email protected]>" } }

it { expect(subject).to eq({ 'email_str' => "Hello (RFC2822 address) <[email protected]>" }) }
end
end

describe 'validate uuid format' do
Expand Down