/
safeParse.ts
36 lines (35 loc) 路 904 Bytes
/
safeParse.ts
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
import { getGlobalConfig } from '../../storages/index.ts';
import type {
BaseIssue,
BaseSchema,
Config,
InferIssue,
} from '../../types/index.ts';
import type { SafeParseResult } from './types.ts';
/**
* Parses an unknown input based on a schema.
*
* @param schema The schema to be used.
* @param input The input to be parsed.
* @param config The parse configuration.
*
* @returns The parse result.
*/
export function safeParse<
const TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>,
>(
schema: TSchema,
input: unknown,
config?: Omit<Config<InferIssue<TSchema>>, 'skipPipe'>
): SafeParseResult<TSchema> {
const dataset = schema._run(
{ typed: false, value: input },
getGlobalConfig(config)
);
return {
typed: dataset.typed,
success: !dataset.issues,
output: dataset.value,
issues: dataset.issues,
} as SafeParseResult<TSchema>;
}