-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
38 lines (36 loc) · 1.23 KB
/
parse.ts
File metadata and controls
38 lines (36 loc) · 1.23 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
/**
* @fileoverview Throwing twin of `validateSchema`.
*
* Use `parseSchema(schema, data)` for fail-fast trust boundaries (app
* startup, config files, internal assertions). Use the non-throwing
* `validateSchema` for recoverable input (form fields, API request bodies,
* anywhere errors need to surface to a user).
*
* @example
* ```ts
* import { z } from 'zod'
* import { parseSchema } from '@socketsecurity/lib/schema/parse'
*
* const Config = z.object({ host: z.string(), port: z.number() })
* const config = parseSchema(Config, json) // throws on invalid
* ```
*/
import { validateSchema } from './validate'
import type { Infer } from './types'
/**
* Parse `data` against `schema` and return the validated value.
*
* @throws {Error} When validation fails. The message lists all issues as
* `path: message, path: message, ...`. Use `validateSchema` if you need
* structured access to the error list.
*/
export function parseSchema<S>(schema: S, data: unknown): Infer<S> {
const result = validateSchema(schema, data)
if (result.ok) {
return result.value
}
const summary = result.errors
.map(e => `${e.path.join('.') || '(root)'}: ${e.message}`)
.join(', ')
throw new Error(`Validation failed: ${summary}`)
}