-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
arktype.ts
63 lines (57 loc) · 1.54 KB
/
arktype.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
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
import type { Type } from 'arktype';
import {
type ValidationAdapter,
type RequiredDefaultsOptions,
type Infer,
type InferIn,
createAdapter,
type ClientValidationAdapter,
type ValidationResult,
createJsonSchema
} from './adapters.js';
import { memoize } from '$lib/memoize.js';
async function modules() {
const { type } = await import(/* webpackIgnore: true */ 'arktype');
return { type };
}
const fetchModule = /* @__PURE__ */ memoize(modules);
async function _validate<T extends Type>(
schema: T,
data: unknown
): Promise<ValidationResult<Infer<T>>> {
const { type } = await fetchModule();
const result = schema(data);
if (!(result instanceof type.errors)) {
return {
data: result as Infer<T>,
success: true
};
}
const issues = [];
for (const error of result) {
issues.push({ message: error.message, path: error.path });
}
return {
issues,
success: false
};
}
function _arktype<T extends Type>(
schema: T,
options: RequiredDefaultsOptions<Infer<T>>
): ValidationAdapter<Infer<T>, InferIn<T>> {
return createAdapter({
superFormValidationLibrary: 'arktype',
defaults: options.defaults,
jsonSchema: createJsonSchema(options),
validate: async (data) => _validate(schema, data)
});
}
function _arktypeClient<T extends Type>(schema: T): ClientValidationAdapter<Infer<T>, InferIn<T>> {
return {
superFormValidationLibrary: 'arktype',
validate: async (data) => _validate(schema, data)
};
}
export const arktype = /* @__PURE__ */ memoize(_arktype);
export const arktypeClient = /* @__PURE__ */ memoize(_arktypeClient);