@zenstackhq/runtime 2.6.1 → 2.6.2

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zenstackhq/runtime",
3
3
  "displayName": "ZenStack Runtime Library",
4
- "version": "2.6.1",
4
+ "version": "2.6.2",
5
5
  "description": "Runtime of ZenStack for both client-side and server-side environments.",
6
6
  "repository": {
7
7
  "type": "git",
@@ -69,6 +69,10 @@
69
69
  "./models": {
70
70
  "types": "./models.d.ts"
71
71
  },
72
+ "./zod-utils": {
73
+ "types": "./zod-utils.d.ts",
74
+ "default": "./zod-utils.js"
75
+ },
72
76
  "./package.json": {
73
77
  "default": "./package.json"
74
78
  }
@@ -100,7 +104,7 @@
100
104
  "zod-validation-error": "^1.5.0"
101
105
  },
102
106
  "peerDependencies": {
103
- "@prisma/client": "5.0.0 - 5.19.x"
107
+ "@prisma/client": "5.0.0 - 5.20.x"
104
108
  },
105
109
  "author": {
106
110
  "name": "ZenStack Team"
package/zod-utils.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { z as Z } from 'zod';
2
+ /**
3
+ * A smarter version of `z.union` that decide which candidate to use based on how few unrecognized keys it has.
4
+ *
5
+ * The helper is used to deal with ambiguity in union generated for Prisma inputs when the zod schemas are configured
6
+ * to run in "strip" object parsing mode. Since "strip" automatically drops unrecognized keys, it may result in
7
+ * accidentally matching a less-ideal schema candidate.
8
+ *
9
+ * The helper uses a custom schema to find the candidate that results in the fewest unrecognized keys when parsing the data.
10
+ */
11
+ export declare function smartUnion(z: typeof Z, candidates: Z.ZodSchema[]): Z.ZodUnion<any> | Z.ZodEffects<Z.ZodType<unknown, Z.ZodTypeDef, unknown>, any, unknown>;
package/zod-utils.js ADDED
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.smartUnion = smartUnion;
4
+ /**
5
+ * A smarter version of `z.union` that decide which candidate to use based on how few unrecognized keys it has.
6
+ *
7
+ * The helper is used to deal with ambiguity in union generated for Prisma inputs when the zod schemas are configured
8
+ * to run in "strip" object parsing mode. Since "strip" automatically drops unrecognized keys, it may result in
9
+ * accidentally matching a less-ideal schema candidate.
10
+ *
11
+ * The helper uses a custom schema to find the candidate that results in the fewest unrecognized keys when parsing the data.
12
+ */
13
+ function smartUnion(z, candidates) {
14
+ // strip `z.lazy`
15
+ const processedCandidates = candidates.map((candidate) => unwrapLazy(z, candidate));
16
+ if (processedCandidates.some((c) => !(c instanceof z.ZodObject || c instanceof z.ZodArray))) {
17
+ // fall back to plain union if not all candidates are objects or arrays
18
+ return z.union(candidates);
19
+ }
20
+ let resultData;
21
+ return z
22
+ .custom((data) => {
23
+ if (Array.isArray(data)) {
24
+ const { data: result, success } = smartArrayUnion(z, processedCandidates.filter((c) => c instanceof z.ZodArray), data);
25
+ if (success) {
26
+ resultData = result;
27
+ }
28
+ return success;
29
+ }
30
+ else {
31
+ const { data: result, success } = smartObjectUnion(z, processedCandidates.filter((c) => c instanceof z.ZodObject), data);
32
+ if (success) {
33
+ resultData = result;
34
+ }
35
+ return success;
36
+ }
37
+ })
38
+ .transform(() => {
39
+ // return the parsed data
40
+ return resultData;
41
+ });
42
+ }
43
+ function smartArrayUnion(z, candidates, data) {
44
+ if (candidates.length === 0) {
45
+ return { data: undefined, success: false };
46
+ }
47
+ if (!Array.isArray(data)) {
48
+ return { data: undefined, success: false };
49
+ }
50
+ if (data.length === 0) {
51
+ return { data, success: true };
52
+ }
53
+ // use the first element to identify the candidate schema to use
54
+ const item = data[0];
55
+ const itemSchema = identifyCandidate(z, candidates.map((candidate) => candidate.element), item);
56
+ // find the matching schema and re-parse the data
57
+ const schema = candidates.find((candidate) => candidate.element === itemSchema);
58
+ return schema.safeParse(data);
59
+ }
60
+ function smartObjectUnion(z, candidates, data) {
61
+ if (candidates.length === 0) {
62
+ return { data: undefined, success: false };
63
+ }
64
+ const schema = identifyCandidate(z, candidates, data);
65
+ return schema.safeParse(data);
66
+ }
67
+ function identifyCandidate(z, candidates, data) {
68
+ const strictResults = candidates.map((candidate) => {
69
+ // make sure to strip `z.lazy` before parsing
70
+ const unwrapped = unwrapLazy(z, candidate);
71
+ return {
72
+ schema: candidate,
73
+ // force object schema to run in strict mode to capture unrecognized keys
74
+ result: unwrapped.strict().safeParse(data),
75
+ };
76
+ });
77
+ // find the schema with the fewest unrecognized keys
78
+ const { schema } = strictResults.sort((a, b) => {
79
+ var _a, _b, _c, _d;
80
+ const aCount = countUnrecognizedKeys((_b = (_a = a.result.error) === null || _a === void 0 ? void 0 : _a.issues) !== null && _b !== void 0 ? _b : []);
81
+ const bCount = countUnrecognizedKeys((_d = (_c = b.result.error) === null || _c === void 0 ? void 0 : _c.issues) !== null && _d !== void 0 ? _d : []);
82
+ return aCount - bCount;
83
+ })[0];
84
+ return schema;
85
+ }
86
+ function countUnrecognizedKeys(issues) {
87
+ return issues
88
+ .filter((issue) => issue.code === 'unrecognized_keys')
89
+ .map((issue) => issue.keys.length)
90
+ .reduce((a, b) => a + b, 0);
91
+ }
92
+ function unwrapLazy(z, schema) {
93
+ return schema instanceof z.ZodLazy ? schema.schema : schema;
94
+ }
95
+ //# sourceMappingURL=zod-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod-utils.js","sourceRoot":"","sources":["../src/zod-utils.ts"],"names":[],"mappings":";;AAYA,gCAuCC;AAhDD;;;;;;;;GAQG;AACH,SAAgB,UAAU,CAAC,CAAW,EAAE,UAAyB;IAC7D,iBAAiB;IACjB,MAAM,mBAAmB,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC;IAEpF,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,SAAS,IAAI,CAAC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