@cms0/shared 0.1.0 → 0.2.14
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/descriptor-capabilities.js +404 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/union.js +170 -0
- package/dist/cjs/validation.js +60 -0
- package/dist/esm/descriptor-capabilities.js +385 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/union.js +159 -0
- package/dist/esm/validation.js +60 -0
- package/dist/types/descriptor-capabilities.d.ts +90 -0
- package/dist/types/descriptor-capabilities.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/union.d.ts +25 -0
- package/dist/types/union.d.ts.map +1 -0
- package/dist/types/validation.d.ts +38 -4
- package/dist/types/validation.d.ts.map +1 -1
- package/package.json +13 -10
- /package/{src → dist}/responsive-break.css +0 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isPrimitiveDescriptor = isPrimitiveDescriptor;
|
|
4
|
+
exports.isEnumDescriptor = isEnumDescriptor;
|
|
5
|
+
exports.isUnionDescriptor = isUnionDescriptor;
|
|
6
|
+
exports.isModelRefDescriptor = isModelRefDescriptor;
|
|
7
|
+
exports.isObjectDescriptor = isObjectDescriptor;
|
|
8
|
+
exports.isArrayDescriptor = isArrayDescriptor;
|
|
9
|
+
exports.getModelDescriptor = getModelDescriptor;
|
|
10
|
+
exports.getDescriptorPropertyEntries = getDescriptorPropertyEntries;
|
|
11
|
+
exports.resolveAssetKindFromModelDescriptor = resolveAssetKindFromModelDescriptor;
|
|
12
|
+
exports.resolveDescriptorCapability = resolveDescriptorCapability;
|
|
13
|
+
exports.getDescriptorDefaultValue = getDescriptorDefaultValue;
|
|
14
|
+
exports.normalizeEnumValue = normalizeEnumValue;
|
|
15
|
+
exports.formatEnumOptionValue = formatEnumOptionValue;
|
|
16
|
+
exports.parseEnumOptionValue = parseEnumOptionValue;
|
|
17
|
+
exports.resolveUnionBranchIndex = resolveUnionBranchIndex;
|
|
18
|
+
exports.normalizeUnionValue = normalizeUnionValue;
|
|
19
|
+
exports.getDescriptorDisplayName = getDescriptorDisplayName;
|
|
20
|
+
const union_js_1 = require("./union.js");
|
|
21
|
+
function isPlainRecord(value) {
|
|
22
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
23
|
+
}
|
|
24
|
+
function isPrimitiveDescriptor(descriptor) {
|
|
25
|
+
if (!descriptor)
|
|
26
|
+
return false;
|
|
27
|
+
const type = descriptor.type;
|
|
28
|
+
return (descriptor.kind === "primitive" ||
|
|
29
|
+
(!descriptor.kind && type !== "object" && type !== "array"));
|
|
30
|
+
}
|
|
31
|
+
function isEnumDescriptor(descriptor) {
|
|
32
|
+
return descriptor?.kind === "enum";
|
|
33
|
+
}
|
|
34
|
+
function isUnionDescriptor(descriptor) {
|
|
35
|
+
return descriptor?.kind === "union";
|
|
36
|
+
}
|
|
37
|
+
function isModelRefDescriptor(descriptor) {
|
|
38
|
+
return descriptor?.kind === "modelRef";
|
|
39
|
+
}
|
|
40
|
+
function isObjectDescriptor(descriptor) {
|
|
41
|
+
return !!descriptor && descriptor.type === "object";
|
|
42
|
+
}
|
|
43
|
+
function isArrayDescriptor(descriptor) {
|
|
44
|
+
return !!descriptor && descriptor.type === "array";
|
|
45
|
+
}
|
|
46
|
+
function getModelDescriptor(fullDescriptor, modelName) {
|
|
47
|
+
if (!fullDescriptor || !modelName)
|
|
48
|
+
return undefined;
|
|
49
|
+
return fullDescriptor.models?.[modelName];
|
|
50
|
+
}
|
|
51
|
+
function getDescriptorPropertyEntries(descriptor) {
|
|
52
|
+
const properties = descriptor.properties ?? {};
|
|
53
|
+
const order = Array.isArray(descriptor.propertyOrder)
|
|
54
|
+
? descriptor.propertyOrder
|
|
55
|
+
: [];
|
|
56
|
+
const ordered = order
|
|
57
|
+
.filter((key) => Object.prototype.hasOwnProperty.call(properties, key))
|
|
58
|
+
.map((key) => [key, properties[key]]);
|
|
59
|
+
const remaining = Object.entries(properties)
|
|
60
|
+
.filter(([key]) => !order.includes(key))
|
|
61
|
+
.sort(([left], [right]) => left.localeCompare(right));
|
|
62
|
+
return [...ordered, ...remaining];
|
|
63
|
+
}
|
|
64
|
+
function resolveAssetKindFromModelDescriptor(modelName, modelDescriptor) {
|
|
65
|
+
const presentation = modelDescriptor?.presentation;
|
|
66
|
+
if (presentation?.kind === "asset") {
|
|
67
|
+
if (presentation.assetKind === "image")
|
|
68
|
+
return "image";
|
|
69
|
+
if (presentation.assetKind === "video")
|
|
70
|
+
return "video";
|
|
71
|
+
return "file";
|
|
72
|
+
}
|
|
73
|
+
if (modelName === "Image")
|
|
74
|
+
return "image";
|
|
75
|
+
if (modelName === "Video")
|
|
76
|
+
return "video";
|
|
77
|
+
if (modelName === "File")
|
|
78
|
+
return "file";
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
function resolveDescriptorCapability(descriptor, fullDescriptor) {
|
|
82
|
+
if (isUnionDescriptor(descriptor)) {
|
|
83
|
+
const keys = (0, union_js_1.getUnionBranchKeys)(descriptor);
|
|
84
|
+
const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
|
|
85
|
+
return {
|
|
86
|
+
kind: "union",
|
|
87
|
+
discriminatorKey: descriptor.discriminator?.key,
|
|
88
|
+
branches: branches.map((branch, index) => ({
|
|
89
|
+
index,
|
|
90
|
+
key: keys[index] ?? (0, union_js_1.getUnionBranchKeyAt)(descriptor, index) ?? `branch_${index + 1}`,
|
|
91
|
+
descriptor: branch,
|
|
92
|
+
capability: resolveDescriptorCapability(branch, fullDescriptor),
|
|
93
|
+
label: getDescriptorDisplayName(branch, index),
|
|
94
|
+
})),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (isModelRefDescriptor(descriptor)) {
|
|
98
|
+
const modelDescriptor = getModelDescriptor(fullDescriptor, descriptor.model);
|
|
99
|
+
const assetKind = resolveAssetKindFromModelDescriptor(descriptor.model, modelDescriptor);
|
|
100
|
+
if (assetKind) {
|
|
101
|
+
return {
|
|
102
|
+
kind: "asset-ref",
|
|
103
|
+
modelName: descriptor.model,
|
|
104
|
+
assetKind,
|
|
105
|
+
modelDescriptor,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
kind: "model-ref",
|
|
110
|
+
modelName: descriptor.model,
|
|
111
|
+
modelDescriptor,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (isEnumDescriptor(descriptor)) {
|
|
115
|
+
return {
|
|
116
|
+
kind: "enum",
|
|
117
|
+
valueType: descriptor.valueType,
|
|
118
|
+
options: Array.isArray(descriptor.values) ? descriptor.values.slice() : [],
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (isObjectDescriptor(descriptor)) {
|
|
122
|
+
return {
|
|
123
|
+
kind: "object-inline",
|
|
124
|
+
properties: getDescriptorPropertyEntries(descriptor),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (isArrayDescriptor(descriptor)) {
|
|
128
|
+
return {
|
|
129
|
+
kind: "collection-inline",
|
|
130
|
+
itemDescriptor: descriptor.items,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (!isPrimitiveDescriptor(descriptor)) {
|
|
134
|
+
return {
|
|
135
|
+
kind: "unsupported",
|
|
136
|
+
reason: "Unsupported descriptor kind.",
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (descriptor.customType === "LocalizedString") {
|
|
140
|
+
return { kind: "localized-string" };
|
|
141
|
+
}
|
|
142
|
+
if (descriptor.customType === "RichText") {
|
|
143
|
+
return { kind: "rich-text" };
|
|
144
|
+
}
|
|
145
|
+
if (descriptor.customType === "LocalizedRichText") {
|
|
146
|
+
return { kind: "localized-rich-text" };
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
kind: "scalar",
|
|
150
|
+
primitiveType: descriptor.type,
|
|
151
|
+
customType: descriptor.customType,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function getDescriptorDefaultValue(descriptor, options = {}) {
|
|
155
|
+
const capability = resolveDescriptorCapability(descriptor);
|
|
156
|
+
if (capability.kind === "localized-string") {
|
|
157
|
+
const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
|
|
158
|
+
const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
|
|
159
|
+
return {
|
|
160
|
+
defaultLocale,
|
|
161
|
+
locales: Object.fromEntries(locales.map((locale) => [locale, ""])),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (capability.kind === "rich-text") {
|
|
165
|
+
return { value: {}, html: "" };
|
|
166
|
+
}
|
|
167
|
+
if (capability.kind === "localized-rich-text") {
|
|
168
|
+
const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
|
|
169
|
+
const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
|
|
170
|
+
return {
|
|
171
|
+
defaultLocale,
|
|
172
|
+
locales: Object.fromEntries(locales.map((locale) => [locale, { value: {}, html: "" }])),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
if (capability.kind === "model-ref" || capability.kind === "asset-ref") {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
if (capability.kind === "enum") {
|
|
179
|
+
return capability.options[0] ?? null;
|
|
180
|
+
}
|
|
181
|
+
if (capability.kind === "union") {
|
|
182
|
+
const firstBranch = capability.branches[0];
|
|
183
|
+
if (!firstBranch)
|
|
184
|
+
return null;
|
|
185
|
+
return (0, union_js_1.createTaggedUnionValue)(descriptor, 0, getDescriptorDefaultValue(firstBranch.descriptor, options));
|
|
186
|
+
}
|
|
187
|
+
if (capability.kind === "object-inline") {
|
|
188
|
+
return Object.fromEntries(capability.properties.map(([key, propertyDescriptor]) => [
|
|
189
|
+
key,
|
|
190
|
+
getDescriptorDefaultValue(propertyDescriptor, options),
|
|
191
|
+
]));
|
|
192
|
+
}
|
|
193
|
+
if (capability.kind === "collection-inline") {
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
if (capability.kind === "scalar") {
|
|
197
|
+
if (capability.primitiveType === "string")
|
|
198
|
+
return "";
|
|
199
|
+
if (capability.primitiveType === "number")
|
|
200
|
+
return 0;
|
|
201
|
+
if (capability.primitiveType === "boolean")
|
|
202
|
+
return false;
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
function normalizeEnumValue(descriptor, value) {
|
|
208
|
+
const valueType = (descriptor.valueType ?? "string");
|
|
209
|
+
const values = Array.isArray(descriptor.values) ? descriptor.values : [];
|
|
210
|
+
let normalized = value;
|
|
211
|
+
if (valueType === "string") {
|
|
212
|
+
normalized = value == null ? "" : String(value);
|
|
213
|
+
}
|
|
214
|
+
else if (valueType === "number") {
|
|
215
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
216
|
+
normalized = value;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
const parsed = Number(value);
|
|
220
|
+
normalized = Number.isFinite(parsed) ? parsed : null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
else if (valueType === "boolean") {
|
|
224
|
+
if (value === true || value === "true" || value === 1 || value === "1") {
|
|
225
|
+
normalized = true;
|
|
226
|
+
}
|
|
227
|
+
else if (value === false || value === "false" || value === 0 || value === "0") {
|
|
228
|
+
normalized = false;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
normalized = Boolean(value);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (!values.length)
|
|
235
|
+
return normalized;
|
|
236
|
+
if (values.some((entry) => Object.is(entry, normalized))) {
|
|
237
|
+
return normalized;
|
|
238
|
+
}
|
|
239
|
+
return values[0];
|
|
240
|
+
}
|
|
241
|
+
function formatEnumOptionValue(value) {
|
|
242
|
+
return JSON.stringify(value);
|
|
243
|
+
}
|
|
244
|
+
function parseEnumOptionValue(raw, valueType) {
|
|
245
|
+
try {
|
|
246
|
+
const parsed = JSON.parse(raw);
|
|
247
|
+
if (valueType === "string")
|
|
248
|
+
return String(parsed ?? "");
|
|
249
|
+
if (valueType === "number") {
|
|
250
|
+
const num = Number(parsed);
|
|
251
|
+
return Number.isFinite(num) ? num : null;
|
|
252
|
+
}
|
|
253
|
+
return Boolean(parsed);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
if (valueType === "number") {
|
|
257
|
+
const num = Number(raw);
|
|
258
|
+
return Number.isFinite(num) ? num : null;
|
|
259
|
+
}
|
|
260
|
+
if (valueType === "boolean") {
|
|
261
|
+
return raw === "true";
|
|
262
|
+
}
|
|
263
|
+
return raw;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function isUrlLikeString(input) {
|
|
267
|
+
return (typeof input === "string" &&
|
|
268
|
+
(input.includes("://") || input.startsWith("/") || input.startsWith("./")));
|
|
269
|
+
}
|
|
270
|
+
function isUuidLikeId(input) {
|
|
271
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input);
|
|
272
|
+
}
|
|
273
|
+
function normalizeModelRefValue(value) {
|
|
274
|
+
if (typeof value === "string" || typeof value === "number")
|
|
275
|
+
return value;
|
|
276
|
+
if (!isPlainRecord(value))
|
|
277
|
+
return null;
|
|
278
|
+
const id = value.id;
|
|
279
|
+
return typeof id === "string" || typeof id === "number" ? id : null;
|
|
280
|
+
}
|
|
281
|
+
function descriptorMatchScore(descriptor, value) {
|
|
282
|
+
if (isPrimitiveDescriptor(descriptor)) {
|
|
283
|
+
if (descriptor.type === "json")
|
|
284
|
+
return 1;
|
|
285
|
+
if (descriptor.type === "string")
|
|
286
|
+
return typeof value === "string" ? 4 : 0;
|
|
287
|
+
if (descriptor.type === "number")
|
|
288
|
+
return typeof value === "number" ? 4 : 0;
|
|
289
|
+
if (descriptor.type === "boolean")
|
|
290
|
+
return typeof value === "boolean" ? 4 : 0;
|
|
291
|
+
return 0;
|
|
292
|
+
}
|
|
293
|
+
if (isEnumDescriptor(descriptor)) {
|
|
294
|
+
const values = Array.isArray(descriptor.values) ? descriptor.values : [];
|
|
295
|
+
if (!values.length)
|
|
296
|
+
return 2;
|
|
297
|
+
return values.some((entry) => Object.is(entry, value)) ? 6 : 0;
|
|
298
|
+
}
|
|
299
|
+
if (isModelRefDescriptor(descriptor)) {
|
|
300
|
+
if (typeof value === "string") {
|
|
301
|
+
const normalized = value.trim();
|
|
302
|
+
if (!normalized)
|
|
303
|
+
return 0;
|
|
304
|
+
if (isUrlLikeString(normalized))
|
|
305
|
+
return 1;
|
|
306
|
+
return isUuidLikeId(normalized) ? 6 : 2;
|
|
307
|
+
}
|
|
308
|
+
if (typeof value === "number") {
|
|
309
|
+
return Number.isFinite(value) && value !== 0 ? 4 : 0;
|
|
310
|
+
}
|
|
311
|
+
const candidate = normalizeModelRefValue(value);
|
|
312
|
+
if (candidate == null)
|
|
313
|
+
return 0;
|
|
314
|
+
return typeof candidate === "string" && isUuidLikeId(candidate) ? 5 : 2;
|
|
315
|
+
}
|
|
316
|
+
if (isArrayDescriptor(descriptor)) {
|
|
317
|
+
return Array.isArray(value) ? 3 : 0;
|
|
318
|
+
}
|
|
319
|
+
if (isObjectDescriptor(descriptor)) {
|
|
320
|
+
if (!isPlainRecord(value))
|
|
321
|
+
return 0;
|
|
322
|
+
const keys = Object.keys(descriptor.properties ?? {});
|
|
323
|
+
const matched = keys.filter((key) => Object.prototype.hasOwnProperty.call(value, key)).length;
|
|
324
|
+
return 2 + matched;
|
|
325
|
+
}
|
|
326
|
+
if (isUnionDescriptor(descriptor)) {
|
|
327
|
+
const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
|
|
328
|
+
if (!branches.length)
|
|
329
|
+
return 0;
|
|
330
|
+
return Math.max(...branches.map((branch) => descriptorMatchScore(branch, value)));
|
|
331
|
+
}
|
|
332
|
+
return 0;
|
|
333
|
+
}
|
|
334
|
+
function resolveUnionBranchIndex(descriptor, value) {
|
|
335
|
+
const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
|
|
336
|
+
if (!branches.length)
|
|
337
|
+
return 0;
|
|
338
|
+
const discriminatorKey = descriptor.discriminator?.key;
|
|
339
|
+
if (discriminatorKey && isPlainRecord(value)) {
|
|
340
|
+
const discriminatorValue = value[discriminatorKey];
|
|
341
|
+
for (let index = 0; index < branches.length; index += 1) {
|
|
342
|
+
const branch = branches[index];
|
|
343
|
+
const expected = branch?.properties?.[discriminatorKey]?.values?.[0];
|
|
344
|
+
if (expected !== undefined && Object.is(expected, discriminatorValue)) {
|
|
345
|
+
return index;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
let bestIndex = 0;
|
|
350
|
+
let bestScore = -1;
|
|
351
|
+
for (let index = 0; index < branches.length; index += 1) {
|
|
352
|
+
const score = descriptorMatchScore(branches[index], value);
|
|
353
|
+
if (score > bestScore) {
|
|
354
|
+
bestScore = score;
|
|
355
|
+
bestIndex = index;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return bestIndex;
|
|
359
|
+
}
|
|
360
|
+
function normalizeUnionValue(descriptor, value, options = {}) {
|
|
361
|
+
if (value == null) {
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
if ((0, union_js_1.isTaggedUnionValue)(value)) {
|
|
365
|
+
return value;
|
|
366
|
+
}
|
|
367
|
+
const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
|
|
368
|
+
if (!branches.length)
|
|
369
|
+
return null;
|
|
370
|
+
const branchIndex = resolveUnionBranchIndex(descriptor, value);
|
|
371
|
+
const branchKey = (0, union_js_1.getUnionBranchKeyAt)(descriptor, branchIndex);
|
|
372
|
+
if (!branchKey)
|
|
373
|
+
return null;
|
|
374
|
+
const branchDescriptor = branches[branchIndex];
|
|
375
|
+
const branchValue = value == null
|
|
376
|
+
? getDescriptorDefaultValue(branchDescriptor, options)
|
|
377
|
+
: value;
|
|
378
|
+
return (0, union_js_1.createTaggedUnionValue)(descriptor, branchIndex, branchValue);
|
|
379
|
+
}
|
|
380
|
+
function getDescriptorDisplayName(descriptor, index = 0) {
|
|
381
|
+
const customType = descriptor?.customType;
|
|
382
|
+
if (typeof customType === "string" && customType.length > 0) {
|
|
383
|
+
return humanizeVisualText(customType);
|
|
384
|
+
}
|
|
385
|
+
if (isModelRefDescriptor(descriptor)) {
|
|
386
|
+
return humanizeVisualText(descriptor.model || `Branch ${index + 1}`);
|
|
387
|
+
}
|
|
388
|
+
if (isEnumDescriptor(descriptor))
|
|
389
|
+
return "Enum";
|
|
390
|
+
if (isUnionDescriptor(descriptor))
|
|
391
|
+
return "Union";
|
|
392
|
+
const descriptorType = descriptor?.type;
|
|
393
|
+
if (typeof descriptorType === "string") {
|
|
394
|
+
return humanizeVisualText(descriptorType);
|
|
395
|
+
}
|
|
396
|
+
return `Branch ${index + 1}`;
|
|
397
|
+
}
|
|
398
|
+
function humanizeVisualText(value) {
|
|
399
|
+
return value
|
|
400
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
401
|
+
.replace(/[-_]+/g, " ")
|
|
402
|
+
.trim()
|
|
403
|
+
.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
404
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,5 +16,7 @@ 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);
|
|
20
|
+
__exportStar(require("./descriptor-capabilities.js"), exports);
|
|
19
21
|
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
20
22
|
exports.delay = delay;
|
|
@@ -0,0 +1,170 @@
|
|
|
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.createTaggedUnionValue = createTaggedUnionValue;
|
|
11
|
+
exports.resolveTaggedUnionBranchIndex = resolveTaggedUnionBranchIndex;
|
|
12
|
+
exports.CMS0_UNION_META_KEY = "__cms0Union";
|
|
13
|
+
function isObjectRecord(value) {
|
|
14
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
function normalizeBranchShape(descriptor) {
|
|
17
|
+
if (descriptor?.kind === "modelRef") {
|
|
18
|
+
return {
|
|
19
|
+
kind: "modelRef",
|
|
20
|
+
model: descriptor.model,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (descriptor?.kind === "enum") {
|
|
24
|
+
return {
|
|
25
|
+
kind: "enum",
|
|
26
|
+
valueType: descriptor.valueType ?? "string",
|
|
27
|
+
values: Array.isArray(descriptor.values)
|
|
28
|
+
? [...descriptor.values]
|
|
29
|
+
: [],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (descriptor?.kind === "union") {
|
|
33
|
+
const branches = Array.isArray(descriptor?.anyOf)
|
|
34
|
+
? descriptor.anyOf
|
|
35
|
+
: [];
|
|
36
|
+
return {
|
|
37
|
+
kind: "union",
|
|
38
|
+
anyOf: branches.map((branch) => normalizeBranchShape(branch)),
|
|
39
|
+
...(typeof descriptor?.discriminator?.key === "string"
|
|
40
|
+
? { discriminator: { key: descriptor.discriminator.key } }
|
|
41
|
+
: {}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (descriptor?.type === "array") {
|
|
45
|
+
return {
|
|
46
|
+
kind: "array",
|
|
47
|
+
items: normalizeBranchShape(descriptor.items),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (descriptor?.type === "object") {
|
|
51
|
+
const properties = descriptor?.properties &&
|
|
52
|
+
typeof descriptor.properties === "object"
|
|
53
|
+
? descriptor.properties
|
|
54
|
+
: {};
|
|
55
|
+
const normalizedProperties = Object.fromEntries(Object.keys(properties)
|
|
56
|
+
.sort((a, b) => a.localeCompare(b))
|
|
57
|
+
.map((key) => [key, normalizeBranchShape(properties[key])]));
|
|
58
|
+
return {
|
|
59
|
+
kind: "object",
|
|
60
|
+
properties: normalizedProperties,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
kind: "primitive",
|
|
65
|
+
type: descriptor?.type ?? "json",
|
|
66
|
+
...(typeof descriptor?.customType === "string"
|
|
67
|
+
? { customType: descriptor.customType }
|
|
68
|
+
: {}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function stableStringify(value) {
|
|
72
|
+
if (value === null)
|
|
73
|
+
return "null";
|
|
74
|
+
if (typeof value === "string")
|
|
75
|
+
return JSON.stringify(value);
|
|
76
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
77
|
+
return String(value);
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
80
|
+
}
|
|
81
|
+
if (!isObjectRecord(value))
|
|
82
|
+
return JSON.stringify(value);
|
|
83
|
+
const entries = Object.keys(value)
|
|
84
|
+
.sort((a, b) => a.localeCompare(b))
|
|
85
|
+
.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`);
|
|
86
|
+
return `{${entries.join(",")}}`;
|
|
87
|
+
}
|
|
88
|
+
function fnv1a32(input) {
|
|
89
|
+
let hash = 0x811c9dc5;
|
|
90
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
91
|
+
hash ^= input.charCodeAt(i);
|
|
92
|
+
hash = Math.imul(hash, 0x01000193);
|
|
93
|
+
}
|
|
94
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
95
|
+
}
|
|
96
|
+
function computeUnionBranchKeys(branches) {
|
|
97
|
+
const used = new Set();
|
|
98
|
+
return branches.map((branch, index) => {
|
|
99
|
+
const signature = stableStringify(normalizeBranchShape(branch));
|
|
100
|
+
const base = `branch_${fnv1a32(signature)}`;
|
|
101
|
+
let key = base;
|
|
102
|
+
let suffix = 1;
|
|
103
|
+
while (used.has(key)) {
|
|
104
|
+
suffix += 1;
|
|
105
|
+
key = `${base}_${suffix}`;
|
|
106
|
+
}
|
|
107
|
+
used.add(key);
|
|
108
|
+
return key || `branch_${index + 1}`;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function getUnionBranchKeys(descriptor) {
|
|
112
|
+
const branches = Array.isArray(descriptor?.anyOf)
|
|
113
|
+
? descriptor.anyOf
|
|
114
|
+
: [];
|
|
115
|
+
if (!branches.length)
|
|
116
|
+
return [];
|
|
117
|
+
const explicit = Array.isArray(descriptor?.branchKeys)
|
|
118
|
+
? descriptor.branchKeys
|
|
119
|
+
: [];
|
|
120
|
+
const normalizedExplicit = explicit
|
|
121
|
+
.map((entry) => (typeof entry === "string" ? entry.trim() : ""))
|
|
122
|
+
.filter((entry) => entry.length > 0);
|
|
123
|
+
if (normalizedExplicit.length === branches.length &&
|
|
124
|
+
new Set(normalizedExplicit).size === normalizedExplicit.length) {
|
|
125
|
+
return normalizedExplicit;
|
|
126
|
+
}
|
|
127
|
+
return computeUnionBranchKeys(branches);
|
|
128
|
+
}
|
|
129
|
+
function getUnionBranchKeyAt(descriptor, index) {
|
|
130
|
+
const keys = getUnionBranchKeys(descriptor);
|
|
131
|
+
return keys[index];
|
|
132
|
+
}
|
|
133
|
+
function isTaggedUnionValue(value) {
|
|
134
|
+
if (!isObjectRecord(value))
|
|
135
|
+
return false;
|
|
136
|
+
const meta = value[exports.CMS0_UNION_META_KEY];
|
|
137
|
+
if (!isObjectRecord(meta))
|
|
138
|
+
return false;
|
|
139
|
+
if (typeof meta.branchKey !== "string" || !meta.branchKey.trim())
|
|
140
|
+
return false;
|
|
141
|
+
return Object.prototype.hasOwnProperty.call(value, "value");
|
|
142
|
+
}
|
|
143
|
+
function encodeTaggedUnionValue(branchKey, value) {
|
|
144
|
+
return {
|
|
145
|
+
[exports.CMS0_UNION_META_KEY]: { branchKey },
|
|
146
|
+
value,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function decodeTaggedUnionValue(value) {
|
|
150
|
+
if (!isTaggedUnionValue(value))
|
|
151
|
+
return null;
|
|
152
|
+
const meta = value[exports.CMS0_UNION_META_KEY];
|
|
153
|
+
return {
|
|
154
|
+
branchKey: meta.branchKey.trim(),
|
|
155
|
+
value: value.value,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function createTaggedUnionValue(descriptor, branchIndex, branchValue) {
|
|
159
|
+
const branchKey = getUnionBranchKeyAt(descriptor, branchIndex);
|
|
160
|
+
if (!branchKey)
|
|
161
|
+
return null;
|
|
162
|
+
return encodeTaggedUnionValue(branchKey, branchValue);
|
|
163
|
+
}
|
|
164
|
+
function resolveTaggedUnionBranchIndex(descriptor, value) {
|
|
165
|
+
const decoded = decodeTaggedUnionValue(value);
|
|
166
|
+
if (!decoded)
|
|
167
|
+
return -1;
|
|
168
|
+
const keys = getUnionBranchKeys(descriptor);
|
|
169
|
+
return keys.findIndex((key) => key === decoded.branchKey);
|
|
170
|
+
}
|
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();
|