Class: Explicit::Type::Date

Inherits:
Explicit::Type show all
Defined in:
lib/explicit/type/date.rb

Constant Summary collapse

Eval =
->(expr) { expr.respond_to?(:call) ? expr.call : expr }

Instance Attribute Summary collapse

Attributes inherited from Explicit::Type

#auth_type, #default, #description, #nilable, #param_location

Instance Method Summary collapse

Methods inherited from Explicit::Type

#auth_basic?, #auth_bearer?, build, #error_i18n, #mcp_schema, #merge_base_json_schema, #param_location_body?, #param_location_path?, #param_location_query?, #required?, #swagger_i18n, #swagger_schema

Constructor Details

#initialize(min: nil, max: nil) ⇒ Date



8
9
10
11
# File 'lib/explicit/type/date.rb', line 8

def initialize(min: nil, max: nil)
  @min = min
  @max = max
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/explicit/type/date.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



4
5
6
# File 'lib/explicit/type/date.rb', line 4

def min
  @min
end

Instance Method Details

#json_schema(flavour) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/explicit/type/date.rb', line 54

def json_schema(flavour)
  {
    type: "string",
    pattern: /\d{4}-\d{2}-\d{2}/.inspect[1..-2],
    format: "date",
    description_topics: [
      swagger_i18n("date_format")
    ]
  }
end

#validate(value) ⇒ Object



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
# File 'lib/explicit/type/date.rb', line 13

def validate(value)
  return [ :ok, value ] if value.is_a?(::Date)
  return error_i18n("string") if !value.is_a?(::String)

  date = ::Date.parse(value, false)

  if min
    min_value = Eval[min]

    if date.before?(min_value)
      return error_i18n("date_min", min: min_value)
    end
  end

  if max
    max_value = Eval[max]

    if date.after?(max_value)
      return error_i18n("date_max", max: max_value)
    end
  end

  [ :ok, date ]
rescue ::Date::Error
  error_i18n("date_format")
end