Class: OpenAPIParser::SchemaValidator::StringValidator

Inherits:
Base
  • Object
show all
Includes:
Enumable
Defined in:
lib/openapi_parser/schema_validator/string_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#validatable

Instance Method Summary collapse

Methods included from Enumable

#check_enum_include

Methods inherited from Base

#validate_discriminator_schema

Constructor Details

#initialize(validator, allow_empty_date_and_datetime, coerce_value, datetime_coerce_class) ⇒ StringValidator

Returns a new instance of StringValidator.



5
6
7
8
9
# File 'lib/openapi_parser/schema_validator/string_validator.rb', line 5

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

Instance Method Details

#coerce_and_validate(value, schema, **_keyword_args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openapi_parser/schema_validator/string_validator.rb', line 11

def coerce_and_validate(value, schema, **_keyword_args)
  unless value.kind_of?(String)
    # Skip validation if the format is `binary`, even if the value is not an actual string.
    # ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types
    if schema.format == 'binary'
      # TODO:
      # It would be better to check whether the value is an instance of `Rack::Multipart::UploadFile`,
      # `ActionDispatch::Http::UploadedFile`, or another similar class.
      return [value, nil]
    end

    return OpenAPIParser::ValidateError.build_error_result(value, schema)
  end

  value, err = check_enum_include(value, schema)
  return [nil, err] if err

  value, err = pattern_validate(value, schema)
  return [nil, err] if err

  value, err = validate_max_min_length(value, schema)
  return [nil, err] if err

  value, err = validate_email_format(value, schema)
  return [nil, err] if err

  value, err = validate_uuid_format(value, schema)
  return [nil, err] if err

  value, err = validate_date_format(value, schema)
  return [nil, err] if err

  value, err = validate_datetime_format(value, schema)
  return [nil, err] if err

  [value, nil]
end