or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-schemas.mdcoercion.mdcollections.mderrors.mdindex.mdiso-datetime.mdjson-schema.mdlocales.mdnumber-formats.mdparsing.mdprimitives.mdrefinements.mdstring-formats.mdtransformations.mdunions-intersections.mdutilities.mdwrappers.md

number-formats.mddocs/

0

# Number Format Schemas

1

2

Integer and float validators with specific bit-width constraints.

3

4

## Number Formats

5

6

```typescript { .api }

7

// Integer formats

8

function int(params?: { description?: string; errorMap?: ZodErrorMap }): ZodInt;

9

function int32(params?: { description?: string; errorMap?: ZodErrorMap }): ZodInt32;

10

function uint32(params?: { description?: string; errorMap?: ZodErrorMap }): ZodUInt32;

11

function int64(params?: { description?: string; errorMap?: ZodErrorMap }): ZodBigIntFormat;

12

function uint64(params?: { description?: string; errorMap?: ZodErrorMap }): ZodBigIntFormat;

13

14

// Float formats

15

function float32(params?: { description?: string; errorMap?: ZodErrorMap }): ZodFloat32;

16

function float64(params?: { description?: string; errorMap?: ZodErrorMap }): ZodFloat64;

17

```

18

19

## Integer Formats

20

21

```typescript

22

// Generic integer

23

z.int()

24

// Any integer (safe JavaScript integer range)

25

26

// 32-bit signed integer

27

z.int32()

28

// Range: -2,147,483,648 to 2,147,483,647

29

30

// 32-bit unsigned integer

31

z.uint32()

32

// Range: 0 to 4,294,967,295

33

34

// 64-bit signed integer (BigInt)

35

z.int64()

36

// Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

37

38

// 64-bit unsigned integer (BigInt)

39

z.uint64()

40

// Range: 0 to 18,446,744,073,709,551,615

41

```

42

43

**Examples:**

44

```typescript

45

z.int().parse(42); // Valid

46

z.int().parse(3.14); // Invalid (not an integer)

47

48

z.int32().parse(1000000); // Valid

49

z.int32().parse(3000000000); // Invalid (out of range)

50

51

z.uint32().parse(1000000); // Valid

52

z.uint32().parse(-1); // Invalid (must be non-negative)

53

54

z.int64().parse(9007199254740991n); // Valid (BigInt)

55

z.uint64().parse(18446744073709551615n); // Valid (BigInt)

56

```

57

58

## Float Formats

59

60

```typescript

61

// 32-bit float

62

z.float32()

63

// Single precision floating point

64

65

// 64-bit float

66

z.float64()

67

// Double precision floating point

68

```

69

70

**Examples:**

71

```typescript

72

z.float32().parse(3.14); // Valid

73

z.float64().parse(3.14); // Valid

74

```

75

76

## Common Patterns

77

78

```typescript

79

// API with specific number types

80

const APISchema = z.object({

81

id: z.int32(),

82

userId: z.int64(),

83

count: z.uint32(),

84

score: z.float32(),

85

});

86

87

// Database schema

88

const UserSchema = z.object({

89

id: z.int64(), // BIGINT

90

age: z.int32().positive(), // INT

91

balance: z.float64(), // DOUBLE

92

loginCount: z.uint32(), // UNSIGNED INT

93

});

94

95

// Protocol buffers style

96

const MessageSchema = z.object({

97

messageId: z.uint64(),

98

timestamp: z.int64(),

99

flags: z.uint32(),

100

payload: z.string(),

101

});

102

103

// Game state

104

const PlayerSchema = z.object({

105

playerId: z.uint32(),

106

score: z.int32(),

107

health: z.uint32().max(100),

108

position: z.object({

109

x: z.float32(),

110

y: z.float32(),

111

z: z.float32(),

112

}),

113

});

114

115

// Financial data

116

const TransactionSchema = z.object({

117

id: z.int64(),

118

amount: z.float64().positive(),

119

timestamp: z.int64(),

120

accountId: z.uint32(),

121

});

122

```

123

124

## With Additional Constraints

125

126

```typescript

127

// Integer with range

128

z.int32().min(0).max(1000)

129

z.uint32().max(100)

130

131

// Integer with multipleOf

132

z.int().multipleOf(10) // Multiples of 10

133

z.int32().multipleOf(5) // Multiples of 5

134

135

// Float with precision

136

z.float32().multipleOf(0.01) // 2 decimal places

137

z.float64().multipleOf(0.001) // 3 decimal places

138

139

// Combined

140

z.uint32()

141

.min(1)

142

.max(1000)

143

.multipleOf(10)

144

```

145

146

## Type Inference

147

148

```typescript

149

const Int32Schema = z.int32();

150

type Int32Type = z.infer<typeof Int32Schema>; // number

151

152

const Int64Schema = z.int64();

153

type Int64Type = z.infer<typeof Int64Schema>; // bigint

154

155

const Float32Schema = z.float32();

156

type Float32Type = z.infer<typeof Float32Schema>; // number

157

```

158

159

## Comparison

160

161

```typescript

162

// Generic number (any finite number)

163

z.number()

164

165

// Generic integer (any safe integer)

166

z.int()

167

168

// Specific bit-width integers

169

z.int32() // 32-bit signed

170

z.uint32() // 32-bit unsigned

171

z.int64() // 64-bit signed (BigInt)

172

z.uint64() // 64-bit unsigned (BigInt)

173

174

// Specific precision floats

175

z.float32() // Single precision

176

z.float64() // Double precision

177

```

178

179

## When to Use

180

181

- **z.number()**: General numeric validation

182

- **z.int()**: Integer validation without size constraints

183

- **z.int32()**: 32-bit integers (database INT columns, most APIs)

184

- **z.uint32()**: Non-negative 32-bit integers (counts, IDs)

185

- **z.int64()**: Large integers (database BIGINT columns, timestamps)

186

- **z.uint64()**: Large non-negative integers (large IDs, hashes)

187

- **z.float32()**: Single precision (graphics, compact storage)

188

- **z.float64()**: Double precision (financial calculations, precision required)

189