Sha256: e39abe5eed3c54ff65e7f416c99006c0de4969a99be10548e99483892f3f63ab

Contents?: true

Size: 1010 Bytes

Versions: 3

Compression:

Stored size: 1010 Bytes

Contents

class OpenAPIParser::SchemaValidator
  class StringValidator < Base
    include ::OpenAPIParser::SchemaValidator::Enumable

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

    def coerce_and_validate(value, schema)
      return validator.validate_error(value, schema) unless value.is_a?(String)

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

      value = coerce_date_time(value, schema) unless @datetime_coerce_class.nil?

      [value, nil]
    end

    private

    # @param [OpenAPIParser::Schemas::Schema] schema
    def coerce_date_time(value, schema)
      schema.format == "date-time" ? parse_date_time(value) : value
    end

    def parse_date_time(value)
      begin
        return @datetime_coerce_class.parse(value)
      rescue ArgumentError => e
        raise e unless e.message =~ /invalid date/
      end

      value
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
openapi_parser-0.1.5 lib/openapi_parser/schema_validators/string_validator.rb
openapi_parser-0.1.4 lib/openapi_parser/schema_validators/string_validator.rb
openapi_parser-0.1.3 lib/openapi_parser/schema_validators/string_validator.rb