@orpc/zod 0.0.0-next.d42488d → 0.0.0-next.d74cac4
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 +71 -55
- package/dist/src/coercer.d.ts +5 -4
- package/dist/src/converter.d.ts +6 -0
- package/dist/src/schemas.d.ts +1 -1
- package/package.json +6 -3
package/dist/index.js
CHANGED
@@ -1,44 +1,58 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
function
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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);
|
1
|
+
var __create = Object.create;
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
27
15
|
}
|
16
|
+
return to;
|
28
17
|
};
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
24
|
+
mod
|
25
|
+
));
|
26
|
+
|
27
|
+
// ../../node_modules/.pnpm/escape-string-regexp@4.0.0/node_modules/escape-string-regexp/index.js
|
28
|
+
var require_escape_string_regexp = __commonJS({
|
29
|
+
"../../node_modules/.pnpm/escape-string-regexp@4.0.0/node_modules/escape-string-regexp/index.js"(exports, module) {
|
30
|
+
"use strict";
|
31
|
+
module.exports = (string) => {
|
32
|
+
if (typeof string !== "string") {
|
33
|
+
throw new TypeError("Expected a string");
|
34
|
+
}
|
35
|
+
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
|
36
|
+
};
|
37
|
+
}
|
38
|
+
});
|
29
39
|
|
30
40
|
// src/coercer.ts
|
41
|
+
import { guard, isObject } from "@orpc/shared";
|
31
42
|
import {
|
32
43
|
ZodFirstPartyTypeKind
|
33
44
|
} from "zod";
|
34
|
-
var
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
45
|
+
var ZodAutoCoercePlugin = class {
|
46
|
+
beforeCreateProcedureClient(clientOptions) {
|
47
|
+
clientOptions.interceptors ??= [];
|
48
|
+
clientOptions.interceptors.unshift((options) => {
|
49
|
+
const inputSchema = options.procedure["~orpc"].inputSchema;
|
50
|
+
if (!inputSchema || inputSchema["~standard"].vendor !== "zod") {
|
51
|
+
return options.next();
|
52
|
+
}
|
53
|
+
const coercedInput = zodCoerceInternal(inputSchema, options.input, { bracketNotation: true });
|
54
|
+
return options.next({ ...options, input: coercedInput });
|
55
|
+
});
|
42
56
|
}
|
43
57
|
};
|
44
58
|
function zodCoerceInternal(schema, value, options) {
|
@@ -125,7 +139,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
125
139
|
if (value === void 0) {
|
126
140
|
return [];
|
127
141
|
}
|
128
|
-
if (
|
142
|
+
if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
|
129
143
|
const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
|
130
144
|
const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
|
131
145
|
for (const i of indexes) {
|
@@ -136,7 +150,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
136
150
|
}
|
137
151
|
} else if (typeName === ZodFirstPartyTypeKind.ZodObject) {
|
138
152
|
const schema_ = schema;
|
139
|
-
if (
|
153
|
+
if (isObject(value)) {
|
140
154
|
const newObj = {};
|
141
155
|
const keys = /* @__PURE__ */ new Set([
|
142
156
|
...Object.keys(value),
|
@@ -174,7 +188,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
174
188
|
if (value === void 0) {
|
175
189
|
return /* @__PURE__ */ new Set();
|
176
190
|
}
|
177
|
-
if (
|
191
|
+
if (isObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
|
178
192
|
const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
|
179
193
|
const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
|
180
194
|
for (const i of indexes) {
|
@@ -197,9 +211,9 @@ function zodCoerceInternal(schema, value, options) {
|
|
197
211
|
if (value === void 0) {
|
198
212
|
return /* @__PURE__ */ new Map();
|
199
213
|
}
|
200
|
-
if (
|
214
|
+
if (isObject(value)) {
|
201
215
|
const arr = Array.from({ length: Object.keys(value).length }).fill(void 0).map(
|
202
|
-
(_, i) =>
|
216
|
+
(_, 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
217
|
);
|
204
218
|
if (arr.every((v) => !!v)) {
|
205
219
|
return new Map(
|
@@ -213,7 +227,7 @@ function zodCoerceInternal(schema, value, options) {
|
|
213
227
|
}
|
214
228
|
} else if (typeName === ZodFirstPartyTypeKind.ZodRecord) {
|
215
229
|
const schema_ = schema;
|
216
|
-
if (
|
230
|
+
if (isObject(value)) {
|
217
231
|
const newObj = {};
|
218
232
|
for (const [k, v] of Object.entries(value)) {
|
219
233
|
const key = zodCoerceInternal(schema_._def.keyType, k, options_);
|
@@ -341,17 +355,8 @@ function zodCoerceInternal(schema, value, options) {
|
|
341
355
|
}
|
342
356
|
|
343
357
|
// src/converter.ts
|
358
|
+
var import_escape_string_regexp = __toESM(require_escape_string_regexp(), 1);
|
344
359
|
import { JSONSchemaFormat } from "@orpc/openapi";
|
345
|
-
|
346
|
-
// ../../node_modules/.pnpm/escape-string-regexp@5.0.0/node_modules/escape-string-regexp/index.js
|
347
|
-
function escapeStringRegexp(string) {
|
348
|
-
if (typeof string !== "string") {
|
349
|
-
throw new TypeError("Expected a string");
|
350
|
-
}
|
351
|
-
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
|
352
|
-
}
|
353
|
-
|
354
|
-
// src/converter.ts
|
355
360
|
import {
|
356
361
|
ZodFirstPartyTypeKind as ZodFirstPartyTypeKind2
|
357
362
|
} from "zod";
|
@@ -528,6 +533,16 @@ function zodToJsonSchema(schema, options) {
|
|
528
533
|
return {};
|
529
534
|
}
|
530
535
|
const schema__ = schema;
|
536
|
+
if (!options?.isHandledZodDescription && "description" in schema__._def) {
|
537
|
+
const json = zodToJsonSchema(schema__, {
|
538
|
+
...options,
|
539
|
+
isHandledZodDescription: true
|
540
|
+
});
|
541
|
+
return {
|
542
|
+
description: schema__._def.description,
|
543
|
+
...json
|
544
|
+
};
|
545
|
+
}
|
531
546
|
if (!options?.isHandledCustomJSONSchema) {
|
532
547
|
const customJSONSchema = getCustomJSONSchema(schema__._def, options);
|
533
548
|
if (customJSONSchema) {
|
@@ -541,7 +556,7 @@ function zodToJsonSchema(schema, options) {
|
|
541
556
|
};
|
542
557
|
}
|
543
558
|
}
|
544
|
-
const childOptions = { ...options, isHandledCustomJSONSchema: false };
|
559
|
+
const childOptions = { ...options, isHandledCustomJSONSchema: false, isHandledZodDescription: false };
|
545
560
|
const customType = getCustomZodType(schema__._def);
|
546
561
|
switch (customType) {
|
547
562
|
case "Blob": {
|
@@ -601,13 +616,13 @@ function zodToJsonSchema(schema, options) {
|
|
601
616
|
json.maxLength = check.value;
|
602
617
|
break;
|
603
618
|
case "includes":
|
604
|
-
json.pattern =
|
619
|
+
json.pattern = (0, import_escape_string_regexp.default)(check.value);
|
605
620
|
break;
|
606
621
|
case "startsWith":
|
607
|
-
json.pattern = `^${
|
622
|
+
json.pattern = `^${(0, import_escape_string_regexp.default)(check.value)}`;
|
608
623
|
break;
|
609
624
|
case "endsWith":
|
610
|
-
json.pattern = `${
|
625
|
+
json.pattern = `${(0, import_escape_string_regexp.default)(check.value)}$`;
|
611
626
|
break;
|
612
627
|
case "emoji":
|
613
628
|
json.pattern = "^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$";
|
@@ -714,6 +729,7 @@ function zodToJsonSchema(schema, options) {
|
|
714
729
|
const schema_ = schema__;
|
715
730
|
const def = schema_._def;
|
716
731
|
const json = { type: "array" };
|
732
|
+
json.items = zodToJsonSchema(def.type, childOptions);
|
717
733
|
if (def.exactLength) {
|
718
734
|
json.maxItems = def.exactLength.value;
|
719
735
|
json.minItems = def.exactLength.value;
|
@@ -946,7 +962,7 @@ export {
|
|
946
962
|
NON_LOGIC_KEYWORDS,
|
947
963
|
UNDEFINED_JSON_SCHEMA,
|
948
964
|
UNSUPPORTED_JSON_SCHEMA,
|
949
|
-
|
965
|
+
ZodAutoCoercePlugin,
|
950
966
|
ZodToJsonSchemaConverter,
|
951
967
|
blob,
|
952
968
|
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/converter.d.ts
CHANGED
@@ -35,6 +35,12 @@ export interface ZodToJsonSchemaOptions {
|
|
35
35
|
* @internal
|
36
36
|
*/
|
37
37
|
isHandledCustomJSONSchema?: boolean;
|
38
|
+
/**
|
39
|
+
* Track if current level schema is handled zod description to prevent recursive
|
40
|
+
*
|
41
|
+
* @internal
|
42
|
+
*/
|
43
|
+
isHandledZodDescription?: boolean;
|
38
44
|
}
|
39
45
|
export declare function zodToJsonSchema(schema: StandardSchemaV1, options?: ZodToJsonSchemaOptions): Exclude<JSONSchema.JSONSchema, boolean>;
|
40
46
|
export declare class ZodToJsonSchemaConverter implements SchemaConverter {
|
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.d74cac4",
|
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.d74cac4",
|
33
|
+
"@orpc/server": "0.0.0-next.d74cac4"
|
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.d74cac4"
|
38
41
|
},
|
39
42
|
"scripts": {
|
40
43
|
"build": "tsup --clean --sourcemap --entry.index=src/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
|