@cms0/shared 0.0.3 → 0.0.5
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/cjs/index.js +20 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/validation.js +75 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/validation.js +72 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/validation.d.ts +46 -0
- package/dist/types/validation.d.ts.map +1 -0
- package/package.json +19 -10
- package/CHANGELOG.md +0 -18
- package/src/index.ts +0 -3
- package/src/validation.ts +0 -142
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.delay = void 0;
|
|
18
|
+
__exportStar(require("./validation.js"), exports);
|
|
19
|
+
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
20
|
+
exports.delay = delay;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildZodSchemasFromDescriptor = buildZodSchemasFromDescriptor;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
// Map custom primitive-models (or types) you defined
|
|
6
|
+
const customTypeSchemas = {
|
|
7
|
+
Asset: zod_1.z.object({ id: zod_1.z.string(), url: zod_1.z.string().url() }),
|
|
8
|
+
RichText: zod_1.z.object({ html: zod_1.z.string(), delta: zod_1.z.array(zod_1.z.any()) }),
|
|
9
|
+
Url: zod_1.z.string().url(),
|
|
10
|
+
};
|
|
11
|
+
function convertDescriptorToZod(desc, modelZodSchemas, opts = {}) {
|
|
12
|
+
const applyOptionality = (schema) => {
|
|
13
|
+
let out = schema;
|
|
14
|
+
if (desc.nullable || opts.loose)
|
|
15
|
+
out = out.nullable();
|
|
16
|
+
if (desc.optional || opts.loose)
|
|
17
|
+
out = out.optional();
|
|
18
|
+
return out;
|
|
19
|
+
};
|
|
20
|
+
if (desc.kind === "primitive") {
|
|
21
|
+
switch (desc.type) {
|
|
22
|
+
case "string":
|
|
23
|
+
return applyOptionality(zod_1.z.string());
|
|
24
|
+
case "number":
|
|
25
|
+
return applyOptionality(zod_1.z.number());
|
|
26
|
+
case "boolean":
|
|
27
|
+
return applyOptionality(zod_1.z.boolean());
|
|
28
|
+
case "json":
|
|
29
|
+
return applyOptionality(zod_1.z.any());
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (desc.kind === "modelRef") {
|
|
33
|
+
const schema = modelZodSchemas[desc.model];
|
|
34
|
+
const base = schema ?? zod_1.z.any();
|
|
35
|
+
// For loose schemas (root singletons), accept either the object or just an id.
|
|
36
|
+
const withId = opts.loose ? zod_1.z.union([base, zod_1.z.string()]) : base;
|
|
37
|
+
return applyOptionality(withId);
|
|
38
|
+
}
|
|
39
|
+
if (desc.type === "object" && desc.properties) {
|
|
40
|
+
const shape = {};
|
|
41
|
+
for (const [key, valueDesc] of Object.entries(desc.properties)) {
|
|
42
|
+
shape[key] = convertDescriptorToZod(valueDesc, modelZodSchemas, opts);
|
|
43
|
+
}
|
|
44
|
+
const objectSchema = opts.loose ? zod_1.z.object(shape).partial() : zod_1.z.object(shape);
|
|
45
|
+
return applyOptionality(objectSchema);
|
|
46
|
+
}
|
|
47
|
+
if (desc.type === "array" && desc.items) {
|
|
48
|
+
return applyOptionality(zod_1.z.array(convertDescriptorToZod(desc.items, modelZodSchemas, opts)));
|
|
49
|
+
}
|
|
50
|
+
if (typeof desc.type === "string") {
|
|
51
|
+
const custom = customTypeSchemas[desc.type];
|
|
52
|
+
if (custom) {
|
|
53
|
+
return applyOptionality(custom);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return applyOptionality(zod_1.z.any());
|
|
57
|
+
}
|
|
58
|
+
function buildZodSchemasFromDescriptor(descriptor) {
|
|
59
|
+
const modelZodSchemas = {};
|
|
60
|
+
const zodSchemas = {};
|
|
61
|
+
for (const [modelName, modelDesc] of Object.entries(descriptor.models)) {
|
|
62
|
+
const shape = {};
|
|
63
|
+
for (const [propName, propDesc] of Object.entries(modelDesc.properties)) {
|
|
64
|
+
shape[propName] = convertDescriptorToZod(propDesc, modelZodSchemas);
|
|
65
|
+
}
|
|
66
|
+
modelZodSchemas[modelName] = zod_1.z.object(shape);
|
|
67
|
+
}
|
|
68
|
+
for (const [rootKey, rootDesc] of Object.entries(descriptor.roots)) {
|
|
69
|
+
// Root schemas are used for singleton PATCH; make nested structures loose/optional.
|
|
70
|
+
zodSchemas[rootKey] = convertDescriptorToZod(rootDesc, modelZodSchemas, {
|
|
71
|
+
loose: true,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return { zodSchemas, modelZodSchemas };
|
|
75
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
// Map custom primitive-models (or types) you defined
|
|
3
|
+
const customTypeSchemas = {
|
|
4
|
+
Asset: z.object({ id: z.string(), url: z.string().url() }),
|
|
5
|
+
RichText: z.object({ html: z.string(), delta: z.array(z.any()) }),
|
|
6
|
+
Url: z.string().url(),
|
|
7
|
+
};
|
|
8
|
+
function convertDescriptorToZod(desc, modelZodSchemas, opts = {}) {
|
|
9
|
+
const applyOptionality = (schema) => {
|
|
10
|
+
let out = schema;
|
|
11
|
+
if (desc.nullable || opts.loose)
|
|
12
|
+
out = out.nullable();
|
|
13
|
+
if (desc.optional || opts.loose)
|
|
14
|
+
out = out.optional();
|
|
15
|
+
return out;
|
|
16
|
+
};
|
|
17
|
+
if (desc.kind === "primitive") {
|
|
18
|
+
switch (desc.type) {
|
|
19
|
+
case "string":
|
|
20
|
+
return applyOptionality(z.string());
|
|
21
|
+
case "number":
|
|
22
|
+
return applyOptionality(z.number());
|
|
23
|
+
case "boolean":
|
|
24
|
+
return applyOptionality(z.boolean());
|
|
25
|
+
case "json":
|
|
26
|
+
return applyOptionality(z.any());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (desc.kind === "modelRef") {
|
|
30
|
+
const schema = modelZodSchemas[desc.model];
|
|
31
|
+
const base = schema ?? z.any();
|
|
32
|
+
// For loose schemas (root singletons), accept either the object or just an id.
|
|
33
|
+
const withId = opts.loose ? z.union([base, z.string()]) : base;
|
|
34
|
+
return applyOptionality(withId);
|
|
35
|
+
}
|
|
36
|
+
if (desc.type === "object" && desc.properties) {
|
|
37
|
+
const shape = {};
|
|
38
|
+
for (const [key, valueDesc] of Object.entries(desc.properties)) {
|
|
39
|
+
shape[key] = convertDescriptorToZod(valueDesc, modelZodSchemas, opts);
|
|
40
|
+
}
|
|
41
|
+
const objectSchema = opts.loose ? z.object(shape).partial() : z.object(shape);
|
|
42
|
+
return applyOptionality(objectSchema);
|
|
43
|
+
}
|
|
44
|
+
if (desc.type === "array" && desc.items) {
|
|
45
|
+
return applyOptionality(z.array(convertDescriptorToZod(desc.items, modelZodSchemas, opts)));
|
|
46
|
+
}
|
|
47
|
+
if (typeof desc.type === "string") {
|
|
48
|
+
const custom = customTypeSchemas[desc.type];
|
|
49
|
+
if (custom) {
|
|
50
|
+
return applyOptionality(custom);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return applyOptionality(z.any());
|
|
54
|
+
}
|
|
55
|
+
export function buildZodSchemasFromDescriptor(descriptor) {
|
|
56
|
+
const modelZodSchemas = {};
|
|
57
|
+
const zodSchemas = {};
|
|
58
|
+
for (const [modelName, modelDesc] of Object.entries(descriptor.models)) {
|
|
59
|
+
const shape = {};
|
|
60
|
+
for (const [propName, propDesc] of Object.entries(modelDesc.properties)) {
|
|
61
|
+
shape[propName] = convertDescriptorToZod(propDesc, modelZodSchemas);
|
|
62
|
+
}
|
|
63
|
+
modelZodSchemas[modelName] = z.object(shape);
|
|
64
|
+
}
|
|
65
|
+
for (const [rootKey, rootDesc] of Object.entries(descriptor.roots)) {
|
|
66
|
+
// Root schemas are used for singleton PATCH; make nested structures loose/optional.
|
|
67
|
+
zodSchemas[rootKey] = convertDescriptorToZod(rootDesc, modelZodSchemas, {
|
|
68
|
+
loose: true,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return { zodSchemas, modelZodSchemas };
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAEhC,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,qBAA8C,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export type PrimitiveType = "string" | "number" | "boolean" | "json";
|
|
3
|
+
export type FieldDescriptor = {
|
|
4
|
+
kind?: "primitive";
|
|
5
|
+
type: PrimitiveType;
|
|
6
|
+
optional?: boolean;
|
|
7
|
+
nullable?: boolean;
|
|
8
|
+
customType?: string;
|
|
9
|
+
} | {
|
|
10
|
+
kind: "modelRef";
|
|
11
|
+
model: string;
|
|
12
|
+
optional?: boolean;
|
|
13
|
+
nullable?: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
kind?: "object";
|
|
16
|
+
type: "object";
|
|
17
|
+
properties: Record<string, FieldDescriptor>;
|
|
18
|
+
optional?: boolean;
|
|
19
|
+
nullable?: boolean;
|
|
20
|
+
customType?: string;
|
|
21
|
+
} | {
|
|
22
|
+
kind?: "array";
|
|
23
|
+
type: "array";
|
|
24
|
+
items: FieldDescriptor;
|
|
25
|
+
optional?: boolean;
|
|
26
|
+
nullable?: boolean;
|
|
27
|
+
customType?: string;
|
|
28
|
+
};
|
|
29
|
+
export type ModelDescriptor = {
|
|
30
|
+
kind: "model";
|
|
31
|
+
properties: Record<string, FieldDescriptor>;
|
|
32
|
+
};
|
|
33
|
+
export type RootDescriptor = FieldDescriptor;
|
|
34
|
+
export type FullDescriptor = {
|
|
35
|
+
models: Record<string, ModelDescriptor>;
|
|
36
|
+
roots: Record<string, RootDescriptor>;
|
|
37
|
+
metadata?: {
|
|
38
|
+
locales?: string[];
|
|
39
|
+
defaultLocale?: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export declare function buildZodSchemasFromDescriptor(descriptor: FullDescriptor): {
|
|
43
|
+
zodSchemas: Record<string, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>;
|
|
44
|
+
modelZodSchemas: Record<string, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>;
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAW,MAAM,KAAK,CAAC;AASjC,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;AAErE,MAAM,MAAM,eAAe,GACvB;IACE,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,IAAI,EAAE,aAAa,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACD;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GAC3E;IACE,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAC5C,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACD;IACE,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,eAAe,CAAC;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEN,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CAC7C,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,eAAe,CAAC;AAE7C,MAAM,MAAM,cAAc,GAAG;IAC3B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACxC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACtC,QAAQ,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;QACnB,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;CACH,CAAC;AAsEF,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,cAAc;;;EAoBvE"}
|
package/package.json
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cms0/shared",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"private": false,
|
|
5
6
|
"publishConfig": {
|
|
6
7
|
"access": "restricted"
|
|
7
8
|
},
|
|
8
|
-
"main": "dist/index.js",
|
|
9
|
-
"
|
|
9
|
+
"main": "./dist/cjs/index.js",
|
|
10
|
+
"module": "./dist/esm/index.js",
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
10
15
|
"exports": {
|
|
11
16
|
".": {
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"import": "./dist/index.js",
|
|
14
|
-
"require": "./dist/index.js"
|
|
17
|
+
"types": "./dist/types/index.d.ts",
|
|
18
|
+
"import": "./dist/esm/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js"
|
|
15
20
|
},
|
|
16
21
|
"./*": {
|
|
17
|
-
"types": "./dist/*.d.ts",
|
|
18
|
-
"import": "./dist/*.js",
|
|
19
|
-
"require": "./dist/*.js"
|
|
22
|
+
"types": "./dist/types/*.d.ts",
|
|
23
|
+
"import": "./dist/esm/*.js",
|
|
24
|
+
"require": "./dist/cjs/*.js"
|
|
20
25
|
}
|
|
21
26
|
},
|
|
22
27
|
"dependencies": {
|
|
@@ -28,7 +33,11 @@
|
|
|
28
33
|
"@cms0/typescript-config": "0.0.1"
|
|
29
34
|
},
|
|
30
35
|
"scripts": {
|
|
31
|
-
"build": "
|
|
36
|
+
"build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs && pnpm run build:post",
|
|
37
|
+
"build:clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true });\"",
|
|
38
|
+
"build:esm": "tsc -p tsconfig.json",
|
|
39
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
40
|
+
"build:post": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\\\n');\"",
|
|
32
41
|
"dev": "tsc -b --watch"
|
|
33
42
|
}
|
|
34
43
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# @cms0/shared
|
|
2
|
-
|
|
3
|
-
## 0.0.3
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- 0f52b27: Fix `@cms0/cms0` publish/build and TypeScript consumption issues:
|
|
8
|
-
|
|
9
|
-
- Generate a fallback schema descriptor file before TypeScript compilation when `src/generated/schema-descriptor.ts` is missing in clean CI.
|
|
10
|
-
- Correct package `exports` type/import paths to point to `dist/types` and `dist/esm` so consumers can resolve `@cms0/cms0` and `@cms0/cms0/config` declarations properly.
|
|
11
|
-
- Fix `@cms0/shared` package exports to resolve from `dist` instead of `src`, so SSR bundlers (including Next.js Turbopack) don’t try to compile source TypeScript from `node_modules`.
|
|
12
|
-
|
|
13
|
-
## 0.0.2
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- bef5ec7: Ensure the admin server binary is executable in Docker images.
|
|
18
|
-
- 2ee81b6: Fix rich text editor transaction sync issues and stabilize localized field test behavior.
|
package/src/index.ts
DELETED
package/src/validation.ts
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { z, ZodType } from "zod";
|
|
2
|
-
|
|
3
|
-
// Map custom primitive-models (or types) you defined
|
|
4
|
-
const customTypeSchemas: Record<string, ZodType<any>> = {
|
|
5
|
-
Asset: z.object({ id: z.string(), url: z.string().url() }),
|
|
6
|
-
RichText: z.object({ html: z.string(), delta: z.array(z.any()) }),
|
|
7
|
-
Url: z.string().url(),
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export type PrimitiveType = "string" | "number" | "boolean" | "json";
|
|
11
|
-
// Type definitions for descriptor elements
|
|
12
|
-
export type FieldDescriptor =
|
|
13
|
-
| {
|
|
14
|
-
kind?: "primitive";
|
|
15
|
-
type: PrimitiveType;
|
|
16
|
-
optional?: boolean;
|
|
17
|
-
nullable?: boolean;
|
|
18
|
-
customType?: string;
|
|
19
|
-
}
|
|
20
|
-
| { kind: "modelRef"; model: string; optional?: boolean; nullable?: boolean }
|
|
21
|
-
| {
|
|
22
|
-
kind?: "object";
|
|
23
|
-
type: "object";
|
|
24
|
-
properties: Record<string, FieldDescriptor>;
|
|
25
|
-
optional?: boolean;
|
|
26
|
-
nullable?: boolean;
|
|
27
|
-
customType?: string;
|
|
28
|
-
}
|
|
29
|
-
| {
|
|
30
|
-
kind?: "array";
|
|
31
|
-
type: "array";
|
|
32
|
-
items: FieldDescriptor;
|
|
33
|
-
optional?: boolean;
|
|
34
|
-
nullable?: boolean;
|
|
35
|
-
customType?: string;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type ModelDescriptor = {
|
|
39
|
-
kind: "model";
|
|
40
|
-
properties: Record<string, FieldDescriptor>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type RootDescriptor = FieldDescriptor;
|
|
44
|
-
|
|
45
|
-
export type FullDescriptor = {
|
|
46
|
-
models: Record<string, ModelDescriptor>;
|
|
47
|
-
roots: Record<string, RootDescriptor>;
|
|
48
|
-
metadata?: {
|
|
49
|
-
locales?: string[];
|
|
50
|
-
defaultLocale?: string;
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
function convertDescriptorToZod(
|
|
55
|
-
desc: FieldDescriptor,
|
|
56
|
-
modelZodSchemas: Record<string, ZodType<any>>,
|
|
57
|
-
opts: { loose?: boolean } = {}
|
|
58
|
-
): ZodType<any> {
|
|
59
|
-
const applyOptionality = (schema: ZodType<any>) => {
|
|
60
|
-
let out = schema;
|
|
61
|
-
if (desc.nullable || opts.loose) out = out.nullable();
|
|
62
|
-
if (desc.optional || opts.loose) out = out.optional();
|
|
63
|
-
return out;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
if (desc.kind === "primitive") {
|
|
67
|
-
switch (desc.type) {
|
|
68
|
-
case "string":
|
|
69
|
-
return applyOptionality(z.string());
|
|
70
|
-
case "number":
|
|
71
|
-
return applyOptionality(z.number());
|
|
72
|
-
case "boolean":
|
|
73
|
-
return applyOptionality(z.boolean());
|
|
74
|
-
case "json":
|
|
75
|
-
return applyOptionality(z.any());
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (desc.kind === "modelRef") {
|
|
80
|
-
const schema = modelZodSchemas[desc.model];
|
|
81
|
-
const base = schema ?? z.any();
|
|
82
|
-
// For loose schemas (root singletons), accept either the object or just an id.
|
|
83
|
-
const withId = opts.loose ? z.union([base, z.string()]) : base;
|
|
84
|
-
return applyOptionality(withId);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (desc.type === "object" && (desc as any).properties) {
|
|
88
|
-
const shape: Record<string, ZodType<any>> = {};
|
|
89
|
-
for (const [key, valueDesc] of Object.entries((desc as any).properties)) {
|
|
90
|
-
shape[key] = convertDescriptorToZod(
|
|
91
|
-
valueDesc as FieldDescriptor,
|
|
92
|
-
modelZodSchemas,
|
|
93
|
-
opts
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
const objectSchema = opts.loose ? z.object(shape).partial() : z.object(shape);
|
|
97
|
-
return applyOptionality(objectSchema);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (desc.type === "array" && (desc as any).items) {
|
|
101
|
-
return applyOptionality(
|
|
102
|
-
z.array(
|
|
103
|
-
convertDescriptorToZod(
|
|
104
|
-
(desc as any).items as FieldDescriptor,
|
|
105
|
-
modelZodSchemas,
|
|
106
|
-
opts
|
|
107
|
-
)
|
|
108
|
-
)
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (typeof (desc as any).type === "string") {
|
|
113
|
-
const custom = customTypeSchemas[(desc as any).type as string];
|
|
114
|
-
if (custom) {
|
|
115
|
-
return applyOptionality(custom);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return applyOptionality(z.any());
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function buildZodSchemasFromDescriptor(descriptor: FullDescriptor) {
|
|
123
|
-
const modelZodSchemas: Record<string, ZodType<any>> = {};
|
|
124
|
-
const zodSchemas: Record<string, ZodType<any>> = {};
|
|
125
|
-
|
|
126
|
-
for (const [modelName, modelDesc] of Object.entries(descriptor.models)) {
|
|
127
|
-
const shape: Record<string, ZodType<any>> = {};
|
|
128
|
-
for (const [propName, propDesc] of Object.entries(modelDesc.properties)) {
|
|
129
|
-
shape[propName] = convertDescriptorToZod(propDesc, modelZodSchemas);
|
|
130
|
-
}
|
|
131
|
-
modelZodSchemas[modelName] = z.object(shape);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
for (const [rootKey, rootDesc] of Object.entries(descriptor.roots)) {
|
|
135
|
-
// Root schemas are used for singleton PATCH; make nested structures loose/optional.
|
|
136
|
-
zodSchemas[rootKey] = convertDescriptorToZod(rootDesc, modelZodSchemas, {
|
|
137
|
-
loose: true,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return { zodSchemas, modelZodSchemas };
|
|
142
|
-
}
|