Class: ZodRails::Mapping::TypeMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/zod_rails/mapping/type_mapper.rb

Constant Summary collapse

TYPE_MAP =
{
  string: "z.string()",
  text: "z.string()",
  integer: "z.int()",
  bigint: "z.int()",
  float: "z.number()",
  decimal: "z.string()",
  boolean: "z.boolean()",
  date: "z.iso.date()",
  datetime: "z.iso.datetime({ offset: true })",
  timestamp: "z.iso.datetime({ offset: true })",
  time: "z.iso.datetime({ offset: true })",
  json: "z.json()",
  jsonb: "z.json()",
  uuid: "z.uuid()",
  binary: "z.string()"
}.freeze

Class Method Summary collapse

Class Method Details

.call(type, nullable: false, input_schema: false, array: false, **options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zod_rails/mapping/type_mapper.rb', line 24

def self.call(type, nullable: false, input_schema: false, array: false, **options)
  has_default = options.fetch(:has_default, false)
  element_validation_chain = options.fetch(:element_validation_chain, "")
  base = TYPE_MAP.fetch(type.to_sym) do
    ZodRails.logger.warn("ZodRails: Unknown type '#{type}', falling back to z.unknown()")
    "z.unknown()"
  end
  base = "#{base}#{element_validation_chain}"
  base = "z.array(#{base})" if array

  suffix = determine_suffix(nullable: nullable, input_schema: input_schema, has_default: has_default)
  "#{base}#{suffix}"
end