@cms0/shared 0.0.1
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 +34 -0
- package/src/index.ts +3 -0
- package/src/validation.ts +133 -0
- package/tsconfig.json +10 -0
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cms0/shared",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "restricted"
|
|
7
|
+
},
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"types": "./src/index.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./src/index.ts",
|
|
13
|
+
"import": "./src/index.ts",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./*": {
|
|
17
|
+
"types": "./src/*.ts",
|
|
18
|
+
"import": "./src/*.ts",
|
|
19
|
+
"require": "./dist/*.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"zod": "^4.1.12"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.0.0",
|
|
27
|
+
"typescript": "^5.0.0",
|
|
28
|
+
"@cms0/typescript-config": "0.0.1"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc",
|
|
32
|
+
"dev": "tsc -b --watch"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
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";
|
|
11
|
+
// Type definitions for descriptor elements
|
|
12
|
+
export type FieldDescriptor =
|
|
13
|
+
| {
|
|
14
|
+
kind?: "primitive";
|
|
15
|
+
type: PrimitiveType;
|
|
16
|
+
optional?: boolean;
|
|
17
|
+
nullable?: boolean;
|
|
18
|
+
}
|
|
19
|
+
| { kind: "modelRef"; model: string; optional?: boolean; nullable?: boolean }
|
|
20
|
+
| {
|
|
21
|
+
kind?: "object";
|
|
22
|
+
type: "object";
|
|
23
|
+
properties: Record<string, FieldDescriptor>;
|
|
24
|
+
optional?: boolean;
|
|
25
|
+
nullable?: boolean;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
kind?: "array";
|
|
29
|
+
type: "array";
|
|
30
|
+
items: FieldDescriptor;
|
|
31
|
+
optional?: boolean;
|
|
32
|
+
nullable?: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type ModelDescriptor = {
|
|
36
|
+
kind: "model";
|
|
37
|
+
properties: Record<string, FieldDescriptor>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type RootDescriptor = FieldDescriptor;
|
|
41
|
+
|
|
42
|
+
export type FullDescriptor = {
|
|
43
|
+
models: Record<string, ModelDescriptor>;
|
|
44
|
+
roots: Record<string, RootDescriptor>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function convertDescriptorToZod(
|
|
48
|
+
desc: FieldDescriptor,
|
|
49
|
+
modelZodSchemas: Record<string, ZodType<any>>,
|
|
50
|
+
opts: { loose?: boolean } = {}
|
|
51
|
+
): ZodType<any> {
|
|
52
|
+
const applyOptionality = (schema: ZodType<any>) => {
|
|
53
|
+
let out = schema;
|
|
54
|
+
if (desc.nullable || opts.loose) out = out.nullable();
|
|
55
|
+
if (desc.optional || opts.loose) out = out.optional();
|
|
56
|
+
return out;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if (desc.kind === "primitive") {
|
|
60
|
+
switch (desc.type) {
|
|
61
|
+
case "string":
|
|
62
|
+
return applyOptionality(z.string());
|
|
63
|
+
case "number":
|
|
64
|
+
return applyOptionality(z.number());
|
|
65
|
+
case "boolean":
|
|
66
|
+
return applyOptionality(z.boolean());
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (desc.kind === "modelRef") {
|
|
71
|
+
const schema = modelZodSchemas[desc.model];
|
|
72
|
+
const base = schema ?? z.any();
|
|
73
|
+
// For loose schemas (root singletons), accept either the object or just an id.
|
|
74
|
+
const withId = opts.loose ? z.union([base, z.string()]) : base;
|
|
75
|
+
return applyOptionality(withId);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (desc.type === "object" && (desc as any).properties) {
|
|
79
|
+
const shape: Record<string, ZodType<any>> = {};
|
|
80
|
+
for (const [key, valueDesc] of Object.entries((desc as any).properties)) {
|
|
81
|
+
shape[key] = convertDescriptorToZod(
|
|
82
|
+
valueDesc as FieldDescriptor,
|
|
83
|
+
modelZodSchemas,
|
|
84
|
+
opts
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const objectSchema = opts.loose ? z.object(shape).partial() : z.object(shape);
|
|
88
|
+
return applyOptionality(objectSchema);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (desc.type === "array" && (desc as any).items) {
|
|
92
|
+
return applyOptionality(
|
|
93
|
+
z.array(
|
|
94
|
+
convertDescriptorToZod(
|
|
95
|
+
(desc as any).items as FieldDescriptor,
|
|
96
|
+
modelZodSchemas,
|
|
97
|
+
opts
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (typeof (desc as any).type === "string") {
|
|
104
|
+
const custom = customTypeSchemas[(desc as any).type as string];
|
|
105
|
+
if (custom) {
|
|
106
|
+
return applyOptionality(custom);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return applyOptionality(z.any());
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function buildZodSchemasFromDescriptor(descriptor: FullDescriptor) {
|
|
114
|
+
const modelZodSchemas: Record<string, ZodType<any>> = {};
|
|
115
|
+
const zodSchemas: Record<string, ZodType<any>> = {};
|
|
116
|
+
|
|
117
|
+
for (const [modelName, modelDesc] of Object.entries(descriptor.models)) {
|
|
118
|
+
const shape: Record<string, ZodType<any>> = {};
|
|
119
|
+
for (const [propName, propDesc] of Object.entries(modelDesc.properties)) {
|
|
120
|
+
shape[propName] = convertDescriptorToZod(propDesc, modelZodSchemas);
|
|
121
|
+
}
|
|
122
|
+
modelZodSchemas[modelName] = z.object(shape);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const [rootKey, rootDesc] of Object.entries(descriptor.roots)) {
|
|
126
|
+
// Root schemas are used for singleton PATCH; make nested structures loose/optional.
|
|
127
|
+
zodSchemas[rootKey] = convertDescriptorToZod(rootDesc, modelZodSchemas, {
|
|
128
|
+
loose: true,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { zodSchemas, modelZodSchemas };
|
|
133
|
+
}
|