@cms0/shared 0.0.2 → 0.0.4
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.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/validation.d.ts +46 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +75 -0
- package/package.json +9 -6
- package/CHANGELOG.md +0 -8
- package/src/index.ts +0 -3
- package/src/validation.ts +0 -142
- package/tsconfig.json +0 -10
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAE7B,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,qBAA8C,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -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"), exports);
|
|
19
|
+
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
20
|
+
exports.delay = delay;
|
|
@@ -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"}
|
|
@@ -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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cms0/shared",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "restricted"
|
|
7
7
|
},
|
|
8
8
|
"main": "dist/index.js",
|
|
9
|
-
"types": "./
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
10
13
|
"exports": {
|
|
11
14
|
".": {
|
|
12
|
-
"types": "./
|
|
13
|
-
"import": "./
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
14
17
|
"require": "./dist/index.js"
|
|
15
18
|
},
|
|
16
19
|
"./*": {
|
|
17
|
-
"types": "./
|
|
18
|
-
"import": "./
|
|
20
|
+
"types": "./dist/*.d.ts",
|
|
21
|
+
"import": "./dist/*.js",
|
|
19
22
|
"require": "./dist/*.js"
|
|
20
23
|
}
|
|
21
24
|
},
|
package/CHANGELOG.md
DELETED
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
|
-
}
|