@orpc/zod 0.0.0-next.da84cda → 0.0.0-next.da8ae32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +20 -44
- package/dist/src/coercer.d.ts +5 -4
- package/dist/src/schemas.d.ts +1 -1
- package/package.json +6 -3
package/dist/index.js
CHANGED
@@ -1,44 +1,19 @@
|
|
1
|
-
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
|
2
|
-
function getType(payload) {
|
3
|
-
return Object.prototype.toString.call(payload).slice(8, -1);
|
4
|
-
}
|
5
|
-
|
6
|
-
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
|
7
|
-
function isPlainObject(payload) {
|
8
|
-
if (getType(payload) !== "Object")
|
9
|
-
return false;
|
10
|
-
const prototype = Object.getPrototypeOf(payload);
|
11
|
-
return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
|
12
|
-
}
|
13
|
-
|
14
|
-
// ../../node_modules/.pnpm/radash@12.1.0/node_modules/radash/dist/esm/async.mjs
|
15
|
-
var guard = (func, shouldGuard) => {
|
16
|
-
const _guard = (err) => {
|
17
|
-
if (shouldGuard && !shouldGuard(err))
|
18
|
-
throw err;
|
19
|
-
return void 0;
|
20
|
-
};
|
21
|
-
const isPromise2 = (result) => result instanceof Promise;
|
22
|
-
try {
|
23
|
-
const result = func();
|
24
|
-
return isPromise2(result) ? result.catch(_guard) : result;
|
25
|
-
} catch (err) {
|
26
|
-
return _guard(err);
|
27
|
-
}
|
28
|
-
};
|
29
|
-
|
30
1
|
// src/coercer.ts
|
2
|
+
import { guard, isObject } from "@orpc/shared";
|
31
3
|
import {
|
32
4
|
ZodFirstPartyTypeKind
|
33
5
|
} from "zod";
|
34
|
-
var
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
6
|
+
var ZodAutoCoercePlugin = class {
|
7
|
+
beforeCreateProcedureClient(clientOptions) {
|
8
|
+
clientOptions.interceptors ??= [];
|
9
|
+
clientOptions.interceptors.unshift((options) => {
|
10
|
+
const inputSchema = options.procedure["~orpc"].inputSchema;
|
11
|
+
if (!inputSchema || inputSchema["~standard"].vendor !== "zod") {
|
12
|
+
return options.next();
|
13
|
+
}
|
14
|
+
const coercedInput = zodCoerceInternal(inputSchema, options.input, { bracketNotation: true });
|
15
|
+
return options.next({ ...options, input: coercedInput });
|
16
|
+
});
|
42
17
|
}
|
43
18
|
};
|
44
19
|
function zodCoerceInternal(schema, value, options) {
|
@@ -125,7 +100,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
125
100
|
if (value === void 0) {
|
126
101
|
return [];
|
127
102
|
}
|
128
|
-
if (
|
103
|
+
if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
|
129
104
|
const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
|
130
105
|
const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
|
131
106
|
for (const i of indexes) {
|
@@ -136,7 +111,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
136
111
|
}
|
137
112
|
} else if (typeName === ZodFirstPartyTypeKind.ZodObject) {
|
138
113
|
const schema_ = schema;
|
139
|
-
if (
|
114
|
+
if (isObject(value)) {
|
140
115
|
const newObj = {};
|
141
116
|
const keys = /* @__PURE__ */ new Set([
|
142
117
|
...Object.keys(value),
|
@@ -174,7 +149,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
174
149
|
if (value === void 0) {
|
175
150
|
return /* @__PURE__ */ new Set();
|
176
151
|
}
|
177
|
-
if (
|
152
|
+
if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
|
178
153
|
const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
|
179
154
|
const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
|
180
155
|
for (const i of indexes) {
|
@@ -197,9 +172,9 @@ function zodCoerceInternal(schema, value, options) {
|
|
197
172
|
if (value === void 0) {
|
198
173
|
return /* @__PURE__ */ new Map();
|
199
174
|
}
|
200
|
-
if (
|
175
|
+
if (isObject(value)) {
|
201
176
|
const arr = Array.from({ length: Object.keys(value).length }).fill(void 0).map(
|
202
|
-
(_, i) =>
|
177
|
+
(_, i) => isObject(value[i]) && Object.keys(value[i]).length === 2 && "0" in value[i] && "1" in value[i] ? [value[i]["0"], value[i]["1"]] : void 0
|
203
178
|
);
|
204
179
|
if (arr.every((v) => !!v)) {
|
205
180
|
return new Map(
|
@@ -213,7 +188,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
213
188
|
}
|
214
189
|
} else if (typeName === ZodFirstPartyTypeKind.ZodRecord) {
|
215
190
|
const schema_ = schema;
|
216
|
-
if (
|
191
|
+
if (isObject(value)) {
|
217
192
|
const newObj = {};
|
218
193
|
for (const [k, v] of Object.entries(value)) {
|
219
194
|
const key = zodCoerceInternal(schema_._def.keyType, k, options_);
|
@@ -724,6 +699,7 @@ function zodToJsonSchema(schema, options) {
|
|
724
699
|
const schema_ = schema__;
|
725
700
|
const def = schema_._def;
|
726
701
|
const json = { type: "array" };
|
702
|
+
json.items = zodToJsonSchema(def.type, childOptions);
|
727
703
|
if (def.exactLength) {
|
728
704
|
json.maxItems = def.exactLength.value;
|
729
705
|
json.minItems = def.exactLength.value;
|
@@ -956,7 +932,7 @@ export {
|
|
956
932
|
NON_LOGIC_KEYWORDS,
|
957
933
|
UNDEFINED_JSON_SCHEMA,
|
958
934
|
UNSUPPORTED_JSON_SCHEMA,
|
959
|
-
|
935
|
+
ZodAutoCoercePlugin,
|
960
936
|
ZodToJsonSchemaConverter,
|
961
937
|
blob,
|
962
938
|
file,
|
package/dist/src/coercer.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import type {
|
2
|
-
import type {
|
3
|
-
|
4
|
-
|
1
|
+
import type { Context } from '@orpc/server';
|
2
|
+
import type { Plugin } from '@orpc/server/plugins';
|
3
|
+
import type { WellCreateProcedureClientOptions } from '@orpc/server/standard';
|
4
|
+
export declare class ZodAutoCoercePlugin<TContext extends Context> implements Plugin<TContext> {
|
5
|
+
beforeCreateProcedureClient(clientOptions: WellCreateProcedureClientOptions<TContext>): void;
|
5
6
|
}
|
6
7
|
//# sourceMappingURL=coercer.d.ts.map
|
package/dist/src/schemas.d.ts
CHANGED
@@ -10,7 +10,7 @@ export declare function getCustomJSONSchema(def: ZodTypeDef, options?: {
|
|
10
10
|
mode?: 'input' | 'output';
|
11
11
|
}): Exclude<JSONSchema, boolean> | undefined;
|
12
12
|
export declare function file(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof File>, ZodTypeDef, InstanceType<typeof File>> & {
|
13
|
-
type
|
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
14
|
};
|
15
15
|
export declare function blob(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<InstanceType<typeof Blob>, ZodTypeDef, InstanceType<typeof Blob>>;
|
16
16
|
export declare function invalidDate(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<Date, ZodTypeDef, Date>;
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/zod",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.da8ae32",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -29,12 +29,15 @@
|
|
29
29
|
"dist"
|
30
30
|
],
|
31
31
|
"peerDependencies": {
|
32
|
-
"@orpc/openapi": "0.0.0-next.
|
32
|
+
"@orpc/openapi": "0.0.0-next.da8ae32",
|
33
|
+
"@orpc/server": "0.0.0-next.da8ae32"
|
33
34
|
},
|
34
35
|
"dependencies": {
|
36
|
+
"@standard-schema/spec": "^1.0.0",
|
35
37
|
"json-schema-typed": "^8.0.1",
|
36
38
|
"wildcard-match": "^5.1.3",
|
37
|
-
"zod": "^3.24.1"
|
39
|
+
"zod": "^3.24.1",
|
40
|
+
"@orpc/shared": "0.0.0-next.da8ae32"
|
38
41
|
},
|
39
42
|
"scripts": {
|
40
43
|
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|