Class: Explicit::Type::String

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

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(empty: nil, strip: nil, format: nil, min_length: nil, max_length: nil, downcase: false) ⇒ String

Returns a new instance of String.



6
7
8
9
10
11
12
13
# File 'lib/explicit/type/string.rb', line 6

def initialize(empty: nil, strip: nil, format: nil, min_length: nil, max_length: nil, downcase: false)
  @empty = empty
  @strip = strip
  @format = format
  @min_length = min_length
  @max_length = max_length
  @downcase = downcase
end

Instance Attribute Details

#downcaseObject (readonly)

Returns the value of attribute downcase.



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

def downcase
  @downcase
end

#emptyObject (readonly)

Returns the value of attribute empty.



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

def empty
  @empty
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



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

def max_length
  @max_length
end

#min_lengthObject (readonly)

Returns the value of attribute min_length.



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

def min_length
  @min_length
end

#stripObject (readonly)

Returns the value of attribute strip.



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

def strip
  @strip
end

Instance Method Details

#json_schema(flavour) ⇒ Object



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

def json_schema(flavour)
  {
    type: "string",
    pattern: format&.inspect&.then { _1[1..-2] },
    minLength: min_length || (empty == false ? 1 : nil),
    maxLength: max_length,
    description_topics: [
      empty == false ? swagger_i18n("string_not_empty") : nil,
      downcase == true ? swagger_i18n("string_downcase") : nil
    ].compact_blank
  }.compact_blank
end

#validate(value) ⇒ Object



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/string.rb', line 15

def validate(value)
  return error_i18n("string") if !value.is_a?(::String)

  value = value.strip if strip
  value = value.downcase if downcase

  if empty == false && value.empty?
    return error_i18n("empty")
  end

  if min_length && value.length < min_length
    return error_i18n("min_length", min_length:)
  end

  if max_length && value.length > max_length
    return error_i18n("max_length", max_length:)
  end

  if format && !format.match?(value)
    return error_i18n("format", regex: format.inspect)
  end

  [ :ok, value ]
end