@tinacms/schema-tools 0.0.6 → 0.0.9
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/LICENSE +176 -0
- package/dist/index.es.js +715 -0
- package/dist/index.js +690 -705
- package/dist/types/SchemaTypes.d.ts +10 -3
- package/dist/validate/schema.d.ts +34 -0
- package/dist/validate/tinaCloudSchemaConfig.d.ts +18 -0
- package/package.json +19 -12
- package/CHANGELOG.md +0 -59
package/dist/index.js
CHANGED
|
@@ -1,765 +1,750 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
25
|
-
var __objRest = (source, exclude) => {
|
|
26
|
-
var target = {};
|
|
27
|
-
for (var prop in source)
|
|
28
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
if (source != null && __getOwnPropSymbols)
|
|
31
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
-
target[prop] = source[prop];
|
|
34
|
-
}
|
|
35
|
-
return target;
|
|
36
|
-
};
|
|
37
|
-
var __export = (target, all) => {
|
|
38
|
-
__markAsModule(target);
|
|
39
|
-
for (var name2 in all)
|
|
40
|
-
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
41
|
-
};
|
|
42
|
-
var __reExport = (target, module2, desc) => {
|
|
43
|
-
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
44
|
-
for (let key of __getOwnPropNames(module2))
|
|
45
|
-
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
46
|
-
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
47
|
-
}
|
|
48
|
-
return target;
|
|
49
|
-
};
|
|
50
|
-
var __toModule = (module2) => {
|
|
51
|
-
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/index.ts
|
|
55
|
-
__export(exports, {
|
|
56
|
-
TinaSchema: () => TinaSchema,
|
|
57
|
-
TinaSchemaValidationError: () => TinaSchemaValidationError,
|
|
58
|
-
addNamespaceToSchema: () => addNamespaceToSchema,
|
|
59
|
-
resolveField: () => resolveField,
|
|
60
|
-
resolveForm: () => resolveForm,
|
|
61
|
-
validateSchema: () => validateSchema,
|
|
62
|
-
validateTinaCloudSchemaConfig: () => validateTinaCloudSchemaConfig
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/schema/addNamespaceToSchema.ts
|
|
66
|
-
function addNamespaceToSchema(maybeNode, namespace = []) {
|
|
67
|
-
if (typeof maybeNode === "string") {
|
|
68
|
-
return maybeNode;
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("yup"), require("zod")) : typeof define === "function" && define.amd ? define(["exports", "yup", "zod"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@tinacms/schema-tools"] = {}, global.NOOP, global.NOOP));
|
|
3
|
+
})(this, function(exports2, yup, z) {
|
|
4
|
+
"use strict";
|
|
5
|
+
function _interopDefaultLegacy(e) {
|
|
6
|
+
return e && typeof e === "object" && "default" in e ? e : { "default": e };
|
|
69
7
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
8
|
+
function _interopNamespace(e) {
|
|
9
|
+
if (e && e.__esModule)
|
|
10
|
+
return e;
|
|
11
|
+
var n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function(k) {
|
|
14
|
+
if (k !== "default") {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function() {
|
|
19
|
+
return e[k];
|
|
20
|
+
}
|
|
21
|
+
});
|
|
84
22
|
}
|
|
85
|
-
const value = element.name || element.value;
|
|
86
|
-
return addNamespaceToSchema(element, [...namespace, value]);
|
|
87
23
|
});
|
|
88
|
-
} else {
|
|
89
|
-
if (!m) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (!m.hasOwnProperty("name")) {
|
|
93
|
-
newNode[key] = m;
|
|
94
|
-
} else {
|
|
95
|
-
newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
|
|
96
|
-
}
|
|
97
24
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/validate.ts
|
|
103
|
-
var yup = __toModule(require("yup"));
|
|
104
|
-
function assertShape(value, yupSchema, errorMessage) {
|
|
105
|
-
const shape = yupSchema(yup);
|
|
106
|
-
try {
|
|
107
|
-
shape.validateSync(value);
|
|
108
|
-
} catch (e) {
|
|
109
|
-
const message = errorMessage || `Failed to assertShape - ${e.message}`;
|
|
110
|
-
throw new Error(message);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/lastItem.ts
|
|
115
|
-
var lastItem = (arr) => {
|
|
116
|
-
if (typeof arr === "undefined") {
|
|
117
|
-
throw new Error("Can not call lastItem when arr is undefined");
|
|
118
|
-
}
|
|
119
|
-
return arr[arr.length - 1];
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/namer.ts
|
|
123
|
-
var capitalize = (s) => {
|
|
124
|
-
if (typeof s !== "string")
|
|
125
|
-
return "";
|
|
126
|
-
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
127
|
-
};
|
|
128
|
-
var generateNamespacedFieldName = (names, suffix = "") => {
|
|
129
|
-
return (suffix ? [...names, suffix] : names).map(capitalize).join("");
|
|
130
|
-
};
|
|
131
|
-
var NAMER = {
|
|
132
|
-
dataFilterTypeNameOn: (namespace) => {
|
|
133
|
-
return generateNamespacedFieldName(namespace, "_FilterOn");
|
|
134
|
-
},
|
|
135
|
-
dataFilterTypeName: (namespace) => {
|
|
136
|
-
return generateNamespacedFieldName(namespace, "Filter");
|
|
137
|
-
},
|
|
138
|
-
dataMutationTypeNameOn: (namespace) => {
|
|
139
|
-
return generateNamespacedFieldName(namespace, "_MutationOn");
|
|
140
|
-
},
|
|
141
|
-
dataMutationTypeName: (namespace) => {
|
|
142
|
-
return generateNamespacedFieldName(namespace, "Mutation");
|
|
143
|
-
},
|
|
144
|
-
updateName: (namespace) => {
|
|
145
|
-
return "update" + generateNamespacedFieldName(namespace, "Document");
|
|
146
|
-
},
|
|
147
|
-
createName: (namespace) => {
|
|
148
|
-
return "create" + generateNamespacedFieldName(namespace, "Document");
|
|
149
|
-
},
|
|
150
|
-
queryName: (namespace) => {
|
|
151
|
-
return "get" + generateNamespacedFieldName(namespace, "Document");
|
|
152
|
-
},
|
|
153
|
-
generateQueryListName: (namespace) => {
|
|
154
|
-
return "get" + generateNamespacedFieldName(namespace, "List");
|
|
155
|
-
},
|
|
156
|
-
fragmentName: (namespace) => {
|
|
157
|
-
return generateNamespacedFieldName(namespace, "") + "Parts";
|
|
158
|
-
},
|
|
159
|
-
collectionTypeName: (namespace) => {
|
|
160
|
-
return generateNamespacedFieldName(namespace, "Collection");
|
|
161
|
-
},
|
|
162
|
-
documentTypeName: (namespace) => {
|
|
163
|
-
return generateNamespacedFieldName(namespace, "Document");
|
|
164
|
-
},
|
|
165
|
-
dataTypeName: (namespace) => {
|
|
166
|
-
return generateNamespacedFieldName(namespace, "");
|
|
167
|
-
},
|
|
168
|
-
referenceConnectionType: (namespace) => {
|
|
169
|
-
return generateNamespacedFieldName(namespace, "Connection");
|
|
170
|
-
},
|
|
171
|
-
referenceConnectionEdgesTypeName: (namespace) => {
|
|
172
|
-
return generateNamespacedFieldName(namespace, "ConnectionEdges");
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/hasDuplicates.ts
|
|
177
|
-
function hasDuplicates(array) {
|
|
178
|
-
if (!array) {
|
|
179
|
-
return false;
|
|
180
|
-
} else {
|
|
181
|
-
return new Set(array).size !== array.length;
|
|
25
|
+
n["default"] = e;
|
|
26
|
+
return Object.freeze(n);
|
|
182
27
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
if (!collection) {
|
|
204
|
-
throw new Error(`Expected to find collection named ${collectionName}`);
|
|
205
|
-
}
|
|
206
|
-
const extraFields = {};
|
|
207
|
-
const templateInfo = this.getTemplatesForCollectable(collection);
|
|
208
|
-
switch (templateInfo.type) {
|
|
209
|
-
case "object":
|
|
210
|
-
extraFields["fields"] = templateInfo.template.fields;
|
|
211
|
-
break;
|
|
212
|
-
case "union":
|
|
213
|
-
extraFields["templates"] = templateInfo.templates;
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
return __spreadProps(__spreadValues(__spreadValues({
|
|
217
|
-
slug: collection.name
|
|
218
|
-
}, extraFields), collection), {
|
|
219
|
-
format: collection.format || "md"
|
|
220
|
-
});
|
|
221
|
-
};
|
|
222
|
-
this.getCollections = () => {
|
|
223
|
-
return this.schema.collections.map((collection) => this.getCollection(collection.name)) || [];
|
|
224
|
-
};
|
|
225
|
-
this.getGlobalTemplate = (templateName) => {
|
|
226
|
-
var _a;
|
|
227
|
-
const globalTemplate = (_a = this.schema.templates) == null ? void 0 : _a.find((template) => template.name === templateName);
|
|
228
|
-
if (!globalTemplate) {
|
|
229
|
-
throw new Error(`Expected to find global template of name ${templateName}`);
|
|
230
|
-
}
|
|
231
|
-
return globalTemplate;
|
|
232
|
-
};
|
|
233
|
-
this.getCollectionByFullPath = (filepath) => {
|
|
234
|
-
const collection = this.getCollections().find((collection2) => {
|
|
235
|
-
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
236
|
-
});
|
|
237
|
-
if (!collection) {
|
|
238
|
-
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
239
|
-
}
|
|
240
|
-
return collection;
|
|
241
|
-
};
|
|
242
|
-
this.getCollectionAndTemplateByFullPath = (filepath, templateName) => {
|
|
243
|
-
let template;
|
|
244
|
-
const collection = this.getCollections().find((collection2) => {
|
|
245
|
-
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
246
|
-
});
|
|
247
|
-
if (!collection) {
|
|
248
|
-
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
249
|
-
}
|
|
250
|
-
const templates = this.getTemplatesForCollectable(collection);
|
|
251
|
-
if (templates.type === "union") {
|
|
252
|
-
if (templateName) {
|
|
253
|
-
template = templates.templates.find((template2) => lastItem(template2.namespace) === templateName);
|
|
254
|
-
if (!template) {
|
|
255
|
-
throw new Error(`Unable to determine template for item at ${filepath}`);
|
|
28
|
+
var yup__namespace = /* @__PURE__ */ _interopNamespace(yup);
|
|
29
|
+
var z__default = /* @__PURE__ */ _interopDefaultLegacy(z);
|
|
30
|
+
function addNamespaceToSchema(maybeNode, namespace = []) {
|
|
31
|
+
if (typeof maybeNode === "string") {
|
|
32
|
+
return maybeNode;
|
|
33
|
+
}
|
|
34
|
+
if (typeof maybeNode === "boolean") {
|
|
35
|
+
return maybeNode;
|
|
36
|
+
}
|
|
37
|
+
const newNode = { ...maybeNode };
|
|
38
|
+
const keys = Object.keys(maybeNode);
|
|
39
|
+
Object.values(maybeNode).map((m, index) => {
|
|
40
|
+
const key = keys[index];
|
|
41
|
+
if (Array.isArray(m)) {
|
|
42
|
+
newNode[key] = m.map((element) => {
|
|
43
|
+
if (!element) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!element.hasOwnProperty("name")) {
|
|
47
|
+
return element;
|
|
256
48
|
}
|
|
49
|
+
const value = element.name || element.value;
|
|
50
|
+
return addNamespaceToSchema(element, [...namespace, value]);
|
|
51
|
+
});
|
|
52
|
+
} else {
|
|
53
|
+
if (!m) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!m.hasOwnProperty("name")) {
|
|
57
|
+
newNode[key] = m;
|
|
257
58
|
} else {
|
|
258
|
-
|
|
59
|
+
newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
|
|
259
60
|
}
|
|
260
61
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
62
|
+
});
|
|
63
|
+
return { ...newNode, namespace };
|
|
64
|
+
}
|
|
65
|
+
function assertShape(value, yupSchema, errorMessage) {
|
|
66
|
+
const shape = yupSchema(yup__namespace);
|
|
67
|
+
try {
|
|
68
|
+
shape.validateSync(value);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
const message = errorMessage || `Failed to assertShape - ${e.message}`;
|
|
71
|
+
throw new Error(message);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const lastItem = (arr) => {
|
|
75
|
+
if (typeof arr === "undefined") {
|
|
76
|
+
throw new Error("Can not call lastItem when arr is undefined");
|
|
77
|
+
}
|
|
78
|
+
return arr[arr.length - 1];
|
|
79
|
+
};
|
|
80
|
+
const capitalize = (s) => {
|
|
81
|
+
if (typeof s !== "string")
|
|
82
|
+
return "";
|
|
83
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
84
|
+
};
|
|
85
|
+
const generateNamespacedFieldName = (names, suffix = "") => {
|
|
86
|
+
return (suffix ? [...names, suffix] : names).map(capitalize).join("");
|
|
87
|
+
};
|
|
88
|
+
const NAMER = {
|
|
89
|
+
dataFilterTypeNameOn: (namespace) => {
|
|
90
|
+
return generateNamespacedFieldName(namespace, "_FilterOn");
|
|
91
|
+
},
|
|
92
|
+
dataFilterTypeName: (namespace) => {
|
|
93
|
+
return generateNamespacedFieldName(namespace, "Filter");
|
|
94
|
+
},
|
|
95
|
+
dataMutationTypeNameOn: (namespace) => {
|
|
96
|
+
return generateNamespacedFieldName(namespace, "_MutationOn");
|
|
97
|
+
},
|
|
98
|
+
dataMutationTypeName: (namespace) => {
|
|
99
|
+
return generateNamespacedFieldName(namespace, "Mutation");
|
|
100
|
+
},
|
|
101
|
+
updateName: (namespace) => {
|
|
102
|
+
return "update" + generateNamespacedFieldName(namespace, "Document");
|
|
103
|
+
},
|
|
104
|
+
createName: (namespace) => {
|
|
105
|
+
return "create" + generateNamespacedFieldName(namespace, "Document");
|
|
106
|
+
},
|
|
107
|
+
queryName: (namespace) => {
|
|
108
|
+
return "get" + generateNamespacedFieldName(namespace, "Document");
|
|
109
|
+
},
|
|
110
|
+
generateQueryListName: (namespace) => {
|
|
111
|
+
return "get" + generateNamespacedFieldName(namespace, "List");
|
|
112
|
+
},
|
|
113
|
+
fragmentName: (namespace) => {
|
|
114
|
+
return generateNamespacedFieldName(namespace, "") + "Parts";
|
|
115
|
+
},
|
|
116
|
+
collectionTypeName: (namespace) => {
|
|
117
|
+
return generateNamespacedFieldName(namespace, "Collection");
|
|
118
|
+
},
|
|
119
|
+
documentTypeName: (namespace) => {
|
|
120
|
+
return generateNamespacedFieldName(namespace, "Document");
|
|
121
|
+
},
|
|
122
|
+
dataTypeName: (namespace) => {
|
|
123
|
+
return generateNamespacedFieldName(namespace, "");
|
|
124
|
+
},
|
|
125
|
+
referenceConnectionType: (namespace) => {
|
|
126
|
+
return generateNamespacedFieldName(namespace, "Connection");
|
|
127
|
+
},
|
|
128
|
+
referenceConnectionEdgesTypeName: (namespace) => {
|
|
129
|
+
return generateNamespacedFieldName(namespace, "ConnectionEdges");
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
function hasDuplicates(array) {
|
|
133
|
+
if (!array) {
|
|
295
134
|
return false;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
135
|
+
} else {
|
|
136
|
+
return new Set(array).size !== array.length;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
class TinaSchema {
|
|
140
|
+
constructor(config) {
|
|
141
|
+
this.config = config;
|
|
142
|
+
this.getIsTitleFieldName = (collection) => {
|
|
143
|
+
var _a;
|
|
144
|
+
const col = this.getCollection(collection);
|
|
145
|
+
const field = (_a = col == null ? void 0 : col.fields) == null ? void 0 : _a.find((x) => x.type === "string" && x.isTitle);
|
|
146
|
+
return field == null ? void 0 : field.name;
|
|
147
|
+
};
|
|
148
|
+
this.getCollectionsByName = (collectionNames) => {
|
|
149
|
+
return this.schema.collections.filter((collection) => collectionNames.includes(collection.name));
|
|
150
|
+
};
|
|
151
|
+
this.getAllCollectionPaths = () => {
|
|
152
|
+
const paths = this.getCollections().map((collection) => `${collection.path}${collection.match || ""}`);
|
|
153
|
+
return paths;
|
|
154
|
+
};
|
|
155
|
+
this.getCollection = (collectionName) => {
|
|
156
|
+
const collection = this.schema.collections.find((collection2) => collection2.name === collectionName);
|
|
157
|
+
if (!collection) {
|
|
158
|
+
throw new Error(`Expected to find collection named ${collectionName}`);
|
|
159
|
+
}
|
|
160
|
+
const extraFields = {};
|
|
161
|
+
const templateInfo = this.getTemplatesForCollectable(collection);
|
|
162
|
+
switch (templateInfo.type) {
|
|
163
|
+
case "object":
|
|
164
|
+
extraFields["fields"] = templateInfo.template.fields;
|
|
165
|
+
break;
|
|
166
|
+
case "union":
|
|
167
|
+
extraFields["templates"] = templateInfo.templates;
|
|
168
|
+
break;
|
|
306
169
|
}
|
|
307
170
|
return {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
})
|
|
171
|
+
slug: collection.name,
|
|
172
|
+
...extraFields,
|
|
173
|
+
...collection,
|
|
174
|
+
format: collection.format || "md"
|
|
313
175
|
};
|
|
314
|
-
}
|
|
315
|
-
|
|
176
|
+
};
|
|
177
|
+
this.getCollections = () => {
|
|
178
|
+
return this.schema.collections.map((collection) => this.getCollection(collection.name)) || [];
|
|
179
|
+
};
|
|
180
|
+
this.getGlobalTemplate = (templateName) => {
|
|
181
|
+
var _a;
|
|
182
|
+
const globalTemplate = (_a = this.schema.templates) == null ? void 0 : _a.find((template) => template.name === templateName);
|
|
183
|
+
if (!globalTemplate) {
|
|
184
|
+
throw new Error(`Expected to find global template of name ${templateName}`);
|
|
185
|
+
}
|
|
186
|
+
return globalTemplate;
|
|
187
|
+
};
|
|
188
|
+
this.getCollectionByFullPath = (filepath) => {
|
|
189
|
+
const collection = this.getCollections().find((collection2) => {
|
|
190
|
+
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
191
|
+
});
|
|
192
|
+
if (!collection) {
|
|
193
|
+
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
194
|
+
}
|
|
195
|
+
return collection;
|
|
196
|
+
};
|
|
197
|
+
this.getCollectionAndTemplateByFullPath = (filepath, templateName) => {
|
|
198
|
+
let template;
|
|
199
|
+
const collection = this.getCollections().find((collection2) => {
|
|
200
|
+
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
201
|
+
});
|
|
202
|
+
if (!collection) {
|
|
203
|
+
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
204
|
+
}
|
|
205
|
+
const templates = this.getTemplatesForCollectable(collection);
|
|
206
|
+
if (templates.type === "union") {
|
|
207
|
+
if (templateName) {
|
|
208
|
+
template = templates.templates.find((template2) => lastItem(template2.namespace) === templateName);
|
|
209
|
+
if (!template) {
|
|
210
|
+
throw new Error(`Unable to determine template for item at ${filepath}`);
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
throw new Error(`Unable to determine template for item at ${filepath}, no template name provided for collection with multiple templates`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (templates.type === "object") {
|
|
217
|
+
template = templates.template;
|
|
218
|
+
}
|
|
219
|
+
if (!template) {
|
|
220
|
+
throw new Error(`Something went wrong while trying to determine template for ${filepath}`);
|
|
221
|
+
}
|
|
222
|
+
return { collection, template };
|
|
223
|
+
};
|
|
224
|
+
this.getTemplateForData = ({
|
|
225
|
+
data,
|
|
226
|
+
collection
|
|
227
|
+
}) => {
|
|
228
|
+
const templateInfo = this.getTemplatesForCollectable(collection);
|
|
229
|
+
switch (templateInfo.type) {
|
|
230
|
+
case "object":
|
|
231
|
+
return templateInfo.template;
|
|
232
|
+
case "union":
|
|
233
|
+
assertShape(data, (yup2) => yup2.object({ _template: yup2.string().required() }));
|
|
234
|
+
const template = templateInfo.templates.find((template2) => template2.namespace[template2.namespace.length - 1] === data._template);
|
|
235
|
+
if (!template) {
|
|
236
|
+
throw new Error(`Expected to find template named '${data._template}' for collection '${lastItem(collection.namespace)}'`);
|
|
237
|
+
}
|
|
238
|
+
return template;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
this.isMarkdownCollection = (collectionName) => {
|
|
242
|
+
const collection = this.getCollection(collectionName);
|
|
243
|
+
const format = collection.format;
|
|
244
|
+
if (!format) {
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
if (["markdown", "md"].includes(format)) {
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
};
|
|
252
|
+
this.getTemplatesForCollectable = (collection) => {
|
|
253
|
+
let extraFields = [];
|
|
254
|
+
if (collection.references) {
|
|
255
|
+
extraFields = collection.references;
|
|
256
|
+
}
|
|
257
|
+
if (collection.fields) {
|
|
258
|
+
const template = typeof collection.fields === "string" ? this.getGlobalTemplate(collection.fields) : collection;
|
|
259
|
+
if (typeof template.fields === "string" || typeof template.fields === "undefined") {
|
|
260
|
+
throw new Error("Exptected template to have fields but none were found");
|
|
261
|
+
}
|
|
316
262
|
return {
|
|
317
263
|
namespace: collection.namespace,
|
|
318
|
-
type: "
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
});
|
|
324
|
-
})
|
|
264
|
+
type: "object",
|
|
265
|
+
template: {
|
|
266
|
+
...template,
|
|
267
|
+
fields: [...template.fields, ...extraFields]
|
|
268
|
+
}
|
|
325
269
|
};
|
|
326
270
|
} else {
|
|
327
|
-
|
|
271
|
+
if (collection.templates) {
|
|
272
|
+
return {
|
|
273
|
+
namespace: collection.namespace,
|
|
274
|
+
type: "union",
|
|
275
|
+
templates: collection.templates.map((templateOrTemplateString) => {
|
|
276
|
+
const template = typeof templateOrTemplateString === "string" ? this.getGlobalTemplate(templateOrTemplateString) : templateOrTemplateString;
|
|
277
|
+
return {
|
|
278
|
+
...template,
|
|
279
|
+
fields: [...template.fields, ...extraFields]
|
|
280
|
+
};
|
|
281
|
+
})
|
|
282
|
+
};
|
|
283
|
+
} else {
|
|
284
|
+
throw new Error(`Expected either fields or templates array to be defined on collection ${collection.namespace.join("_")}`);
|
|
285
|
+
}
|
|
328
286
|
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
|
|
287
|
+
};
|
|
288
|
+
this.schema = config;
|
|
289
|
+
}
|
|
332
290
|
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
291
|
+
const resolveField = ({ namespace, ...field }, schema) => {
|
|
292
|
+
var _a;
|
|
293
|
+
field.parentTypename = NAMER.dataTypeName(namespace.filter((_, i) => i < namespace.length - 1));
|
|
294
|
+
const extraFields = field.ui || {};
|
|
295
|
+
switch (field.type) {
|
|
296
|
+
case "number":
|
|
297
|
+
return {
|
|
298
|
+
component: "number",
|
|
299
|
+
...field,
|
|
300
|
+
...extraFields
|
|
301
|
+
};
|
|
302
|
+
case "datetime":
|
|
303
|
+
return {
|
|
304
|
+
component: "date",
|
|
305
|
+
...field,
|
|
306
|
+
...extraFields
|
|
307
|
+
};
|
|
308
|
+
case "boolean":
|
|
309
|
+
return {
|
|
310
|
+
component: "toggle",
|
|
311
|
+
...field,
|
|
312
|
+
...extraFields
|
|
313
|
+
};
|
|
314
|
+
case "image":
|
|
315
|
+
return {
|
|
316
|
+
component: "image",
|
|
317
|
+
clearable: true,
|
|
318
|
+
...field,
|
|
319
|
+
...extraFields
|
|
320
|
+
};
|
|
321
|
+
case "string":
|
|
322
|
+
if (field.options) {
|
|
323
|
+
if (field.list) {
|
|
324
|
+
return {
|
|
325
|
+
component: "checkbox-group",
|
|
326
|
+
...field,
|
|
327
|
+
...extraFields,
|
|
328
|
+
options: field.options
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
component: "select",
|
|
333
|
+
...field,
|
|
334
|
+
...extraFields,
|
|
335
|
+
options: [{ label: `Choose an option`, value: "" }, ...field.options]
|
|
336
|
+
};
|
|
337
|
+
}
|
|
361
338
|
if (field.list) {
|
|
362
|
-
return
|
|
363
|
-
component: "
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
339
|
+
return {
|
|
340
|
+
component: "list",
|
|
341
|
+
field: {
|
|
342
|
+
component: "text"
|
|
343
|
+
},
|
|
344
|
+
...field,
|
|
345
|
+
...extraFields
|
|
346
|
+
};
|
|
367
347
|
}
|
|
368
|
-
return
|
|
369
|
-
component: "
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
field: {
|
|
378
|
-
component: "text"
|
|
379
|
-
}
|
|
380
|
-
}, field), extraFields);
|
|
381
|
-
}
|
|
382
|
-
return __spreadValues(__spreadValues({
|
|
383
|
-
component: "text"
|
|
384
|
-
}, field), extraFields);
|
|
385
|
-
case "object":
|
|
386
|
-
const templateInfo = schema.getTemplatesForCollectable(__spreadProps(__spreadValues({}, field), {
|
|
387
|
-
namespace
|
|
388
|
-
}));
|
|
389
|
-
if (templateInfo.type === "object") {
|
|
390
|
-
return __spreadValues(__spreadProps(__spreadValues({}, field), {
|
|
391
|
-
component: field.list ? "group-list" : "group",
|
|
392
|
-
fields: templateInfo.template.fields.map((field2) => resolveField(field2, schema))
|
|
393
|
-
}), extraFields);
|
|
394
|
-
} else if (templateInfo.type === "union") {
|
|
395
|
-
const templates2 = {};
|
|
396
|
-
const typeMap2 = {};
|
|
397
|
-
templateInfo.templates.forEach((template) => {
|
|
398
|
-
const extraFields2 = template.ui || {};
|
|
399
|
-
const templateName = lastItem(template.namespace);
|
|
400
|
-
typeMap2[templateName] = NAMER.dataTypeName(template.namespace);
|
|
401
|
-
templates2[lastItem(template.namespace)] = __spreadValues({
|
|
402
|
-
label: template.label || templateName,
|
|
403
|
-
key: templateName,
|
|
404
|
-
fields: template.fields.map((field2) => resolveField(field2, schema))
|
|
405
|
-
}, extraFields2);
|
|
406
|
-
return true;
|
|
348
|
+
return {
|
|
349
|
+
component: "text",
|
|
350
|
+
...field,
|
|
351
|
+
...extraFields
|
|
352
|
+
};
|
|
353
|
+
case "object":
|
|
354
|
+
const templateInfo = schema.getTemplatesForCollectable({
|
|
355
|
+
...field,
|
|
356
|
+
namespace
|
|
407
357
|
});
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
358
|
+
if (templateInfo.type === "object") {
|
|
359
|
+
return {
|
|
360
|
+
...field,
|
|
361
|
+
component: field.list ? "group-list" : "group",
|
|
362
|
+
fields: templateInfo.template.fields.map((field2) => resolveField(field2, schema)),
|
|
363
|
+
...extraFields
|
|
364
|
+
};
|
|
365
|
+
} else if (templateInfo.type === "union") {
|
|
366
|
+
const templates2 = {};
|
|
367
|
+
const typeMap2 = {};
|
|
368
|
+
templateInfo.templates.forEach((template) => {
|
|
369
|
+
const extraFields2 = template.ui || {};
|
|
370
|
+
const templateName = lastItem(template.namespace);
|
|
371
|
+
typeMap2[templateName] = NAMER.dataTypeName(template.namespace);
|
|
372
|
+
templates2[lastItem(template.namespace)] = {
|
|
373
|
+
label: template.label || templateName,
|
|
374
|
+
key: templateName,
|
|
375
|
+
fields: template.fields.map((field2) => resolveField(field2, schema)),
|
|
376
|
+
...extraFields2
|
|
377
|
+
};
|
|
378
|
+
return true;
|
|
379
|
+
});
|
|
380
|
+
return {
|
|
381
|
+
...field,
|
|
382
|
+
typeMap: typeMap2,
|
|
383
|
+
component: field.list ? "blocks" : "not-implemented",
|
|
384
|
+
templates: templates2,
|
|
385
|
+
...extraFields
|
|
386
|
+
};
|
|
422
387
|
} else {
|
|
423
|
-
|
|
424
|
-
const templateName = lastItem(template.namespace);
|
|
425
|
-
typeMap[templateName] = NAMER.dataTypeName(template.namespace);
|
|
426
|
-
templates[lastItem(template.namespace)] = __spreadValues({
|
|
427
|
-
label: template.label || templateName,
|
|
428
|
-
key: templateName,
|
|
429
|
-
inline: template.inline,
|
|
430
|
-
name: templateName,
|
|
431
|
-
fields: template.fields.map((field2) => resolveField(field2, schema))
|
|
432
|
-
}, extraFields2);
|
|
433
|
-
return true;
|
|
388
|
+
throw new Error(`Unknown object for resolveField function`);
|
|
434
389
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
templates:
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/parseZodErrors.ts
|
|
470
|
-
var parseZodError = ({ zodError }) => {
|
|
471
|
-
var _a, _b;
|
|
472
|
-
const errors = zodError.flatten((issue) => {
|
|
473
|
-
const moreInfo = [];
|
|
474
|
-
if (issue.code === "invalid_union") {
|
|
475
|
-
issue.unionErrors.map((unionError) => {
|
|
476
|
-
moreInfo.push(parseZodError({ zodError: unionError }));
|
|
477
|
-
});
|
|
390
|
+
case "rich-text":
|
|
391
|
+
const templates = {};
|
|
392
|
+
(_a = field.templates) == null ? void 0 : _a.forEach((template) => {
|
|
393
|
+
if (typeof template === "string") {
|
|
394
|
+
throw new Error(`Global templates not yet supported for rich-text`);
|
|
395
|
+
} else {
|
|
396
|
+
const extraFields2 = template.ui || {};
|
|
397
|
+
const templateName = lastItem(template.namespace);
|
|
398
|
+
NAMER.dataTypeName(template.namespace);
|
|
399
|
+
templates[lastItem(template.namespace)] = {
|
|
400
|
+
label: template.label || templateName,
|
|
401
|
+
key: templateName,
|
|
402
|
+
inline: template.inline,
|
|
403
|
+
name: templateName,
|
|
404
|
+
fields: template.fields.map((field2) => resolveField(field2, schema)),
|
|
405
|
+
...extraFields2
|
|
406
|
+
};
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
return {
|
|
411
|
+
...field,
|
|
412
|
+
templates: Object.values(templates),
|
|
413
|
+
component: "rich-text",
|
|
414
|
+
...extraFields
|
|
415
|
+
};
|
|
416
|
+
case "reference":
|
|
417
|
+
return {
|
|
418
|
+
...field,
|
|
419
|
+
component: "reference",
|
|
420
|
+
...extraFields
|
|
421
|
+
};
|
|
422
|
+
default:
|
|
423
|
+
throw new Error(`Unknown field type ${field.type}`);
|
|
478
424
|
}
|
|
479
|
-
|
|
480
|
-
|
|
425
|
+
};
|
|
426
|
+
const resolveForm = ({
|
|
427
|
+
collection,
|
|
428
|
+
basename,
|
|
429
|
+
template,
|
|
430
|
+
schema
|
|
431
|
+
}) => {
|
|
481
432
|
return {
|
|
482
|
-
|
|
433
|
+
id: basename,
|
|
434
|
+
label: collection.label,
|
|
435
|
+
name: basename,
|
|
436
|
+
fields: template.fields.map((field) => {
|
|
437
|
+
return resolveField(field, schema);
|
|
438
|
+
})
|
|
483
439
|
};
|
|
440
|
+
};
|
|
441
|
+
const parseZodError = ({ zodError }) => {
|
|
442
|
+
var _a, _b, _c, _d;
|
|
443
|
+
const errors = zodError.flatten((issue) => {
|
|
444
|
+
const moreInfo = [];
|
|
445
|
+
if (issue.code === "invalid_union") {
|
|
446
|
+
issue.unionErrors.map((unionError) => {
|
|
447
|
+
moreInfo.push(parseZodError({ zodError: unionError }));
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
const errorMessage = `Error ${issue == null ? void 0 : issue.message} at path ${issue.path.join(".")}`;
|
|
451
|
+
const errorMessages = [errorMessage, ...moreInfo];
|
|
452
|
+
return {
|
|
453
|
+
errors: errorMessages
|
|
454
|
+
};
|
|
455
|
+
});
|
|
456
|
+
const formErrors = errors.formErrors.flatMap((x) => x.errors);
|
|
457
|
+
const parsedErrors = [
|
|
458
|
+
...((_b = (_a = errors.fieldErrors) == null ? void 0 : _a.collections) == null ? void 0 : _b.flatMap((x) => x.errors)) || [],
|
|
459
|
+
...((_d = (_c = errors.fieldErrors) == null ? void 0 : _c.config) == null ? void 0 : _d.flatMap((x) => x.errors)) || [],
|
|
460
|
+
...formErrors
|
|
461
|
+
];
|
|
462
|
+
return parsedErrors;
|
|
463
|
+
};
|
|
464
|
+
const name = z.z.string({
|
|
465
|
+
required_error: "Name is required but not provided",
|
|
466
|
+
invalid_type_error: "Name must be a string"
|
|
484
467
|
});
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
468
|
+
const TypeName = [
|
|
469
|
+
"string",
|
|
470
|
+
"boolean",
|
|
471
|
+
"number",
|
|
472
|
+
"datetime",
|
|
473
|
+
"image",
|
|
474
|
+
"object",
|
|
475
|
+
"reference",
|
|
476
|
+
"rich-text"
|
|
489
477
|
];
|
|
490
|
-
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
"object",
|
|
512
|
-
"reference",
|
|
513
|
-
"rich-text"
|
|
514
|
-
];
|
|
515
|
-
var typeTypeError = `type must be one of ${TypeName.join(", ")}`;
|
|
516
|
-
var typeRequiredError = `type is required and must be one of ${TypeName.join(", ")}`;
|
|
517
|
-
var nameProp = import_zod2.z.string({
|
|
518
|
-
required_error: "name must be provided",
|
|
519
|
-
invalid_type_error: "name must be a sting"
|
|
520
|
-
});
|
|
521
|
-
var Option = import_zod2.z.union([import_zod2.z.string(), import_zod2.z.object({ label: import_zod2.z.string(), value: import_zod2.z.string() })], {
|
|
522
|
-
errorMap: () => {
|
|
523
|
-
return {
|
|
524
|
-
message: "Invalid option array. Must be a string[] or {label: string, value: string}[]"
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
var TinaField = import_zod2.z.object({
|
|
529
|
-
name: nameProp,
|
|
530
|
-
label: import_zod2.z.string().optional(),
|
|
531
|
-
description: import_zod2.z.string().optional(),
|
|
532
|
-
required: import_zod2.z.boolean().optional()
|
|
533
|
-
});
|
|
534
|
-
var FieldWithList = TinaField.extend({ list: import_zod2.z.boolean().optional() });
|
|
535
|
-
var TinaScalerBase = FieldWithList.extend({
|
|
536
|
-
options: import_zod2.z.array(Option).optional()
|
|
537
|
-
});
|
|
538
|
-
var StringField = TinaScalerBase.extend({
|
|
539
|
-
type: import_zod2.z.literal("string", {
|
|
540
|
-
invalid_type_error: typeTypeError,
|
|
541
|
-
required_error: typeRequiredError
|
|
542
|
-
}),
|
|
543
|
-
isTitle: import_zod2.z.boolean().optional()
|
|
544
|
-
});
|
|
545
|
-
var BooleanField = TinaScalerBase.extend({
|
|
546
|
-
type: import_zod2.z.literal("boolean", {
|
|
547
|
-
invalid_type_error: typeTypeError,
|
|
548
|
-
required_error: typeRequiredError
|
|
549
|
-
})
|
|
550
|
-
});
|
|
551
|
-
var NumberField = TinaScalerBase.extend({
|
|
552
|
-
type: import_zod2.z.literal("number", {
|
|
553
|
-
invalid_type_error: typeTypeError,
|
|
554
|
-
required_error: typeRequiredError
|
|
555
|
-
})
|
|
556
|
-
});
|
|
557
|
-
var ImageField = TinaScalerBase.extend({
|
|
558
|
-
type: import_zod2.z.literal("image", {
|
|
559
|
-
invalid_type_error: typeTypeError,
|
|
560
|
-
required_error: typeRequiredError
|
|
561
|
-
})
|
|
562
|
-
});
|
|
563
|
-
var DateTimeField = TinaScalerBase.extend({
|
|
564
|
-
type: import_zod2.z.literal("datetime", {
|
|
565
|
-
invalid_type_error: typeTypeError,
|
|
566
|
-
required_error: typeRequiredError
|
|
567
|
-
}),
|
|
568
|
-
dateFormat: import_zod2.z.string().optional(),
|
|
569
|
-
timeFormat: import_zod2.z.string().optional()
|
|
570
|
-
});
|
|
571
|
-
var ReferenceField = FieldWithList.extend({
|
|
572
|
-
type: import_zod2.z.literal("reference", {
|
|
573
|
-
invalid_type_error: typeTypeError,
|
|
574
|
-
required_error: typeRequiredError
|
|
575
|
-
})
|
|
576
|
-
});
|
|
577
|
-
var TinaFieldZod = import_zod2.z.lazy(() => {
|
|
578
|
-
const TemplateTemp = import_zod2.z.object({
|
|
579
|
-
label: import_zod2.z.string(),
|
|
478
|
+
const typeTypeError = `type must be one of ${TypeName.join(", ")}`;
|
|
479
|
+
const typeRequiredError = `type is required and must be one of ${TypeName.join(", ")}`;
|
|
480
|
+
const nameProp = z.z.string({
|
|
481
|
+
required_error: "name must be provided",
|
|
482
|
+
invalid_type_error: "name must be a sting"
|
|
483
|
+
}).superRefine((val, ctx) => {
|
|
484
|
+
if (val.includes(" "))
|
|
485
|
+
ctx.addIssue({
|
|
486
|
+
message: "name cannot contain spaces",
|
|
487
|
+
code: z.z.ZodIssueCode.custom,
|
|
488
|
+
fatal: true
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
const Option = z.z.union([z.z.string(), z.z.object({ label: z.z.string(), value: z.z.string() })], {
|
|
492
|
+
errorMap: () => {
|
|
493
|
+
return {
|
|
494
|
+
message: "Invalid option array. Must be a string[] or {label: string, value: string}[]"
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
const TinaField = z.z.object({
|
|
580
499
|
name: nameProp,
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
500
|
+
label: z.z.string().optional(),
|
|
501
|
+
description: z.z.string().optional(),
|
|
502
|
+
required: z.z.boolean().optional()
|
|
503
|
+
});
|
|
504
|
+
const FieldWithList = TinaField.extend({ list: z.z.boolean().optional() });
|
|
505
|
+
const TinaScalerBase = FieldWithList.extend({
|
|
506
|
+
options: z.z.array(Option).optional()
|
|
587
507
|
});
|
|
588
|
-
const
|
|
589
|
-
type:
|
|
508
|
+
const StringField = TinaScalerBase.extend({
|
|
509
|
+
type: z.z.literal("string", {
|
|
590
510
|
invalid_type_error: typeTypeError,
|
|
591
511
|
required_error: typeRequiredError
|
|
592
512
|
}),
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
513
|
+
isTitle: z.z.boolean().optional()
|
|
514
|
+
});
|
|
515
|
+
const BooleanField = TinaScalerBase.extend({
|
|
516
|
+
type: z.z.literal("boolean", {
|
|
517
|
+
invalid_type_error: typeTypeError,
|
|
518
|
+
required_error: typeRequiredError
|
|
519
|
+
})
|
|
520
|
+
});
|
|
521
|
+
const NumberField = TinaScalerBase.extend({
|
|
522
|
+
type: z.z.literal("number", {
|
|
523
|
+
invalid_type_error: typeTypeError,
|
|
524
|
+
required_error: typeRequiredError
|
|
525
|
+
})
|
|
526
|
+
});
|
|
527
|
+
const ImageField = TinaScalerBase.extend({
|
|
528
|
+
type: z.z.literal("image", {
|
|
529
|
+
invalid_type_error: typeTypeError,
|
|
530
|
+
required_error: typeRequiredError
|
|
598
531
|
})
|
|
599
532
|
});
|
|
600
|
-
const
|
|
601
|
-
type:
|
|
533
|
+
const DateTimeField = TinaScalerBase.extend({
|
|
534
|
+
type: z.z.literal("datetime", {
|
|
602
535
|
invalid_type_error: typeTypeError,
|
|
603
536
|
required_error: typeRequiredError
|
|
604
537
|
}),
|
|
605
|
-
|
|
606
|
-
|
|
538
|
+
dateFormat: z.z.string().optional(),
|
|
539
|
+
timeFormat: z.z.string().optional()
|
|
540
|
+
});
|
|
541
|
+
const ReferenceField = FieldWithList.extend({
|
|
542
|
+
type: z.z.literal("reference", {
|
|
543
|
+
invalid_type_error: typeTypeError,
|
|
544
|
+
required_error: typeRequiredError
|
|
607
545
|
})
|
|
608
546
|
});
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
ReferenceField,
|
|
616
|
-
ObjectField,
|
|
617
|
-
RichTextField
|
|
618
|
-
], {
|
|
619
|
-
errorMap: (issue, ctx) => {
|
|
547
|
+
const TinaFieldZod = z.z.lazy(() => {
|
|
548
|
+
const TemplateTemp = z.z.object({
|
|
549
|
+
label: z.z.string(),
|
|
550
|
+
name: nameProp,
|
|
551
|
+
fields: z.z.array(TinaFieldZod)
|
|
552
|
+
}).refine((val) => {
|
|
620
553
|
var _a;
|
|
621
|
-
|
|
554
|
+
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
555
|
+
}, {
|
|
556
|
+
message: "Fields must have a unique name"
|
|
557
|
+
});
|
|
558
|
+
const ObjectField = FieldWithList.extend({
|
|
559
|
+
type: z.z.literal("object", {
|
|
560
|
+
invalid_type_error: typeTypeError,
|
|
561
|
+
required_error: typeRequiredError
|
|
562
|
+
}),
|
|
563
|
+
fields: z.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
564
|
+
message: "Fields must have a unique name"
|
|
565
|
+
}),
|
|
566
|
+
templates: z.z.array(TemplateTemp).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
567
|
+
message: "Templates must have a unique name"
|
|
568
|
+
})
|
|
569
|
+
});
|
|
570
|
+
const RichTextField = FieldWithList.extend({
|
|
571
|
+
type: z.z.literal("rich-text", {
|
|
572
|
+
invalid_type_error: typeTypeError,
|
|
573
|
+
required_error: typeRequiredError
|
|
574
|
+
}),
|
|
575
|
+
templates: z.z.array(TemplateTemp).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
576
|
+
message: "Templates must have a unique name"
|
|
577
|
+
})
|
|
578
|
+
});
|
|
579
|
+
return z.z.discriminatedUnion("type", [
|
|
580
|
+
StringField,
|
|
581
|
+
BooleanField,
|
|
582
|
+
NumberField,
|
|
583
|
+
ImageField,
|
|
584
|
+
DateTimeField,
|
|
585
|
+
ReferenceField,
|
|
586
|
+
ObjectField,
|
|
587
|
+
RichTextField
|
|
588
|
+
], {
|
|
589
|
+
errorMap: (issue, ctx) => {
|
|
590
|
+
var _a;
|
|
591
|
+
if (issue.code === "invalid_union_discriminator") {
|
|
592
|
+
return {
|
|
593
|
+
message: `Invalid \`type\` property. In the schema is 'type: ${(_a = ctx.data) == null ? void 0 : _a.type}' and expected one of ${TypeName.join(", ")}`
|
|
594
|
+
};
|
|
595
|
+
}
|
|
622
596
|
return {
|
|
623
|
-
message:
|
|
597
|
+
message: issue.message
|
|
624
598
|
};
|
|
625
599
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
code: import_zod2.z.ZodIssueCode.custom,
|
|
642
|
-
message: "You must have { required: true } when using `isTitle`"
|
|
643
|
-
});
|
|
600
|
+
}).superRefine((val, ctx) => {
|
|
601
|
+
if (val.type === "string") {
|
|
602
|
+
if (val.isTitle) {
|
|
603
|
+
if (val.list) {
|
|
604
|
+
ctx.addIssue({
|
|
605
|
+
code: z.z.ZodIssueCode.custom,
|
|
606
|
+
message: "You can not have `list: true` when using `isTitle`"
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
if (!val.required) {
|
|
610
|
+
ctx.addIssue({
|
|
611
|
+
code: z.z.ZodIssueCode.custom,
|
|
612
|
+
message: "You must have { required: true } when using `isTitle`"
|
|
613
|
+
});
|
|
614
|
+
}
|
|
644
615
|
}
|
|
645
616
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
|
|
650
|
-
if (!isValid) {
|
|
651
|
-
ctx.addIssue({
|
|
652
|
-
code: import_zod2.z.ZodIssueCode.custom,
|
|
653
|
-
message
|
|
654
|
-
});
|
|
655
|
-
return false;
|
|
656
|
-
} else {
|
|
657
|
-
isValid = !((val == null ? void 0 : val.templates) && (val == null ? void 0 : val.fields));
|
|
617
|
+
if (val.type === "object") {
|
|
618
|
+
const message = "Must provide one of templates or fields in your collection";
|
|
619
|
+
let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
|
|
658
620
|
if (!isValid) {
|
|
659
621
|
ctx.addIssue({
|
|
660
|
-
code:
|
|
622
|
+
code: z.z.ZodIssueCode.custom,
|
|
661
623
|
message
|
|
662
624
|
});
|
|
625
|
+
return false;
|
|
626
|
+
} else {
|
|
627
|
+
isValid = !((val == null ? void 0 : val.templates) && (val == null ? void 0 : val.fields));
|
|
628
|
+
if (!isValid) {
|
|
629
|
+
ctx.addIssue({
|
|
630
|
+
code: z.z.ZodIssueCode.custom,
|
|
631
|
+
message
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
return isValid;
|
|
663
635
|
}
|
|
664
|
-
return isValid;
|
|
665
636
|
}
|
|
666
|
-
|
|
667
|
-
|
|
637
|
+
return true;
|
|
638
|
+
});
|
|
668
639
|
});
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
required_error: "label was not provided but is required"
|
|
693
|
-
}),
|
|
694
|
-
name,
|
|
695
|
-
fields: import_zod4.z.array(TinaFieldZod)
|
|
696
|
-
}).refine((val) => {
|
|
697
|
-
var _a;
|
|
698
|
-
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
699
|
-
}, {
|
|
700
|
-
message: "Fields must have a unique name"
|
|
701
|
-
});
|
|
702
|
-
var TinaCloudCollectionBase = import_zod4.z.object({
|
|
703
|
-
label: import_zod4.z.string().optional(),
|
|
704
|
-
name,
|
|
705
|
-
format: import_zod4.z.enum(FORMATS).optional()
|
|
706
|
-
});
|
|
707
|
-
var TinaCloudCollection = TinaCloudCollectionBase.extend({
|
|
708
|
-
fields: import_zod4.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
709
|
-
message: "Fields must have a unique name"
|
|
640
|
+
const tinaConfigKey = z__default["default"].object({
|
|
641
|
+
publicFolder: z__default["default"].string(),
|
|
642
|
+
mediaRoot: z__default["default"].string()
|
|
643
|
+
}).strict().optional();
|
|
644
|
+
const tinaConfigZod = z__default["default"].object({
|
|
645
|
+
client: z__default["default"].object({ referenceDepth: z__default["default"].number().optional() }).optional(),
|
|
646
|
+
media: z__default["default"].object({
|
|
647
|
+
tina: tinaConfigKey,
|
|
648
|
+
loadCustomStore: z__default["default"].function().optional()
|
|
649
|
+
}).optional()
|
|
650
|
+
});
|
|
651
|
+
const validateTinaCloudSchemaConfig = (config) => {
|
|
652
|
+
const newConfig = tinaConfigZod.parse(config);
|
|
653
|
+
return newConfig;
|
|
654
|
+
};
|
|
655
|
+
const FORMATS = ["json", "md", "markdown", "mdx"];
|
|
656
|
+
const Template = z.z.object({
|
|
657
|
+
label: z.z.string({
|
|
658
|
+
invalid_type_error: "label must be a string",
|
|
659
|
+
required_error: "label was not provided but is required"
|
|
660
|
+
}),
|
|
661
|
+
name,
|
|
662
|
+
fields: z.z.array(TinaFieldZod)
|
|
710
663
|
}).refine((val) => {
|
|
711
|
-
|
|
712
|
-
return
|
|
664
|
+
var _a;
|
|
665
|
+
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
713
666
|
}, {
|
|
714
|
-
message: "Fields
|
|
715
|
-
})
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
}, {
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
})
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
constructor(message) {
|
|
738
|
-
super(message);
|
|
739
|
-
this.name = "TinaSchemaValidationError";
|
|
740
|
-
}
|
|
741
|
-
};
|
|
742
|
-
var validateSchema = ({
|
|
743
|
-
config
|
|
744
|
-
}) => {
|
|
745
|
-
try {
|
|
746
|
-
TinaCloudSchemaZod.parse(config);
|
|
747
|
-
} catch (e) {
|
|
748
|
-
if (e instanceof import_zod5.ZodError) {
|
|
749
|
-
const errors = parseZodError({ zodError: e });
|
|
750
|
-
throw new TinaSchemaValidationError(errors.join(", \n"));
|
|
667
|
+
message: "Fields must have a unique name"
|
|
668
|
+
});
|
|
669
|
+
const TinaCloudCollectionBase = z.z.object({
|
|
670
|
+
label: z.z.string().optional(),
|
|
671
|
+
name,
|
|
672
|
+
format: z.z.enum(FORMATS).optional()
|
|
673
|
+
});
|
|
674
|
+
const TinaCloudCollection = TinaCloudCollectionBase.extend({
|
|
675
|
+
fields: z.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
676
|
+
message: "Fields must have a unique name"
|
|
677
|
+
}).refine((val) => {
|
|
678
|
+
const arr = (val == null ? void 0 : val.filter((x) => x.type === "string" && x.isTitle)) || [];
|
|
679
|
+
return arr.length < 2;
|
|
680
|
+
}, {
|
|
681
|
+
message: "Fields can only have one use of `isTitle`"
|
|
682
|
+
}),
|
|
683
|
+
templates: z.z.array(Template).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
684
|
+
message: "Templates must have a unique name"
|
|
685
|
+
})
|
|
686
|
+
}).refine((val) => {
|
|
687
|
+
let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
|
|
688
|
+
if (!isValid) {
|
|
689
|
+
return false;
|
|
751
690
|
} else {
|
|
752
|
-
|
|
691
|
+
isValid = !((val == null ? void 0 : val.templates) && (val == null ? void 0 : val.fields));
|
|
692
|
+
return isValid;
|
|
693
|
+
}
|
|
694
|
+
}, { message: "Must provide one of templates or fields in your collection" });
|
|
695
|
+
const TinaCloudSchemaZod = z.z.object({
|
|
696
|
+
collections: z.z.array(TinaCloudCollection),
|
|
697
|
+
config: tinaConfigZod.optional()
|
|
698
|
+
}).superRefine((val, ctx) => {
|
|
699
|
+
var _a, _b;
|
|
700
|
+
if (hasDuplicates(val.collections.map((x) => x.name))) {
|
|
701
|
+
ctx.addIssue({
|
|
702
|
+
code: z.z.ZodIssueCode.custom,
|
|
703
|
+
message: "can not have two collections with the same name",
|
|
704
|
+
fatal: true
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
(_a = val == null ? void 0 : val.collections) == null ? void 0 : _a.map((x) => {
|
|
708
|
+
if (!x.format) {
|
|
709
|
+
console.warn(`No format provided for collection ${x.name}, defaulting to .md`);
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
const media = (_b = val == null ? void 0 : val.config) == null ? void 0 : _b.media;
|
|
713
|
+
if (media && media.tina && media.loadCustomStore) {
|
|
714
|
+
ctx.addIssue({
|
|
715
|
+
code: z.z.ZodIssueCode.custom,
|
|
716
|
+
message: "can not have both loadCustomStore and tina. Must use one or the other",
|
|
717
|
+
fatal: true,
|
|
718
|
+
path: ["config", "media"]
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
});
|
|
722
|
+
class TinaSchemaValidationError extends Error {
|
|
723
|
+
constructor(message) {
|
|
724
|
+
super(message);
|
|
725
|
+
this.name = "TinaSchemaValidationError";
|
|
753
726
|
}
|
|
754
727
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
728
|
+
const validateSchema = ({
|
|
729
|
+
config
|
|
730
|
+
}) => {
|
|
731
|
+
try {
|
|
732
|
+
TinaCloudSchemaZod.parse(config);
|
|
733
|
+
} catch (e) {
|
|
734
|
+
if (e instanceof z.ZodError) {
|
|
735
|
+
const errors = parseZodError({ zodError: e });
|
|
736
|
+
throw new TinaSchemaValidationError(errors.join(", \n"));
|
|
737
|
+
} else {
|
|
738
|
+
throw new Error(e);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
exports2.TinaSchema = TinaSchema;
|
|
743
|
+
exports2.TinaSchemaValidationError = TinaSchemaValidationError;
|
|
744
|
+
exports2.addNamespaceToSchema = addNamespaceToSchema;
|
|
745
|
+
exports2.resolveField = resolveField;
|
|
746
|
+
exports2.resolveForm = resolveForm;
|
|
747
|
+
exports2.validateSchema = validateSchema;
|
|
748
|
+
exports2.validateTinaCloudSchemaConfig = validateTinaCloudSchemaConfig;
|
|
749
|
+
Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
765
750
|
});
|