@orpc/zod 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +144 -0
- package/dist/src/index.d.ts +31 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +46 -0
- package/src/index.ts +243 -0
package/dist/index.js
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
import wcmatch from "wildcard-match";
|
2
|
+
import { custom } from "zod";
|
3
|
+
const customZodTypeSymbol = Symbol("customZodTypeSymbol");
|
4
|
+
const customZodFileMimeTypeSymbol = Symbol("customZodFileMimeTypeSymbol");
|
5
|
+
const CUSTOM_JSON_SCHEMA_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA");
|
6
|
+
const CUSTOM_JSON_SCHEMA_INPUT_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA_INPUT");
|
7
|
+
const CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA_OUTPUT");
|
8
|
+
function getCustomZodType(def) {
|
9
|
+
return customZodTypeSymbol in def ? def[customZodTypeSymbol] : void 0;
|
10
|
+
}
|
11
|
+
function getCustomZodFileMimeType(def) {
|
12
|
+
return customZodFileMimeTypeSymbol in def ? def[customZodFileMimeTypeSymbol] : void 0;
|
13
|
+
}
|
14
|
+
function getCustomJSONSchema(def, options) {
|
15
|
+
if ((options == null ? void 0 : options.mode) === "input" && CUSTOM_JSON_SCHEMA_INPUT_SYMBOL in def) {
|
16
|
+
return def[CUSTOM_JSON_SCHEMA_INPUT_SYMBOL];
|
17
|
+
}
|
18
|
+
if ((options == null ? void 0 : options.mode) === "output" && CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL in def) {
|
19
|
+
return def[CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL];
|
20
|
+
}
|
21
|
+
if (CUSTOM_JSON_SCHEMA_SYMBOL in def) {
|
22
|
+
return def[CUSTOM_JSON_SCHEMA_SYMBOL];
|
23
|
+
}
|
24
|
+
return void 0;
|
25
|
+
}
|
26
|
+
function composeParams(options) {
|
27
|
+
return (val) => {
|
28
|
+
const defaultMessage = typeof options.defaultMessage === "function" ? options.defaultMessage(val) : options.defaultMessage;
|
29
|
+
if (!options.params) {
|
30
|
+
return {
|
31
|
+
message: defaultMessage
|
32
|
+
};
|
33
|
+
}
|
34
|
+
if (typeof options.params === "function") {
|
35
|
+
return {
|
36
|
+
message: defaultMessage,
|
37
|
+
...options.params(val)
|
38
|
+
};
|
39
|
+
}
|
40
|
+
if (typeof options.params === "object") {
|
41
|
+
return {
|
42
|
+
message: defaultMessage,
|
43
|
+
...options.params
|
44
|
+
};
|
45
|
+
}
|
46
|
+
return {
|
47
|
+
message: options.params
|
48
|
+
};
|
49
|
+
};
|
50
|
+
}
|
51
|
+
function file(params) {
|
52
|
+
const schema = custom(
|
53
|
+
(val) => val instanceof File,
|
54
|
+
composeParams({ params, defaultMessage: "Input is not a file" })
|
55
|
+
);
|
56
|
+
Object.assign(schema._def, {
|
57
|
+
[customZodTypeSymbol]: "File"
|
58
|
+
});
|
59
|
+
return Object.assign(schema, {
|
60
|
+
type: (mimeType, params2) => {
|
61
|
+
const isMatch = wcmatch(mimeType);
|
62
|
+
const refinedSchema = schema.refine(
|
63
|
+
(val) => isMatch(val.type.split(";")[0]),
|
64
|
+
composeParams({
|
65
|
+
params: params2,
|
66
|
+
defaultMessage: (val) => `Expected a file of type ${mimeType} but got a file of type ${val.type || "unknown"}`
|
67
|
+
})
|
68
|
+
);
|
69
|
+
Object.assign(refinedSchema._def, {
|
70
|
+
[customZodTypeSymbol]: "File",
|
71
|
+
[customZodFileMimeTypeSymbol]: mimeType
|
72
|
+
});
|
73
|
+
return refinedSchema;
|
74
|
+
}
|
75
|
+
});
|
76
|
+
}
|
77
|
+
function blob(params) {
|
78
|
+
const schema = custom(
|
79
|
+
(val) => val instanceof Blob,
|
80
|
+
composeParams({ params, defaultMessage: "Input is not a blob" })
|
81
|
+
);
|
82
|
+
Object.assign(schema._def, {
|
83
|
+
[customZodTypeSymbol]: "Blob"
|
84
|
+
});
|
85
|
+
return schema;
|
86
|
+
}
|
87
|
+
function invalidDate(params) {
|
88
|
+
const schema = custom(
|
89
|
+
(val) => val instanceof Date && Number.isNaN(val.getTime()),
|
90
|
+
composeParams({ params, defaultMessage: "Input is not an invalid date" })
|
91
|
+
);
|
92
|
+
Object.assign(schema._def, {
|
93
|
+
[customZodTypeSymbol]: "Invalid Date"
|
94
|
+
});
|
95
|
+
return schema;
|
96
|
+
}
|
97
|
+
function regexp(options) {
|
98
|
+
const schema = custom(
|
99
|
+
(val) => val instanceof RegExp,
|
100
|
+
composeParams({ params: options, defaultMessage: "Input is not a regexp" })
|
101
|
+
);
|
102
|
+
Object.assign(schema._def, {
|
103
|
+
[customZodTypeSymbol]: "RegExp"
|
104
|
+
});
|
105
|
+
return schema;
|
106
|
+
}
|
107
|
+
function url(options) {
|
108
|
+
const schema = custom(
|
109
|
+
(val) => val instanceof URL,
|
110
|
+
composeParams({ params: options, defaultMessage: "Input is not a URL" })
|
111
|
+
);
|
112
|
+
Object.assign(schema._def, {
|
113
|
+
[customZodTypeSymbol]: "URL"
|
114
|
+
});
|
115
|
+
return schema;
|
116
|
+
}
|
117
|
+
function openapi(schema, custom2, options) {
|
118
|
+
const newSchema = schema.refine(() => true);
|
119
|
+
const SYMBOL = (options == null ? void 0 : options.mode) === "input" ? CUSTOM_JSON_SCHEMA_INPUT_SYMBOL : (options == null ? void 0 : options.mode) === "output" ? CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL : CUSTOM_JSON_SCHEMA_SYMBOL;
|
120
|
+
Object.assign(newSchema._def, {
|
121
|
+
[SYMBOL]: custom2
|
122
|
+
});
|
123
|
+
return newSchema;
|
124
|
+
}
|
125
|
+
const oz = {
|
126
|
+
openapi,
|
127
|
+
file,
|
128
|
+
blob,
|
129
|
+
invalidDate,
|
130
|
+
regexp,
|
131
|
+
url
|
132
|
+
};
|
133
|
+
export {
|
134
|
+
blob,
|
135
|
+
file,
|
136
|
+
getCustomJSONSchema,
|
137
|
+
getCustomZodFileMimeType,
|
138
|
+
getCustomZodType,
|
139
|
+
invalidDate,
|
140
|
+
openapi,
|
141
|
+
oz,
|
142
|
+
regexp,
|
143
|
+
url
|
144
|
+
};
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
2
|
+
import { type CustomErrorParams, type ZodEffects, type ZodType, type ZodTypeAny, type ZodTypeDef, type input, type output } from 'zod';
|
3
|
+
export type CustomZodType = 'File' | 'Blob' | 'Invalid Date' | 'RegExp' | 'URL';
|
4
|
+
type CustomParams = CustomErrorParams & {
|
5
|
+
fatal?: boolean;
|
6
|
+
};
|
7
|
+
export declare function getCustomZodType(def: ZodTypeDef): CustomZodType | undefined;
|
8
|
+
export declare function getCustomZodFileMimeType(def: ZodTypeDef): string | undefined;
|
9
|
+
export declare function getCustomJSONSchema(def: ZodTypeDef, options?: {
|
10
|
+
mode?: 'input' | 'output';
|
11
|
+
}): Exclude<JSONSchema, boolean> | undefined;
|
12
|
+
export declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>> & {
|
13
|
+
type(mimeType: string, params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodEffects<ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>>, InstanceType<typeof File>, InstanceType<typeof File>>;
|
14
|
+
};
|
15
|
+
export declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof Blob>, ZodTypeDef, InstanceType<typeof Blob>>;
|
16
|
+
export declare function invalidDate(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Date, ZodTypeDef, Date>;
|
17
|
+
export declare function regexp(options?: CustomParams): ZodType<RegExp, ZodTypeDef, RegExp>;
|
18
|
+
export declare function url(options?: CustomParams): ZodType<URL, ZodTypeDef, URL>;
|
19
|
+
export declare function openapi<T extends ZodTypeAny, TMode extends 'input' | 'output' | 'both' = 'both'>(schema: T, custom: Exclude<JSONSchema<TMode extends 'input' ? input<T> : TMode extends 'output' ? output<T> : input<T> & output<T>>, boolean>, options?: {
|
20
|
+
mode: TMode;
|
21
|
+
}): ReturnType<T['refine']>;
|
22
|
+
export declare const oz: {
|
23
|
+
openapi: typeof openapi;
|
24
|
+
file: typeof file;
|
25
|
+
blob: typeof blob;
|
26
|
+
invalidDate: typeof invalidDate;
|
27
|
+
regexp: typeof regexp;
|
28
|
+
url: typeof url;
|
29
|
+
};
|
30
|
+
export {};
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAGjE,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,UAAU,EAEf,KAAK,KAAK,EACV,KAAK,MAAM,EACZ,MAAM,KAAK,CAAA;AAEZ,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,cAAc,GAAG,QAAQ,GAAG,KAAK,CAAA;AAU/E,KAAK,YAAY,GAAG,iBAAiB,GAAG;IACtC,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,UAAU,GAAG,aAAa,GAAG,SAAS,CAI3E;AAED,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAI5E;AAED,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,UAAU,EACf,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAA;CAAE,GACtC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG,SAAS,CAc1C;AAsCD,wBAAgB,IAAI,CAClB,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,YAAY,CAAC,GAClE,OAAO,CAAC,YAAY,CAAC,OAAO,IAAI,CAAC,EAAE,UAAU,EAAE,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG;IAC7E,IAAI,CACF,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,YAAY,CAAC,GAClE,UAAU,CACX,OAAO,CAAC,YAAY,CAAC,OAAO,IAAI,CAAC,EAAE,UAAU,EAAE,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC,EACzE,YAAY,CAAC,OAAO,IAAI,CAAC,EACzB,YAAY,CAAC,OAAO,IAAI,CAAC,CAC1B,CAAA;CACF,CAkCA;AAED,wBAAgB,IAAI,CAClB,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,YAAY,CAAC,GAClE,OAAO,CAAC,YAAY,CAAC,OAAO,IAAI,CAAC,EAAE,UAAU,EAAE,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC,CAW3E;AAED,wBAAgB,WAAW,CACzB,MAAM,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,YAAY,CAAC,GAClE,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,CAWjC;AAED,wBAAgB,MAAM,CACpB,OAAO,CAAC,EAAE,YAAY,GACrB,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,CAWrC;AAED,wBAAgB,GAAG,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,CAAC,CAWzE;AAED,wBAAgB,OAAO,CACrB,CAAC,SAAS,UAAU,EACpB,KAAK,SAAS,OAAO,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,EAElD,MAAM,EAAE,CAAC,EACT,MAAM,EAAE,OAAO,CACb,UAAU,CACR,KAAK,SAAS,OAAO,GACjB,KAAK,CAAC,CAAC,CAAC,GACR,KAAK,SAAS,QAAQ,GACpB,MAAM,CAAC,CAAC,CAAC,GACT,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAC3B,EACD,OAAO,CACR,EACD,OAAO,CAAC,EAAE;IAAE,IAAI,EAAE,KAAK,CAAA;CAAE,GACxB,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAezB;AAED,eAAO,MAAM,EAAE;;;;;;;CAOd,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"fileNames":["../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es5.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2016.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2018.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2019.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2021.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.dom.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.core.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2016.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.date.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.object.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.string.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2019.array.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2019.object.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2019.string.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2019.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.date.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.string.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2020.number.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2021.promise.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2021.string.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2021.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.array.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.error.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.intl.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.object.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.string.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.es2022.regexp.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.decorators.d.ts","../../../node_modules/.pnpm/typescript@5.7.1-rc/node_modules/typescript/lib/lib.decorators.legacy.d.ts","../../../node_modules/.pnpm/@types+react@18.3.12/node_modules/@types/react/global.d.ts","../../../node_modules/.pnpm/csstype@3.1.3/node_modules/csstype/index.d.ts","../../../node_modules/.pnpm/@types+prop-types@15.7.13/node_modules/@types/prop-types/index.d.ts","../../../node_modules/.pnpm/@types+react@18.3.12/node_modules/@types/react/index.d.ts","../../../node_modules/.pnpm/@types+react@18.3.12/node_modules/@types/react/jsx-runtime.d.ts","../../../node_modules/.pnpm/json-schema-typed@8.0.1/node_modules/json-schema-typed/draft-2020-12.ts","../../../node_modules/.pnpm/wildcard-match@5.1.3/node_modules/wildcard-match/build/index.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/helpers/typeAliases.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/helpers/util.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/ZodError.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/locales/en.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/errors.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/helpers/parseUtil.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/helpers/enumUtil.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/helpers/errorUtil.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/helpers/partialUtil.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/types.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/external.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/lib/index.d.ts","../../../node_modules/.pnpm/zod@3.23.8/node_modules/zod/index.d.ts","../src/index.ts"],"fileIdsList":[[59,60,61],[62],[63],[77],[66,67,77],[68,69],[66,67,68,70,71,75],[67,68],[76],[68],[66,67,68,71,72,73,74],[63,64,65,78]],"fileInfos":[{"version":"e41c290ef7dd7dab3493e6cbe5909e0148edf4a8dad0271be08edec368a0f7b9","affectsGlobalScope":true,"impliedFormat":1},{"version":"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","impliedFormat":1},{"version":"3facaf05f0c5fc569c5649dd359892c98a85557e3e0c847964caeb67076f4d75","impliedFormat":1},{"version":"e44bb8bbac7f10ecc786703fe0a6a4b952189f908707980ba8f3c8975a760962","impliedFormat":1},{"version":"5e1c4c362065a6b95ff952c0eab010f04dcd2c3494e813b493ecfd4fcb9fc0d8","impliedFormat":1},{"version":"68d73b4a11549f9c0b7d352d10e91e5dca8faa3322bfb77b661839c42b1ddec7","impliedFormat":1},{"version":"5efce4fc3c29ea84e8928f97adec086e3dc876365e0982cc8479a07954a3efd4","impliedFormat":1},{"version":"feecb1be483ed332fad555aff858affd90a48ab19ba7272ee084704eb7167569","impliedFormat":1},{"version":"ee7bad0c15b58988daa84371e0b89d313b762ab83cb5b31b8a2d1162e8eb41c2","impliedFormat":1},{"version":"4fd3f3422b2d2a3dfd5cdd0f387b3a8ec45f006c6ea896a4cb41264c2100bb2c","affectsGlobalScope":true,"impliedFormat":1},{"version":"c57796738e7f83dbc4b8e65132f11a377649c00dd3eee333f672b8f0a6bea671","affectsGlobalScope":true,"impliedFormat":1},{"version":"dc2df20b1bcdc8c2d34af4926e2c3ab15ffe1160a63e58b7e09833f616efff44","affectsGlobalScope":true,"impliedFormat":1},{"version":"515d0b7b9bea2e31ea4ec968e9edd2c39d3eebf4a2d5cbd04e88639819ae3b71","affectsGlobalScope":true,"impliedFormat":1},{"version":"62bb211266ee48b2d0edf0d8d1b191f0c24fc379a82bd4c1692a082c540bc6b1","affectsGlobalScope":true,"impliedFormat":1},{"version":"0dc1e7ceda9b8b9b455c3a2d67b0412feab00bd2f66656cd8850e8831b08b537","affectsGlobalScope":true,"impliedFormat":1},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true,"impliedFormat":1},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true,"impliedFormat":1},{"version":"4ff2a353abf8a80ee399af572debb8faab2d33ad38c4b4474cff7f26e7653b8d","affectsGlobalScope":true,"impliedFormat":1},{"version":"936e80ad36a2ee83fc3caf008e7c4c5afe45b3cf3d5c24408f039c1d47bdc1df","affectsGlobalScope":true,"impliedFormat":1},{"version":"d15bea3d62cbbdb9797079416b8ac375ae99162a7fba5de2c6c505446486ac0a","affectsGlobalScope":true,"impliedFormat":1},{"version":"68d18b664c9d32a7336a70235958b8997ebc1c3b8505f4f1ae2b7e7753b87618","affectsGlobalScope":true,"impliedFormat":1},{"version":"eb3d66c8327153d8fa7dd03f9c58d351107fe824c79e9b56b462935176cdf12a","affectsGlobalScope":true,"impliedFormat":1},{"version":"38f0219c9e23c915ef9790ab1d680440d95419ad264816fa15009a8851e79119","affectsGlobalScope":true,"impliedFormat":1},{"version":"69ab18c3b76cd9b1be3d188eaf8bba06112ebbe2f47f6c322b5105a6fbc45a2e","affectsGlobalScope":true,"impliedFormat":1},{"version":"fef8cfad2e2dc5f5b3d97a6f4f2e92848eb1b88e897bb7318cef0e2820bceaab","affectsGlobalScope":true,"impliedFormat":1},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true,"impliedFormat":1},{"version":"4de680d5bb41c17f7f68e0419412ca23c98d5749dcaaea1896172f06435891fc","affectsGlobalScope":true,"impliedFormat":1},{"version":"954296b30da6d508a104a3a0b5d96b76495c709785c1d11610908e63481ee667","affectsGlobalScope":true,"impliedFormat":1},{"version":"ac9538681b19688c8eae65811b329d3744af679e0bdfa5d842d0e32524c73e1c","affectsGlobalScope":true,"impliedFormat":1},{"version":"0a969edff4bd52585473d24995c5ef223f6652d6ef46193309b3921d65dd4376","affectsGlobalScope":true,"impliedFormat":1},{"version":"9e9fbd7030c440b33d021da145d3232984c8bb7916f277e8ffd3dc2e3eae2bdb","affectsGlobalScope":true,"impliedFormat":1},{"version":"811ec78f7fefcabbda4bfa93b3eb67d9ae166ef95f9bff989d964061cbf81a0c","affectsGlobalScope":true,"impliedFormat":1},{"version":"717937616a17072082152a2ef351cb51f98802fb4b2fdabd32399843875974ca","affectsGlobalScope":true,"impliedFormat":1},{"version":"d7e7d9b7b50e5f22c915b525acc5a49a7a6584cf8f62d0569e557c5cfc4b2ac2","affectsGlobalScope":true,"impliedFormat":1},{"version":"71c37f4c9543f31dfced6c7840e068c5a5aacb7b89111a4364b1d5276b852557","affectsGlobalScope":true,"impliedFormat":1},{"version":"576711e016cf4f1804676043e6a0a5414252560eb57de9faceee34d79798c850","affectsGlobalScope":true,"impliedFormat":1},{"version":"89c1b1281ba7b8a96efc676b11b264de7a8374c5ea1e6617f11880a13fc56dc6","affectsGlobalScope":true,"impliedFormat":1},{"version":"74f7fa2d027d5b33eb0471c8e82a6c87216223181ec31247c357a3e8e2fddc5b","affectsGlobalScope":true,"impliedFormat":1},{"version":"f1e2a172204962276504466a6393426d2ca9c54894b1ad0a6c9dad867a65f876","affectsGlobalScope":true,"impliedFormat":1},{"version":"063600664504610fe3e99b717a1223f8b1900087fab0b4cad1496a114744f8df","affectsGlobalScope":true,"impliedFormat":1},{"version":"934019d7e3c81950f9a8426d093458b65d5aff2c7c1511233c0fd5b941e608ab","affectsGlobalScope":true,"impliedFormat":1},{"version":"52ada8e0b6e0482b728070b7639ee42e83a9b1c22d205992756fe020fd9f4a47","affectsGlobalScope":true,"impliedFormat":1},{"version":"3bdefe1bfd4d6dee0e26f928f93ccc128f1b64d5d501ff4a8cf3c6371200e5e6","affectsGlobalScope":true,"impliedFormat":1},{"version":"59fb2c069260b4ba00b5643b907ef5d5341b167e7d1dbf58dfd895658bda2867","affectsGlobalScope":true,"impliedFormat":1},{"version":"639e512c0dfc3fad96a84caad71b8834d66329a1f28dc95e3946c9b58176c73a","affectsGlobalScope":true,"impliedFormat":1},{"version":"368af93f74c9c932edd84c58883e736c9e3d53cec1fe24c0b0ff451f529ceab1","affectsGlobalScope":true,"impliedFormat":1},{"version":"af3dd424cf267428f30ccfc376f47a2c0114546b55c44d8c0f1d57d841e28d74","affectsGlobalScope":true,"impliedFormat":1},{"version":"995c005ab91a498455ea8dfb63aa9f83fa2ea793c3d8aa344be4a1678d06d399","affectsGlobalScope":true,"impliedFormat":1},{"version":"959d36cddf5e7d572a65045b876f2956c973a586da58e5d26cde519184fd9b8a","affectsGlobalScope":true,"impliedFormat":1},{"version":"965f36eae237dd74e6cca203a43e9ca801ce38824ead814728a2807b1910117d","affectsGlobalScope":true,"impliedFormat":1},{"version":"3925a6c820dcb1a06506c90b1577db1fdbf7705d65b62b99dce4be75c637e26b","affectsGlobalScope":true,"impliedFormat":1},{"version":"0a3d63ef2b853447ec4f749d3f368ce642264246e02911fcb1590d8c161b8005","affectsGlobalScope":true,"impliedFormat":1},{"version":"b5ce7a470bc3628408429040c4e3a53a27755022a32fd05e2cb694e7015386c7","affectsGlobalScope":true,"impliedFormat":1},{"version":"8444af78980e3b20b49324f4a16ba35024fef3ee069a0eb67616ea6ca821c47a","affectsGlobalScope":true,"impliedFormat":1},{"version":"3287d9d085fbd618c3971944b65b4be57859f5415f495b33a6adc994edd2f004","affectsGlobalScope":true,"impliedFormat":1},{"version":"b4b67b1a91182421f5df999988c690f14d813b9850b40acd06ed44691f6727ad","affectsGlobalScope":true,"impliedFormat":1},{"version":"8e7f8264d0fb4c5339605a15daadb037bf238c10b654bb3eee14208f860a32ea","affectsGlobalScope":true,"impliedFormat":1},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true,"impliedFormat":1},{"version":"36a2e4c9a67439aca5f91bb304611d5ae6e20d420503e96c230cf8fcdc948d94","affectsGlobalScope":true,"impliedFormat":1},{"version":"8a8eb4ebffd85e589a1cc7c178e291626c359543403d58c9cd22b81fab5b1fb9","impliedFormat":1},{"version":"ed6b820c54de95b2510bb673490d61c7f2187f532a339d8d04981645a918961f","impliedFormat":1},{"version":"aa17748c522bd586f8712b1a308ea23af59c309b2fd278f6d4f406647c72e659","affectsGlobalScope":true,"impliedFormat":1},{"version":"42c169fb8c2d42f4f668c624a9a11e719d5d07dacbebb63cbcf7ef365b0a75b3","impliedFormat":1},{"version":"126783a400a891fce19c88533874b41073eb898725cb2a86a878e3b18e4e71f8","impliedFormat":99},{"version":"30b2bd1237547fbccd267bd8680d721dce8c71bd2f156d6987d599f9098e15d5","impliedFormat":1},{"version":"5487b97cfa28b26b4a9ef0770f872bdbebd4c46124858de00f242c3eed7519f4","impliedFormat":1},{"version":"c2869c4f2f79fd2d03278a68ce7c061a5a8f4aed59efb655e25fe502e3e471d5","impliedFormat":1},{"version":"b8fe42dbf4b0efba2eb4dbfb2b95a3712676717ff8469767dc439e75d0c1a3b6","impliedFormat":1},{"version":"8485b6da53ec35637d072e516631d25dae53984500de70a6989058f24354666f","impliedFormat":1},{"version":"ebe80346928736532e4a822154eb77f57ef3389dbe2b3ba4e571366a15448ef2","impliedFormat":1},{"version":"83306c97a4643d78420f082547ea0d488a0d134c922c8e65fc0b4f08ef66d92b","impliedFormat":1},{"version":"f672c876c1a04a223cf2023b3d91e8a52bb1544c576b81bf64a8fec82be9969c","impliedFormat":1},{"version":"98a9cc18f661d28e6bd31c436e1984f3980f35e0f0aa9cf795c54f8ccb667ffe","impliedFormat":1},{"version":"c76b0c5727302341d0bdfa2cc2cee4b19ff185b554edb6e8543f0661d8487116","impliedFormat":1},{"version":"dccd26a5c85325a011aff40f401e0892bd0688d44132ba79e803c67e68fffea5","impliedFormat":1},{"version":"f5ef066942e4f0bd98200aa6a6694b831e73200c9b3ade77ad0aa2409e8fe1b1","impliedFormat":1},{"version":"b9e99cd94f4166a245f5158f7286c05406e2a4c694619bceb7a4f3519d1d768e","impliedFormat":1},{"version":"5568d7c32e5cf5f35e092649f4e5e168c3114c800b1d7545b7ae5e0415704802","impliedFormat":1},{"version":"decac2208d94b186b406a6b25605800165bbe4bc8371a5cd4f3db7c8c9219bda","signature":"c7a59d81b4f5df60cfce1aaabb0fd1c5fe92dce23bfceacc0096786fbebb16f8"}],"root":[79],"options":{"composite":true,"declaration":true,"declarationMap":true,"emitDeclarationOnly":true,"esModuleInterop":true,"jsx":4,"module":7,"noImplicitOverride":true,"noUncheckedIndexedAccess":true,"noUncheckedSideEffectImports":true,"outDir":"./","skipLibCheck":true,"strict":true,"target":9,"useDefineForClassFields":true},"referencedMap":[[62,1],[63,2],[64,3],[78,4],[68,5],[70,6],[76,7],[71,8],[74,4],[77,9],[69,10],[75,11],[79,12]],"semanticDiagnosticsPerFile":[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],"latestChangedDtsFile":"./src/index.d.ts","checkPending":true,"version":"5.7.1-rc"}
|
package/package.json
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
{
|
2
|
+
"name": "@orpc/zod",
|
3
|
+
"type": "module",
|
4
|
+
"version": "0.0.0",
|
5
|
+
"author": {
|
6
|
+
"name": "dinwwwh",
|
7
|
+
"email": "dinwwwh@gmail.com",
|
8
|
+
"url": "https://dinwwwh.com"
|
9
|
+
},
|
10
|
+
"license": "MIT",
|
11
|
+
"homepage": "https://github.com/dinwwwh/dinwwwh",
|
12
|
+
"repository": {
|
13
|
+
"type": "git",
|
14
|
+
"url": "https://github.com/dinwwwh/dinwwwh.git",
|
15
|
+
"directory": "examples/typescript-vite-package"
|
16
|
+
},
|
17
|
+
"keywords": [
|
18
|
+
"dinwwwh"
|
19
|
+
],
|
20
|
+
"publishConfig": {
|
21
|
+
"access": "public"
|
22
|
+
},
|
23
|
+
"exports": {
|
24
|
+
".": {
|
25
|
+
"types": "./dist/src/index.d.ts",
|
26
|
+
"import": "./dist/index.js",
|
27
|
+
"default": "./dist/index.js"
|
28
|
+
},
|
29
|
+
"./🔒/*": {
|
30
|
+
"types": "./dist/src/*.d.ts"
|
31
|
+
}
|
32
|
+
},
|
33
|
+
"files": [
|
34
|
+
"dist",
|
35
|
+
"src"
|
36
|
+
],
|
37
|
+
"dependencies": {
|
38
|
+
"json-schema-typed": "^8.0.1",
|
39
|
+
"wildcard-match": "^5.1.3",
|
40
|
+
"zod": "^3.23.8"
|
41
|
+
},
|
42
|
+
"scripts": {
|
43
|
+
"build": "UNPLUGIN_ON_SUCCESS='tsc -b --noCheck' vite build",
|
44
|
+
"check": "tsc -b"
|
45
|
+
}
|
46
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
/// <reference lib="dom" />
|
2
|
+
|
3
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
|
4
|
+
|
5
|
+
import wcmatch from 'wildcard-match'
|
6
|
+
import {
|
7
|
+
type CustomErrorParams,
|
8
|
+
type ZodEffects,
|
9
|
+
type ZodType,
|
10
|
+
type ZodTypeAny,
|
11
|
+
type ZodTypeDef,
|
12
|
+
custom,
|
13
|
+
type input,
|
14
|
+
type output,
|
15
|
+
} from 'zod'
|
16
|
+
|
17
|
+
export type CustomZodType = 'File' | 'Blob' | 'Invalid Date' | 'RegExp' | 'URL'
|
18
|
+
|
19
|
+
const customZodTypeSymbol = Symbol('customZodTypeSymbol')
|
20
|
+
|
21
|
+
const customZodFileMimeTypeSymbol = Symbol('customZodFileMimeTypeSymbol')
|
22
|
+
|
23
|
+
const CUSTOM_JSON_SCHEMA_SYMBOL = Symbol('CUSTOM_JSON_SCHEMA')
|
24
|
+
const CUSTOM_JSON_SCHEMA_INPUT_SYMBOL = Symbol('CUSTOM_JSON_SCHEMA_INPUT')
|
25
|
+
const CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL = Symbol('CUSTOM_JSON_SCHEMA_OUTPUT')
|
26
|
+
|
27
|
+
type CustomParams = CustomErrorParams & {
|
28
|
+
fatal?: boolean
|
29
|
+
}
|
30
|
+
|
31
|
+
export function getCustomZodType(def: ZodTypeDef): CustomZodType | undefined {
|
32
|
+
return customZodTypeSymbol in def
|
33
|
+
? (def[customZodTypeSymbol] as CustomZodType)
|
34
|
+
: undefined
|
35
|
+
}
|
36
|
+
|
37
|
+
export function getCustomZodFileMimeType(def: ZodTypeDef): string | undefined {
|
38
|
+
return customZodFileMimeTypeSymbol in def
|
39
|
+
? (def[customZodFileMimeTypeSymbol] as string)
|
40
|
+
: undefined
|
41
|
+
}
|
42
|
+
|
43
|
+
export function getCustomJSONSchema(
|
44
|
+
def: ZodTypeDef,
|
45
|
+
options?: { mode?: 'input' | 'output' },
|
46
|
+
): Exclude<JSONSchema, boolean> | undefined {
|
47
|
+
if (options?.mode === 'input' && CUSTOM_JSON_SCHEMA_INPUT_SYMBOL in def) {
|
48
|
+
return def[CUSTOM_JSON_SCHEMA_INPUT_SYMBOL] as Exclude<JSONSchema, boolean>
|
49
|
+
}
|
50
|
+
|
51
|
+
if (options?.mode === 'output' && CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL in def) {
|
52
|
+
return def[CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL] as Exclude<JSONSchema, boolean>
|
53
|
+
}
|
54
|
+
|
55
|
+
if (CUSTOM_JSON_SCHEMA_SYMBOL in def) {
|
56
|
+
return def[CUSTOM_JSON_SCHEMA_SYMBOL] as Exclude<JSONSchema, boolean>
|
57
|
+
}
|
58
|
+
|
59
|
+
return undefined
|
60
|
+
}
|
61
|
+
|
62
|
+
function composeParams<T = unknown>(options: {
|
63
|
+
params?: string | CustomParams | ((input: T) => CustomParams)
|
64
|
+
defaultMessage?: string | ((input: T) => string)
|
65
|
+
}): (input: T) => CustomParams {
|
66
|
+
return (val) => {
|
67
|
+
const defaultMessage =
|
68
|
+
typeof options.defaultMessage === 'function'
|
69
|
+
? options.defaultMessage(val)
|
70
|
+
: options.defaultMessage
|
71
|
+
|
72
|
+
if (!options.params) {
|
73
|
+
return {
|
74
|
+
message: defaultMessage,
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
if (typeof options.params === 'function') {
|
79
|
+
return {
|
80
|
+
message: defaultMessage,
|
81
|
+
...options.params(val),
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
if (typeof options.params === 'object') {
|
86
|
+
return {
|
87
|
+
message: defaultMessage,
|
88
|
+
...options.params,
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
return {
|
93
|
+
message: options.params,
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
export function file(
|
99
|
+
params?: string | CustomParams | ((input: unknown) => CustomParams),
|
100
|
+
): ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>> & {
|
101
|
+
type(
|
102
|
+
mimeType: string,
|
103
|
+
params?: string | CustomParams | ((input: unknown) => CustomParams),
|
104
|
+
): ZodEffects<
|
105
|
+
ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>>,
|
106
|
+
InstanceType<typeof File>,
|
107
|
+
InstanceType<typeof File>
|
108
|
+
>
|
109
|
+
} {
|
110
|
+
const schema = custom<InstanceType<typeof File>>(
|
111
|
+
(val) => val instanceof File,
|
112
|
+
composeParams({ params, defaultMessage: 'Input is not a file' }),
|
113
|
+
)
|
114
|
+
|
115
|
+
Object.assign(schema._def, {
|
116
|
+
[customZodTypeSymbol]: 'File' satisfies CustomZodType,
|
117
|
+
})
|
118
|
+
|
119
|
+
return Object.assign(schema, {
|
120
|
+
type: (
|
121
|
+
mimeType: string,
|
122
|
+
params: string | CustomParams | ((input: unknown) => CustomParams),
|
123
|
+
) => {
|
124
|
+
const isMatch = wcmatch(mimeType)
|
125
|
+
|
126
|
+
const refinedSchema = schema.refine(
|
127
|
+
(val) => isMatch(val.type.split(';')[0]!),
|
128
|
+
composeParams<InstanceType<typeof File>>({
|
129
|
+
params,
|
130
|
+
defaultMessage: (val) =>
|
131
|
+
`Expected a file of type ${mimeType} but got a file of type ${val.type || 'unknown'}`,
|
132
|
+
}),
|
133
|
+
)
|
134
|
+
|
135
|
+
Object.assign(refinedSchema._def, {
|
136
|
+
[customZodTypeSymbol]: 'File' satisfies CustomZodType,
|
137
|
+
[customZodFileMimeTypeSymbol]: mimeType,
|
138
|
+
})
|
139
|
+
|
140
|
+
return refinedSchema
|
141
|
+
},
|
142
|
+
})
|
143
|
+
}
|
144
|
+
|
145
|
+
export function blob(
|
146
|
+
params?: string | CustomParams | ((input: unknown) => CustomParams),
|
147
|
+
): ZodType<InstanceType<typeof Blob>, ZodTypeDef, InstanceType<typeof Blob>> {
|
148
|
+
const schema = custom<InstanceType<typeof Blob>>(
|
149
|
+
(val) => val instanceof Blob,
|
150
|
+
composeParams({ params, defaultMessage: 'Input is not a blob' }),
|
151
|
+
)
|
152
|
+
|
153
|
+
Object.assign(schema._def, {
|
154
|
+
[customZodTypeSymbol]: 'Blob' satisfies CustomZodType,
|
155
|
+
})
|
156
|
+
|
157
|
+
return schema
|
158
|
+
}
|
159
|
+
|
160
|
+
export function invalidDate(
|
161
|
+
params?: string | CustomParams | ((input: unknown) => CustomParams),
|
162
|
+
): ZodType<Date, ZodTypeDef, Date> {
|
163
|
+
const schema = custom<Date>(
|
164
|
+
(val) => val instanceof Date && Number.isNaN(val.getTime()),
|
165
|
+
composeParams({ params, defaultMessage: 'Input is not an invalid date' }),
|
166
|
+
)
|
167
|
+
|
168
|
+
Object.assign(schema._def, {
|
169
|
+
[customZodTypeSymbol]: 'Invalid Date' satisfies CustomZodType,
|
170
|
+
})
|
171
|
+
|
172
|
+
return schema
|
173
|
+
}
|
174
|
+
|
175
|
+
export function regexp(
|
176
|
+
options?: CustomParams,
|
177
|
+
): ZodType<RegExp, ZodTypeDef, RegExp> {
|
178
|
+
const schema = custom<RegExp>(
|
179
|
+
(val) => val instanceof RegExp,
|
180
|
+
composeParams({ params: options, defaultMessage: 'Input is not a regexp' }),
|
181
|
+
)
|
182
|
+
|
183
|
+
Object.assign(schema._def, {
|
184
|
+
[customZodTypeSymbol]: 'RegExp' satisfies CustomZodType,
|
185
|
+
})
|
186
|
+
|
187
|
+
return schema
|
188
|
+
}
|
189
|
+
|
190
|
+
export function url(options?: CustomParams): ZodType<URL, ZodTypeDef, URL> {
|
191
|
+
const schema = custom<URL>(
|
192
|
+
(val) => val instanceof URL,
|
193
|
+
composeParams({ params: options, defaultMessage: 'Input is not a URL' }),
|
194
|
+
)
|
195
|
+
|
196
|
+
Object.assign(schema._def, {
|
197
|
+
[customZodTypeSymbol]: 'URL' satisfies CustomZodType,
|
198
|
+
})
|
199
|
+
|
200
|
+
return schema
|
201
|
+
}
|
202
|
+
|
203
|
+
export function openapi<
|
204
|
+
T extends ZodTypeAny,
|
205
|
+
TMode extends 'input' | 'output' | 'both' = 'both',
|
206
|
+
>(
|
207
|
+
schema: T,
|
208
|
+
custom: Exclude<
|
209
|
+
JSONSchema<
|
210
|
+
TMode extends 'input'
|
211
|
+
? input<T>
|
212
|
+
: TMode extends 'output'
|
213
|
+
? output<T>
|
214
|
+
: input<T> & output<T>
|
215
|
+
>,
|
216
|
+
boolean
|
217
|
+
>,
|
218
|
+
options?: { mode: TMode },
|
219
|
+
): ReturnType<T['refine']> {
|
220
|
+
const newSchema = schema.refine(() => true) as ReturnType<T['refine']>
|
221
|
+
|
222
|
+
const SYMBOL =
|
223
|
+
options?.mode === 'input'
|
224
|
+
? CUSTOM_JSON_SCHEMA_INPUT_SYMBOL
|
225
|
+
: options?.mode === 'output'
|
226
|
+
? CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL
|
227
|
+
: CUSTOM_JSON_SCHEMA_SYMBOL
|
228
|
+
|
229
|
+
Object.assign(newSchema._def, {
|
230
|
+
[SYMBOL]: custom,
|
231
|
+
})
|
232
|
+
|
233
|
+
return newSchema
|
234
|
+
}
|
235
|
+
|
236
|
+
export const oz = {
|
237
|
+
openapi,
|
238
|
+
file,
|
239
|
+
blob,
|
240
|
+
invalidDate,
|
241
|
+
regexp,
|
242
|
+
url,
|
243
|
+
}
|