-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
139 lines (130 loc) · 3.5 KB
/
types.ts
File metadata and controls
139 lines (130 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @file Shared types for schema validation. `Schema<T>` is the Zod-shaped
* duck-type contract — any validator with a `.safeParse(data)` method
* returning `{ success, data?, error? }` satisfies it. socket-lib detects Zod
* (v3 and v4) structurally via this interface; consumers bring their own Zod.
* `ValidateResult<T>` / `ValidationIssue` / `Infer<S>` / `AnySchema` are the
* normalized shapes produced by `@socketsecurity/lib/schema/validate` and
* `@socketsecurity/lib/schema/parse`.
*/
/**
* Result of a Zod-shaped schema's `.safeParse()` call.
*
* @template T - The expected type of the parsed data.
*/
export interface ParseResult<T> {
/**
* Indicates whether parsing was successful.
*/
success: boolean
/**
* Parsed and validated data (only present when `success` is `true`)
*/
data?: T | undefined
/**
* Error information (only present when `success` is `false`)
*/
error?: unknown
}
/**
* Zod-shaped duck-type for any validator exposing `safeParse` / `parse`.
*
* @example
* ;```ts
* import { z } from 'zod'
*
* const userSchema = z.object({ name: z.string(), age: z.number() })
*
* // Schema satisfies this interface
* const schema: Schema<User> = userSchema
* const result = schema.safeParse({ name: 'Alice', age: 30 })
* ```
*
* @template T - The expected output type after validation.
*/
export interface Schema<T = unknown> {
/**
* Non-throwing parse.
*/
safeParse(data: unknown): ParseResult<T>
/**
* Throwing parse.
*/
parse(data: unknown): T
/**
* Optional schema name for debugging.
*/
_name?: string | undefined
}
/**
* Internal structural shape of a Zod v4 schema — carries the inferred output
* type on `_zod.output`. Used for type-only detection in `Infer<S>`.
*
* @internal
*/
interface ZodV4LikeSchema<O = unknown> {
_zod: { output: O }
safeParse(data: unknown): unknown
}
/**
* Internal structural shape of a Zod v3 schema — carries the inferred output
* type on `_output`. Used for type-only detection in `Infer<S>`.
*
* @internal
*/
interface ZodV3LikeSchema<O = unknown> {
_output: O
safeParse(data: unknown): unknown
}
/**
* Internal structural shape of a TypeBox `TSchema` — carries the inferred
* output type on the phantom `static` field. Only used inside socket-lib for
* type-only detection in `Infer<S>`; external callers should pass Zod schemas.
*
* @internal
*/
interface TypeBoxLikeSchema {
static: unknown
}
/**
* Any schema kind the validators accept.
*/
export type AnySchema =
| ZodV4LikeSchema<unknown>
| ZodV3LikeSchema<unknown>
| TypeBoxLikeSchema
| Schema<unknown>
/**
* Infer the validated output type from any supported schema kind.
*
* Order matters: TypeBox schemas carry a phantom `static` field, so we check
* for TypeBox before falling through to Zod and the duck-type.
*/
export type Infer<S> = S extends { static: infer Static }
? Static
: S extends { _zod: { output: infer O } }
? O
: S extends { _output: infer O }
? O
: S extends Schema<infer T>
? T
: unknown
/**
* A single normalized validation error.
*/
export interface ValidationIssue {
/**
* Array path into the value (e.g. `['user', 'age']`).
*/
path: Array<string | number>
/**
* Human-readable description of the failure.
*/
message: string
}
/**
* Tagged-union result of `validateSchema`. Callers narrow on `ok`.
*/
export type ValidateResult<T> =
| { ok: true; value: T }
| { ok: false; errors: ValidationIssue[] }