coerce

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package coerce provides constructors that enable automatic type coercion before validation. This allows flexible input types to be automatically converted to the expected schema type.

Supported coercion types:

  • Bool/BoolPtr: strings ("true", "false", "1", "0"), numbers (0, 1), etc. -> bool
  • String/StringPtr: any value -> string representation
  • Int/Int8/.../Int64: strings, floats -> integer types
  • Uint/Uint8/.../Uint64: strings, floats -> unsigned integer types
  • Float32/Float64: strings, integers -> float types
  • BigInt/BigFloat: strings, numbers -> arbitrary precision types
  • Time/Duration: strings (RFC3339, ISO8601) -> time.Time/time.Duration

Example:

s := coerce.Bool()
v, err := s.Parse("true") // v == true, err == nil

n := coerce.Int()
v, err := n.Parse("42")   // v == 42, err == nil

This mirrors the JavaScript Zod API: z.coerce.boolean(), z.coerce.number(), etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigInt

func BigInt(params ...any) *types.ZodBigInt[*big.Int]

BigInt returns a big.Int schema with coercion enabled.

func BigIntPtr

func BigIntPtr(params ...any) *types.ZodBigInt[**big.Int]

BigIntPtr returns a **big.Int schema with coercion enabled.

func Bool

func Bool(params ...any) *types.ZodBool[bool]

Bool returns a boolean schema with coercion enabled.

func BoolPtr

func BoolPtr(params ...any) *types.ZodBool[*bool]

BoolPtr returns a *bool schema with coercion enabled.

func Complex

func Complex(params ...any) *types.ZodComplex[complex128]

Complex returns a complex128 schema with coercion enabled.

func Complex64

func Complex64(params ...any) *types.ZodComplex[complex64]

Complex64 returns a complex64 schema with coercion enabled.

func Complex64Ptr

func Complex64Ptr(params ...any) *types.ZodComplex[*complex64]

Complex64Ptr returns a *complex64 schema with coercion enabled.

func Complex128

func Complex128(params ...any) *types.ZodComplex[complex128]

Complex128 returns a complex128 schema with coercion enabled.

func Complex128Ptr

func Complex128Ptr(params ...any) *types.ZodComplex[*complex128]

Complex128Ptr returns a *complex128 schema with coercion enabled.

func ComplexPtr

func ComplexPtr(params ...any) *types.ZodComplex[*complex128]

ComplexPtr returns a *complex128 schema with coercion enabled.

func Float

func Float(params ...any) *types.ZodFloatTyped[float64, float64]

Float returns a float64 schema with coercion enabled. Unlike Number, Float is a Go-specific alias that maps directly to float64.

func Float32

func Float32(params ...any) *types.ZodFloatTyped[float32, float32]

Float32 returns a float32 schema with coercion enabled.

func Float32Ptr

func Float32Ptr(params ...any) *types.ZodFloatTyped[float32, *float32]

Float32Ptr returns a *float32 schema with coercion enabled.

func Float64

func Float64(params ...any) *types.ZodFloatTyped[float64, float64]

Float64 returns a float64 schema with coercion enabled.

func Float64Ptr

func Float64Ptr(params ...any) *types.ZodFloatTyped[float64, *float64]

Float64Ptr returns a *float64 schema with coercion enabled.

func FloatPtr

func FloatPtr(params ...any) *types.ZodFloatTyped[float64, *float64]

FloatPtr returns a *float64 schema with coercion enabled.

func Int

func Int(params ...any) *types.ZodIntegerTyped[int, int]

Int returns a platform-sized int schema with coercion enabled. For fixed-width int64, use Integer instead.

func Int8

func Int8(params ...any) *types.ZodIntegerTyped[int8, int8]

Int8 returns an int8 schema with coercion enabled.

func Int8Ptr

func Int8Ptr(params ...any) *types.ZodIntegerTyped[int8, *int8]

Int8Ptr returns a *int8 schema with coercion enabled.

func Int16

func Int16(params ...any) *types.ZodIntegerTyped[int16, int16]

Int16 returns an int16 schema with coercion enabled.

func Int16Ptr

func Int16Ptr(params ...any) *types.ZodIntegerTyped[int16, *int16]

Int16Ptr returns a *int16 schema with coercion enabled.

func Int32

func Int32(params ...any) *types.ZodIntegerTyped[int32, int32]

Int32 returns an int32 schema with coercion enabled.

func Int32Ptr

func Int32Ptr(params ...any) *types.ZodIntegerTyped[int32, *int32]

Int32Ptr returns a *int32 schema with coercion enabled.

func Int64

func Int64(params ...any) *types.ZodIntegerTyped[int64, int64]

Int64 returns an int64 schema with coercion enabled.

func Int64Ptr

func Int64Ptr(params ...any) *types.ZodIntegerTyped[int64, *int64]

Int64Ptr returns a *int64 schema with coercion enabled.

func IntPtr

func IntPtr(params ...any) *types.ZodIntegerTyped[int, *int]

IntPtr returns a *int schema with coercion enabled.

func Integer

func Integer(params ...any) *types.ZodIntegerTyped[int64, int64]

Integer returns an int64 schema with coercion enabled. For platform-sized int, use Int instead.

func IntegerPtr

func IntegerPtr(params ...any) *types.ZodIntegerTyped[int64, *int64]

IntegerPtr returns a *int64 schema with coercion enabled.

func Number

func Number(params ...any) *types.ZodFloatTyped[float64, float64]

Number returns a float64 schema with coercion enabled. It is the Go equivalent of Zod's z.coerce.number().

func NumberPtr

func NumberPtr(params ...any) *types.ZodFloatTyped[float64, *float64]

NumberPtr returns a *number schema with coercion enabled.

func String

func String(params ...any) *types.ZodString[string]

String returns a string schema with coercion enabled.

func StringBool

func StringBool(params ...any) *types.ZodStringBool[bool]

StringBool returns a bool schema with string-to-boolean coercion enabled.

func StringBoolPtr

func StringBoolPtr(params ...any) *types.ZodStringBool[*bool]

StringBoolPtr returns a *bool schema with string-to-boolean coercion enabled.

func StringPtr

func StringPtr(params ...any) *types.ZodString[*string]

StringPtr returns a *string schema with coercion enabled.

func Time

func Time(params ...any) *types.ZodTime[time.Time]

Time returns a time.Time schema with coercion enabled.

func TimePtr

func TimePtr(params ...any) *types.ZodTime[*time.Time]

TimePtr returns a *time.Time schema with coercion enabled.

func Uint

func Uint(params ...any) *types.ZodIntegerTyped[uint, uint]

Uint returns a uint schema with coercion enabled.

func Uint8

func Uint8(params ...any) *types.ZodIntegerTyped[uint8, uint8]

Uint8 returns a uint8 schema with coercion enabled.

func Uint8Ptr

func Uint8Ptr(params ...any) *types.ZodIntegerTyped[uint8, *uint8]

Uint8Ptr returns a *uint8 schema with coercion enabled.

func Uint16

func Uint16(params ...any) *types.ZodIntegerTyped[uint16, uint16]

Uint16 returns a uint16 schema with coercion enabled.

func Uint16Ptr

func Uint16Ptr(params ...any) *types.ZodIntegerTyped[uint16, *uint16]

Uint16Ptr returns a *uint16 schema with coercion enabled.

func Uint32

func Uint32(params ...any) *types.ZodIntegerTyped[uint32, uint32]

Uint32 returns a uint32 schema with coercion enabled.

func Uint32Ptr

func Uint32Ptr(params ...any) *types.ZodIntegerTyped[uint32, *uint32]

Uint32Ptr returns a *uint32 schema with coercion enabled.

func Uint64

func Uint64(params ...any) *types.ZodIntegerTyped[uint64, uint64]

Uint64 returns a uint64 schema with coercion enabled.

func Uint64Ptr

func Uint64Ptr(params ...any) *types.ZodIntegerTyped[uint64, *uint64]

Uint64Ptr returns a *uint64 schema with coercion enabled.

func UintPtr

func UintPtr(params ...any) *types.ZodIntegerTyped[uint, *uint]

UintPtr returns a *uint schema with coercion enabled.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL