@cms0/shared 0.1.0 → 0.1.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/dist/cjs/index.js +1 -0
- package/dist/cjs/union.js +163 -0
- package/dist/cjs/validation.js +60 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/union.js +153 -0
- package/dist/esm/validation.js +60 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/union.d.ts +24 -0
- package/dist/types/union.d.ts.map +1 -0
- package/dist/types/validation.d.ts +34 -4
- package/dist/types/validation.d.ts.map +1 -1
- package/package.json +8 -9
- /package/{src → dist}/responsive-break.css +0 -0
package/dist/cjs/index.js
CHANGED
|
@@ -16,5 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.delay = void 0;
|
|
18
18
|
__exportStar(require("./validation.js"), exports);
|
|
19
|
+
__exportStar(require("./union.js"), exports);
|
|
19
20
|
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
20
21
|
exports.delay = delay;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CMS0_UNION_META_KEY = void 0;
|
|
4
|
+
exports.computeUnionBranchKeys = computeUnionBranchKeys;
|
|
5
|
+
exports.getUnionBranchKeys = getUnionBranchKeys;
|
|
6
|
+
exports.getUnionBranchKeyAt = getUnionBranchKeyAt;
|
|
7
|
+
exports.isTaggedUnionValue = isTaggedUnionValue;
|
|
8
|
+
exports.encodeTaggedUnionValue = encodeTaggedUnionValue;
|
|
9
|
+
exports.decodeTaggedUnionValue = decodeTaggedUnionValue;
|
|
10
|
+
exports.resolveTaggedUnionBranchIndex = resolveTaggedUnionBranchIndex;
|
|
11
|
+
exports.CMS0_UNION_META_KEY = "__cms0Union";
|
|
12
|
+
function isObjectRecord(value) {
|
|
13
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
function normalizeBranchShape(descriptor) {
|
|
16
|
+
if (descriptor?.kind === "modelRef") {
|
|
17
|
+
return {
|
|
18
|
+
kind: "modelRef",
|
|
19
|
+
model: descriptor.model,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (descriptor?.kind === "enum") {
|
|
23
|
+
return {
|
|
24
|
+
kind: "enum",
|
|
25
|
+
valueType: descriptor.valueType ?? "string",
|
|
26
|
+
values: Array.isArray(descriptor.values)
|
|
27
|
+
? [...descriptor.values]
|
|
28
|
+
: [],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (descriptor?.kind === "union") {
|
|
32
|
+
const branches = Array.isArray(descriptor?.anyOf)
|
|
33
|
+
? descriptor.anyOf
|
|
34
|
+
: [];
|
|
35
|
+
return {
|
|
36
|
+
kind: "union",
|
|
37
|
+
anyOf: branches.map((branch) => normalizeBranchShape(branch)),
|
|
38
|
+
...(typeof descriptor?.discriminator?.key === "string"
|
|
39
|
+
? { discriminator: { key: descriptor.discriminator.key } }
|
|
40
|
+
: {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (descriptor?.type === "array") {
|
|
44
|
+
return {
|
|
45
|
+
kind: "array",
|
|
46
|
+
items: normalizeBranchShape(descriptor.items),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (descriptor?.type === "object") {
|
|
50
|
+
const properties = descriptor?.properties &&
|
|
51
|
+
typeof descriptor.properties === "object"
|
|
52
|
+
? descriptor.properties
|
|
53
|
+
: {};
|
|
54
|
+
const normalizedProperties = Object.fromEntries(Object.keys(properties)
|
|
55
|
+
.sort((a, b) => a.localeCompare(b))
|
|
56
|
+
.map((key) => [key, normalizeBranchShape(properties[key])]));
|
|
57
|
+
return {
|
|
58
|
+
kind: "object",
|
|
59
|
+
properties: normalizedProperties,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
kind: "primitive",
|
|
64
|
+
type: descriptor?.type ?? "json",
|
|
65
|
+
...(typeof descriptor?.customType === "string"
|
|
66
|
+
? { customType: descriptor.customType }
|
|
67
|
+
: {}),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function stableStringify(value) {
|
|
71
|
+
if (value === null)
|
|
72
|
+
return "null";
|
|
73
|
+
if (typeof value === "string")
|
|
74
|
+
return JSON.stringify(value);
|
|
75
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
76
|
+
return String(value);
|
|
77
|
+
if (Array.isArray(value)) {
|
|
78
|
+
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
79
|
+
}
|
|
80
|
+
if (!isObjectRecord(value))
|
|
81
|
+
return JSON.stringify(value);
|
|
82
|
+
const entries = Object.keys(value)
|
|
83
|
+
.sort((a, b) => a.localeCompare(b))
|
|
84
|
+
.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`);
|
|
85
|
+
return `{${entries.join(",")}}`;
|
|
86
|
+
}
|
|
87
|
+
function fnv1a32(input) {
|
|
88
|
+
let hash = 0x811c9dc5;
|
|
89
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
90
|
+
hash ^= input.charCodeAt(i);
|
|
91
|
+
hash = Math.imul(hash, 0x01000193);
|
|
92
|
+
}
|
|
93
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
94
|
+
}
|
|
95
|
+
function computeUnionBranchKeys(branches) {
|
|
96
|
+
const used = new Set();
|
|
97
|
+
return branches.map((branch, index) => {
|
|
98
|
+
const signature = stableStringify(normalizeBranchShape(branch));
|
|
99
|
+
const base = `branch_${fnv1a32(signature)}`;
|
|
100
|
+
let key = base;
|
|
101
|
+
let suffix = 1;
|
|
102
|
+
while (used.has(key)) {
|
|
103
|
+
suffix += 1;
|
|
104
|
+
key = `${base}_${suffix}`;
|
|
105
|
+
}
|
|
106
|
+
used.add(key);
|
|
107
|
+
return key || `branch_${index + 1}`;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function getUnionBranchKeys(descriptor) {
|
|
111
|
+
const branches = Array.isArray(descriptor?.anyOf)
|
|
112
|
+
? descriptor.anyOf
|
|
113
|
+
: [];
|
|
114
|
+
if (!branches.length)
|
|
115
|
+
return [];
|
|
116
|
+
const explicit = Array.isArray(descriptor?.branchKeys)
|
|
117
|
+
? descriptor.branchKeys
|
|
118
|
+
: [];
|
|
119
|
+
const normalizedExplicit = explicit
|
|
120
|
+
.map((entry) => (typeof entry === "string" ? entry.trim() : ""))
|
|
121
|
+
.filter((entry) => entry.length > 0);
|
|
122
|
+
if (normalizedExplicit.length === branches.length &&
|
|
123
|
+
new Set(normalizedExplicit).size === normalizedExplicit.length) {
|
|
124
|
+
return normalizedExplicit;
|
|
125
|
+
}
|
|
126
|
+
return computeUnionBranchKeys(branches);
|
|
127
|
+
}
|
|
128
|
+
function getUnionBranchKeyAt(descriptor, index) {
|
|
129
|
+
const keys = getUnionBranchKeys(descriptor);
|
|
130
|
+
return keys[index];
|
|
131
|
+
}
|
|
132
|
+
function isTaggedUnionValue(value) {
|
|
133
|
+
if (!isObjectRecord(value))
|
|
134
|
+
return false;
|
|
135
|
+
const meta = value[exports.CMS0_UNION_META_KEY];
|
|
136
|
+
if (!isObjectRecord(meta))
|
|
137
|
+
return false;
|
|
138
|
+
if (typeof meta.branchKey !== "string" || !meta.branchKey.trim())
|
|
139
|
+
return false;
|
|
140
|
+
return Object.prototype.hasOwnProperty.call(value, "value");
|
|
141
|
+
}
|
|
142
|
+
function encodeTaggedUnionValue(branchKey, value) {
|
|
143
|
+
return {
|
|
144
|
+
[exports.CMS0_UNION_META_KEY]: { branchKey },
|
|
145
|
+
value,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function decodeTaggedUnionValue(value) {
|
|
149
|
+
if (!isTaggedUnionValue(value))
|
|
150
|
+
return null;
|
|
151
|
+
const meta = value[exports.CMS0_UNION_META_KEY];
|
|
152
|
+
return {
|
|
153
|
+
branchKey: meta.branchKey.trim(),
|
|
154
|
+
value: value.value,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function resolveTaggedUnionBranchIndex(descriptor, value) {
|
|
158
|
+
const decoded = decodeTaggedUnionValue(value);
|
|
159
|
+
if (!decoded)
|
|
160
|
+
return -1;
|
|
161
|
+
const keys = getUnionBranchKeys(descriptor);
|
|
162
|
+
return keys.findIndex((key) => key === decoded.branchKey);
|
|
163
|
+
}
|
package/dist/cjs/validation.js
CHANGED
|
@@ -29,6 +29,66 @@ function convertDescriptorToZod(desc, modelZodSchemas, opts = {}) {
|
|
|
29
29
|
return applyOptionality(zod_1.z.any());
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
+
if (desc.kind === "enum") {
|
|
33
|
+
const literals = Array.isArray(desc.values) ? desc.values : [];
|
|
34
|
+
if (!literals.length) {
|
|
35
|
+
switch (desc.valueType) {
|
|
36
|
+
case "number":
|
|
37
|
+
return applyOptionality(zod_1.z.number());
|
|
38
|
+
case "boolean":
|
|
39
|
+
return applyOptionality(zod_1.z.boolean());
|
|
40
|
+
case "string":
|
|
41
|
+
default:
|
|
42
|
+
return applyOptionality(zod_1.z.string());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (desc.valueType === "string") {
|
|
46
|
+
const values = literals
|
|
47
|
+
.filter((value) => typeof value === "string")
|
|
48
|
+
.map((value) => value.trim())
|
|
49
|
+
.filter((value) => value.length > 0);
|
|
50
|
+
if (!values.length) {
|
|
51
|
+
return applyOptionality(zod_1.z.string());
|
|
52
|
+
}
|
|
53
|
+
const unique = Array.from(new Set(values));
|
|
54
|
+
const [first, ...rest] = unique;
|
|
55
|
+
if (!first)
|
|
56
|
+
return applyOptionality(zod_1.z.string());
|
|
57
|
+
return applyOptionality(zod_1.z.enum([first, ...rest]));
|
|
58
|
+
}
|
|
59
|
+
if (desc.valueType === "number") {
|
|
60
|
+
const values = literals.filter((value) => typeof value === "number" && Number.isFinite(value));
|
|
61
|
+
if (!values.length)
|
|
62
|
+
return applyOptionality(zod_1.z.number());
|
|
63
|
+
const unique = Array.from(new Set(values));
|
|
64
|
+
const [first, ...rest] = unique;
|
|
65
|
+
if (first === undefined)
|
|
66
|
+
return applyOptionality(zod_1.z.number());
|
|
67
|
+
const schema = rest.length
|
|
68
|
+
? zod_1.z.union([zod_1.z.literal(first), ...rest.map((value) => zod_1.z.literal(value))])
|
|
69
|
+
: zod_1.z.literal(first);
|
|
70
|
+
return applyOptionality(schema);
|
|
71
|
+
}
|
|
72
|
+
const values = literals.filter((value) => typeof value === "boolean");
|
|
73
|
+
if (!values.length)
|
|
74
|
+
return applyOptionality(zod_1.z.boolean());
|
|
75
|
+
if (values.includes(true) && values.includes(false)) {
|
|
76
|
+
return applyOptionality(zod_1.z.boolean());
|
|
77
|
+
}
|
|
78
|
+
return applyOptionality(zod_1.z.literal(values[0]));
|
|
79
|
+
}
|
|
80
|
+
if (desc.kind === "union") {
|
|
81
|
+
const branches = (Array.isArray(desc.anyOf) ? desc.anyOf : [])
|
|
82
|
+
.map((branch) => convertDescriptorToZod(branch, modelZodSchemas, opts))
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
if (!branches.length)
|
|
85
|
+
return applyOptionality(zod_1.z.any());
|
|
86
|
+
if (branches.length === 1)
|
|
87
|
+
return applyOptionality(branches[0]);
|
|
88
|
+
const [first, second, ...rest] = branches;
|
|
89
|
+
const schema = zod_1.z.union([first, second, ...rest]);
|
|
90
|
+
return applyOptionality(schema);
|
|
91
|
+
}
|
|
32
92
|
if (desc.kind === "modelRef") {
|
|
33
93
|
const schema = modelZodSchemas[desc.model];
|
|
34
94
|
const base = schema ?? zod_1.z.any();
|
package/dist/esm/index.js
CHANGED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export const CMS0_UNION_META_KEY = "__cms0Union";
|
|
2
|
+
function isObjectRecord(value) {
|
|
3
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function normalizeBranchShape(descriptor) {
|
|
6
|
+
if (descriptor?.kind === "modelRef") {
|
|
7
|
+
return {
|
|
8
|
+
kind: "modelRef",
|
|
9
|
+
model: descriptor.model,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
if (descriptor?.kind === "enum") {
|
|
13
|
+
return {
|
|
14
|
+
kind: "enum",
|
|
15
|
+
valueType: descriptor.valueType ?? "string",
|
|
16
|
+
values: Array.isArray(descriptor.values)
|
|
17
|
+
? [...descriptor.values]
|
|
18
|
+
: [],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (descriptor?.kind === "union") {
|
|
22
|
+
const branches = Array.isArray(descriptor?.anyOf)
|
|
23
|
+
? descriptor.anyOf
|
|
24
|
+
: [];
|
|
25
|
+
return {
|
|
26
|
+
kind: "union",
|
|
27
|
+
anyOf: branches.map((branch) => normalizeBranchShape(branch)),
|
|
28
|
+
...(typeof descriptor?.discriminator?.key === "string"
|
|
29
|
+
? { discriminator: { key: descriptor.discriminator.key } }
|
|
30
|
+
: {}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (descriptor?.type === "array") {
|
|
34
|
+
return {
|
|
35
|
+
kind: "array",
|
|
36
|
+
items: normalizeBranchShape(descriptor.items),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (descriptor?.type === "object") {
|
|
40
|
+
const properties = descriptor?.properties &&
|
|
41
|
+
typeof descriptor.properties === "object"
|
|
42
|
+
? descriptor.properties
|
|
43
|
+
: {};
|
|
44
|
+
const normalizedProperties = Object.fromEntries(Object.keys(properties)
|
|
45
|
+
.sort((a, b) => a.localeCompare(b))
|
|
46
|
+
.map((key) => [key, normalizeBranchShape(properties[key])]));
|
|
47
|
+
return {
|
|
48
|
+
kind: "object",
|
|
49
|
+
properties: normalizedProperties,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
kind: "primitive",
|
|
54
|
+
type: descriptor?.type ?? "json",
|
|
55
|
+
...(typeof descriptor?.customType === "string"
|
|
56
|
+
? { customType: descriptor.customType }
|
|
57
|
+
: {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function stableStringify(value) {
|
|
61
|
+
if (value === null)
|
|
62
|
+
return "null";
|
|
63
|
+
if (typeof value === "string")
|
|
64
|
+
return JSON.stringify(value);
|
|
65
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
66
|
+
return String(value);
|
|
67
|
+
if (Array.isArray(value)) {
|
|
68
|
+
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
69
|
+
}
|
|
70
|
+
if (!isObjectRecord(value))
|
|
71
|
+
return JSON.stringify(value);
|
|
72
|
+
const entries = Object.keys(value)
|
|
73
|
+
.sort((a, b) => a.localeCompare(b))
|
|
74
|
+
.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`);
|
|
75
|
+
return `{${entries.join(",")}}`;
|
|
76
|
+
}
|
|
77
|
+
function fnv1a32(input) {
|
|
78
|
+
let hash = 0x811c9dc5;
|
|
79
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
80
|
+
hash ^= input.charCodeAt(i);
|
|
81
|
+
hash = Math.imul(hash, 0x01000193);
|
|
82
|
+
}
|
|
83
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
84
|
+
}
|
|
85
|
+
export function computeUnionBranchKeys(branches) {
|
|
86
|
+
const used = new Set();
|
|
87
|
+
return branches.map((branch, index) => {
|
|
88
|
+
const signature = stableStringify(normalizeBranchShape(branch));
|
|
89
|
+
const base = `branch_${fnv1a32(signature)}`;
|
|
90
|
+
let key = base;
|
|
91
|
+
let suffix = 1;
|
|
92
|
+
while (used.has(key)) {
|
|
93
|
+
suffix += 1;
|
|
94
|
+
key = `${base}_${suffix}`;
|
|
95
|
+
}
|
|
96
|
+
used.add(key);
|
|
97
|
+
return key || `branch_${index + 1}`;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
export function getUnionBranchKeys(descriptor) {
|
|
101
|
+
const branches = Array.isArray(descriptor?.anyOf)
|
|
102
|
+
? descriptor.anyOf
|
|
103
|
+
: [];
|
|
104
|
+
if (!branches.length)
|
|
105
|
+
return [];
|
|
106
|
+
const explicit = Array.isArray(descriptor?.branchKeys)
|
|
107
|
+
? descriptor.branchKeys
|
|
108
|
+
: [];
|
|
109
|
+
const normalizedExplicit = explicit
|
|
110
|
+
.map((entry) => (typeof entry === "string" ? entry.trim() : ""))
|
|
111
|
+
.filter((entry) => entry.length > 0);
|
|
112
|
+
if (normalizedExplicit.length === branches.length &&
|
|
113
|
+
new Set(normalizedExplicit).size === normalizedExplicit.length) {
|
|
114
|
+
return normalizedExplicit;
|
|
115
|
+
}
|
|
116
|
+
return computeUnionBranchKeys(branches);
|
|
117
|
+
}
|
|
118
|
+
export function getUnionBranchKeyAt(descriptor, index) {
|
|
119
|
+
const keys = getUnionBranchKeys(descriptor);
|
|
120
|
+
return keys[index];
|
|
121
|
+
}
|
|
122
|
+
export function isTaggedUnionValue(value) {
|
|
123
|
+
if (!isObjectRecord(value))
|
|
124
|
+
return false;
|
|
125
|
+
const meta = value[CMS0_UNION_META_KEY];
|
|
126
|
+
if (!isObjectRecord(meta))
|
|
127
|
+
return false;
|
|
128
|
+
if (typeof meta.branchKey !== "string" || !meta.branchKey.trim())
|
|
129
|
+
return false;
|
|
130
|
+
return Object.prototype.hasOwnProperty.call(value, "value");
|
|
131
|
+
}
|
|
132
|
+
export function encodeTaggedUnionValue(branchKey, value) {
|
|
133
|
+
return {
|
|
134
|
+
[CMS0_UNION_META_KEY]: { branchKey },
|
|
135
|
+
value,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
export function decodeTaggedUnionValue(value) {
|
|
139
|
+
if (!isTaggedUnionValue(value))
|
|
140
|
+
return null;
|
|
141
|
+
const meta = value[CMS0_UNION_META_KEY];
|
|
142
|
+
return {
|
|
143
|
+
branchKey: meta.branchKey.trim(),
|
|
144
|
+
value: value.value,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
export function resolveTaggedUnionBranchIndex(descriptor, value) {
|
|
148
|
+
const decoded = decodeTaggedUnionValue(value);
|
|
149
|
+
if (!decoded)
|
|
150
|
+
return -1;
|
|
151
|
+
const keys = getUnionBranchKeys(descriptor);
|
|
152
|
+
return keys.findIndex((key) => key === decoded.branchKey);
|
|
153
|
+
}
|
package/dist/esm/validation.js
CHANGED
|
@@ -26,6 +26,66 @@ function convertDescriptorToZod(desc, modelZodSchemas, opts = {}) {
|
|
|
26
26
|
return applyOptionality(z.any());
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
+
if (desc.kind === "enum") {
|
|
30
|
+
const literals = Array.isArray(desc.values) ? desc.values : [];
|
|
31
|
+
if (!literals.length) {
|
|
32
|
+
switch (desc.valueType) {
|
|
33
|
+
case "number":
|
|
34
|
+
return applyOptionality(z.number());
|
|
35
|
+
case "boolean":
|
|
36
|
+
return applyOptionality(z.boolean());
|
|
37
|
+
case "string":
|
|
38
|
+
default:
|
|
39
|
+
return applyOptionality(z.string());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (desc.valueType === "string") {
|
|
43
|
+
const values = literals
|
|
44
|
+
.filter((value) => typeof value === "string")
|
|
45
|
+
.map((value) => value.trim())
|
|
46
|
+
.filter((value) => value.length > 0);
|
|
47
|
+
if (!values.length) {
|
|
48
|
+
return applyOptionality(z.string());
|
|
49
|
+
}
|
|
50
|
+
const unique = Array.from(new Set(values));
|
|
51
|
+
const [first, ...rest] = unique;
|
|
52
|
+
if (!first)
|
|
53
|
+
return applyOptionality(z.string());
|
|
54
|
+
return applyOptionality(z.enum([first, ...rest]));
|
|
55
|
+
}
|
|
56
|
+
if (desc.valueType === "number") {
|
|
57
|
+
const values = literals.filter((value) => typeof value === "number" && Number.isFinite(value));
|
|
58
|
+
if (!values.length)
|
|
59
|
+
return applyOptionality(z.number());
|
|
60
|
+
const unique = Array.from(new Set(values));
|
|
61
|
+
const [first, ...rest] = unique;
|
|
62
|
+
if (first === undefined)
|
|
63
|
+
return applyOptionality(z.number());
|
|
64
|
+
const schema = rest.length
|
|
65
|
+
? z.union([z.literal(first), ...rest.map((value) => z.literal(value))])
|
|
66
|
+
: z.literal(first);
|
|
67
|
+
return applyOptionality(schema);
|
|
68
|
+
}
|
|
69
|
+
const values = literals.filter((value) => typeof value === "boolean");
|
|
70
|
+
if (!values.length)
|
|
71
|
+
return applyOptionality(z.boolean());
|
|
72
|
+
if (values.includes(true) && values.includes(false)) {
|
|
73
|
+
return applyOptionality(z.boolean());
|
|
74
|
+
}
|
|
75
|
+
return applyOptionality(z.literal(values[0]));
|
|
76
|
+
}
|
|
77
|
+
if (desc.kind === "union") {
|
|
78
|
+
const branches = (Array.isArray(desc.anyOf) ? desc.anyOf : [])
|
|
79
|
+
.map((branch) => convertDescriptorToZod(branch, modelZodSchemas, opts))
|
|
80
|
+
.filter(Boolean);
|
|
81
|
+
if (!branches.length)
|
|
82
|
+
return applyOptionality(z.any());
|
|
83
|
+
if (branches.length === 1)
|
|
84
|
+
return applyOptionality(branches[0]);
|
|
85
|
+
const [first, second, ...rest] = branches;
|
|
86
|
+
const schema = z.union([first, second, ...rest]);
|
|
87
|
+
return applyOptionality(schema);
|
|
88
|
+
}
|
|
29
89
|
if (desc.kind === "modelRef") {
|
|
30
90
|
const schema = modelZodSchemas[desc.model];
|
|
31
91
|
const base = schema ?? z.any();
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAE3B,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,qBAA8C,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FieldDescriptor } from "./validation.js";
|
|
2
|
+
export declare const CMS0_UNION_META_KEY: "__cms0Union";
|
|
3
|
+
export type Cms0TaggedUnionMeta = {
|
|
4
|
+
branchKey: string;
|
|
5
|
+
};
|
|
6
|
+
export type Cms0TaggedUnionValue = {
|
|
7
|
+
[CMS0_UNION_META_KEY]: Cms0TaggedUnionMeta;
|
|
8
|
+
value: unknown;
|
|
9
|
+
};
|
|
10
|
+
type UnionDescriptor = Extract<FieldDescriptor, {
|
|
11
|
+
kind: "union";
|
|
12
|
+
}>;
|
|
13
|
+
export declare function computeUnionBranchKeys(branches: FieldDescriptor[]): string[];
|
|
14
|
+
export declare function getUnionBranchKeys(descriptor: UnionDescriptor): string[];
|
|
15
|
+
export declare function getUnionBranchKeyAt(descriptor: UnionDescriptor, index: number): string | undefined;
|
|
16
|
+
export declare function isTaggedUnionValue(value: unknown): value is Cms0TaggedUnionValue;
|
|
17
|
+
export declare function encodeTaggedUnionValue(branchKey: string, value: unknown): Cms0TaggedUnionValue;
|
|
18
|
+
export declare function decodeTaggedUnionValue(value: unknown): {
|
|
19
|
+
branchKey: string;
|
|
20
|
+
value: unknown;
|
|
21
|
+
} | null;
|
|
22
|
+
export declare function resolveTaggedUnionBranchIndex(descriptor: UnionDescriptor, value: unknown): number;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=union.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/union.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,eAAO,MAAM,mBAAmB,EAAG,aAAsB,CAAC;AAE1D,MAAM,MAAM,mBAAmB,GAAG;IAChC,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,CAAC,mBAAmB,CAAC,EAAE,mBAAmB,CAAC;IAC3C,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,KAAK,eAAe,GAAG,OAAO,CAAC,eAAe,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AA8FnE,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,eAAe,EAAE,GAAG,MAAM,EAAE,CAc5E;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,eAAe,GAAG,MAAM,EAAE,CAqBxE;AAED,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,eAAe,EAC3B,KAAK,EAAE,MAAM,GACZ,MAAM,GAAG,SAAS,CAGpB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,oBAAoB,CAMhF;AAED,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,GACb,oBAAoB,CAKtB;AAED,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,GACb;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,CAO9C;AAED,wBAAgB,6BAA6B,CAC3C,UAAU,EAAE,eAAe,EAC3B,KAAK,EAAE,OAAO,GACb,MAAM,CAKR"}
|
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
export type PrimitiveType = "string" | "number" | "boolean" | "json";
|
|
3
|
-
export type
|
|
3
|
+
export type EnumValueType = Exclude<PrimitiveType, "json">;
|
|
4
|
+
export type EnumValue = string | number | boolean;
|
|
5
|
+
type PrimitiveFieldDescriptor = {
|
|
4
6
|
kind?: "primitive";
|
|
5
7
|
type: PrimitiveType;
|
|
6
8
|
optional?: boolean;
|
|
7
9
|
nullable?: boolean;
|
|
8
10
|
customType?: string;
|
|
9
|
-
}
|
|
11
|
+
};
|
|
12
|
+
type ModelRefFieldDescriptor = {
|
|
10
13
|
kind: "modelRef";
|
|
11
14
|
model: string;
|
|
12
15
|
optional?: boolean;
|
|
13
16
|
nullable?: boolean;
|
|
14
|
-
}
|
|
17
|
+
};
|
|
18
|
+
type ObjectFieldDescriptor = {
|
|
15
19
|
kind?: "object";
|
|
16
20
|
type: "object";
|
|
17
21
|
properties: Record<string, FieldDescriptor>;
|
|
22
|
+
propertyOrder?: string[];
|
|
18
23
|
optional?: boolean;
|
|
19
24
|
nullable?: boolean;
|
|
20
25
|
customType?: string;
|
|
21
|
-
}
|
|
26
|
+
};
|
|
27
|
+
type ArrayFieldDescriptor = {
|
|
22
28
|
kind?: "array";
|
|
23
29
|
type: "array";
|
|
24
30
|
items: FieldDescriptor;
|
|
@@ -26,9 +32,28 @@ export type FieldDescriptor = {
|
|
|
26
32
|
nullable?: boolean;
|
|
27
33
|
customType?: string;
|
|
28
34
|
};
|
|
35
|
+
type EnumFieldDescriptor = {
|
|
36
|
+
kind: "enum";
|
|
37
|
+
valueType: EnumValueType;
|
|
38
|
+
values: EnumValue[];
|
|
39
|
+
optional?: boolean;
|
|
40
|
+
nullable?: boolean;
|
|
41
|
+
};
|
|
42
|
+
type UnionFieldDescriptor = {
|
|
43
|
+
kind: "union";
|
|
44
|
+
anyOf: FieldDescriptor[];
|
|
45
|
+
branchKeys?: string[];
|
|
46
|
+
discriminator?: {
|
|
47
|
+
key: string;
|
|
48
|
+
};
|
|
49
|
+
optional?: boolean;
|
|
50
|
+
nullable?: boolean;
|
|
51
|
+
};
|
|
52
|
+
export type FieldDescriptor = PrimitiveFieldDescriptor | ModelRefFieldDescriptor | ObjectFieldDescriptor | ArrayFieldDescriptor | EnumFieldDescriptor | UnionFieldDescriptor;
|
|
29
53
|
export type ModelDescriptor = {
|
|
30
54
|
kind: "model";
|
|
31
55
|
properties: Record<string, FieldDescriptor>;
|
|
56
|
+
propertyOrder?: string[];
|
|
32
57
|
};
|
|
33
58
|
export type RootDescriptor = FieldDescriptor;
|
|
34
59
|
export type FullDescriptor = {
|
|
@@ -37,10 +62,15 @@ export type FullDescriptor = {
|
|
|
37
62
|
metadata?: {
|
|
38
63
|
locales?: string[];
|
|
39
64
|
defaultLocale?: string;
|
|
65
|
+
ordering?: {
|
|
66
|
+
roots?: string[];
|
|
67
|
+
models?: string[];
|
|
68
|
+
};
|
|
40
69
|
};
|
|
41
70
|
};
|
|
42
71
|
export declare function buildZodSchemasFromDescriptor(descriptor: FullDescriptor): {
|
|
43
72
|
zodSchemas: Record<string, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>;
|
|
44
73
|
modelZodSchemas: Record<string, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>;
|
|
45
74
|
};
|
|
75
|
+
export {};
|
|
46
76
|
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +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;
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cms0/shared",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
"module": "./dist/esm/index.js",
|
|
11
11
|
"types": "./dist/types/index.d.ts",
|
|
12
12
|
"files": [
|
|
13
|
-
"dist"
|
|
14
|
-
"src/responsive-break.css"
|
|
13
|
+
"dist"
|
|
15
14
|
],
|
|
16
15
|
"exports": {
|
|
17
16
|
".": {
|
|
@@ -19,12 +18,12 @@
|
|
|
19
18
|
"import": "./dist/esm/index.js",
|
|
20
19
|
"require": "./dist/cjs/index.js"
|
|
21
20
|
},
|
|
22
|
-
"
|
|
23
|
-
"types": "./dist/types
|
|
24
|
-
"import": "./dist/esm
|
|
25
|
-
"require": "./dist/cjs
|
|
21
|
+
"./validation": {
|
|
22
|
+
"types": "./dist/types/validation.d.ts",
|
|
23
|
+
"import": "./dist/esm/validation.js",
|
|
24
|
+
"require": "./dist/cjs/validation.js"
|
|
26
25
|
},
|
|
27
|
-
"./responsive-break.css": "./
|
|
26
|
+
"./responsive-break.css": "./dist/responsive-break.css"
|
|
28
27
|
},
|
|
29
28
|
"dependencies": {
|
|
30
29
|
"zod": "^4.1.12"
|
|
@@ -39,7 +38,7 @@
|
|
|
39
38
|
"build:clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true });\"",
|
|
40
39
|
"build:esm": "tsc -p tsconfig.json",
|
|
41
40
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
42
|
-
"build:post": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\\\n');\"",
|
|
41
|
+
"build:post": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\\\n');fs.copyFileSync('src/responsive-break.css','dist/responsive-break.css');\"",
|
|
43
42
|
"dev": "tsc -b --watch"
|
|
44
43
|
}
|
|
45
44
|
}
|
|
File without changes
|