@tinacms/schema-tools 0.0.5 → 0.0.8
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 +701 -0
- package/dist/index.js +676 -705
- package/dist/types/SchemaTypes.d.ts +5 -4
- package/dist/validate/schema.d.ts +20 -11
- package/dist/validate/tinaCloudSchemaConfig.d.ts +12 -7
- package/package.json +20 -13
- package/CHANGELOG.md +0 -53
package/dist/index.js
CHANGED
|
@@ -1,765 +1,736 @@
|
|
|
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
|
+
const col = this.getCollection(collection);
|
|
144
|
+
const field = col == null ? void 0 : col.fields.find((x) => x.type === "string" && x.isTitle);
|
|
145
|
+
return field == null ? void 0 : field.name;
|
|
146
|
+
};
|
|
147
|
+
this.getCollectionsByName = (collectionNames) => {
|
|
148
|
+
return this.schema.collections.filter((collection) => collectionNames.includes(collection.name));
|
|
149
|
+
};
|
|
150
|
+
this.getAllCollectionPaths = () => {
|
|
151
|
+
const paths = this.getCollections().map((collection) => `${collection.path}${collection.match || ""}`);
|
|
152
|
+
return paths;
|
|
153
|
+
};
|
|
154
|
+
this.getCollection = (collectionName) => {
|
|
155
|
+
const collection = this.schema.collections.find((collection2) => collection2.name === collectionName);
|
|
156
|
+
if (!collection) {
|
|
157
|
+
throw new Error(`Expected to find collection named ${collectionName}`);
|
|
158
|
+
}
|
|
159
|
+
const extraFields = {};
|
|
160
|
+
const templateInfo = this.getTemplatesForCollectable(collection);
|
|
161
|
+
switch (templateInfo.type) {
|
|
162
|
+
case "object":
|
|
163
|
+
extraFields["fields"] = templateInfo.template.fields;
|
|
164
|
+
break;
|
|
165
|
+
case "union":
|
|
166
|
+
extraFields["templates"] = templateInfo.templates;
|
|
167
|
+
break;
|
|
306
168
|
}
|
|
307
169
|
return {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
})
|
|
170
|
+
slug: collection.name,
|
|
171
|
+
...extraFields,
|
|
172
|
+
...collection,
|
|
173
|
+
format: collection.format || "md"
|
|
313
174
|
};
|
|
314
|
-
}
|
|
315
|
-
|
|
175
|
+
};
|
|
176
|
+
this.getCollections = () => {
|
|
177
|
+
return this.schema.collections.map((collection) => this.getCollection(collection.name)) || [];
|
|
178
|
+
};
|
|
179
|
+
this.getGlobalTemplate = (templateName) => {
|
|
180
|
+
var _a;
|
|
181
|
+
const globalTemplate = (_a = this.schema.templates) == null ? void 0 : _a.find((template) => template.name === templateName);
|
|
182
|
+
if (!globalTemplate) {
|
|
183
|
+
throw new Error(`Expected to find global template of name ${templateName}`);
|
|
184
|
+
}
|
|
185
|
+
return globalTemplate;
|
|
186
|
+
};
|
|
187
|
+
this.getCollectionByFullPath = (filepath) => {
|
|
188
|
+
const collection = this.getCollections().find((collection2) => {
|
|
189
|
+
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
190
|
+
});
|
|
191
|
+
if (!collection) {
|
|
192
|
+
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
193
|
+
}
|
|
194
|
+
return collection;
|
|
195
|
+
};
|
|
196
|
+
this.getCollectionAndTemplateByFullPath = (filepath, templateName) => {
|
|
197
|
+
let template;
|
|
198
|
+
const collection = this.getCollections().find((collection2) => {
|
|
199
|
+
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
200
|
+
});
|
|
201
|
+
if (!collection) {
|
|
202
|
+
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
203
|
+
}
|
|
204
|
+
const templates = this.getTemplatesForCollectable(collection);
|
|
205
|
+
if (templates.type === "union") {
|
|
206
|
+
if (templateName) {
|
|
207
|
+
template = templates.templates.find((template2) => lastItem(template2.namespace) === templateName);
|
|
208
|
+
if (!template) {
|
|
209
|
+
throw new Error(`Unable to determine template for item at ${filepath}`);
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
throw new Error(`Unable to determine template for item at ${filepath}, no template name provided for collection with multiple templates`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (templates.type === "object") {
|
|
216
|
+
template = templates.template;
|
|
217
|
+
}
|
|
218
|
+
if (!template) {
|
|
219
|
+
throw new Error(`Something went wrong while trying to determine template for ${filepath}`);
|
|
220
|
+
}
|
|
221
|
+
return { collection, template };
|
|
222
|
+
};
|
|
223
|
+
this.getTemplateForData = ({
|
|
224
|
+
data,
|
|
225
|
+
collection
|
|
226
|
+
}) => {
|
|
227
|
+
const templateInfo = this.getTemplatesForCollectable(collection);
|
|
228
|
+
switch (templateInfo.type) {
|
|
229
|
+
case "object":
|
|
230
|
+
return templateInfo.template;
|
|
231
|
+
case "union":
|
|
232
|
+
assertShape(data, (yup2) => yup2.object({ _template: yup2.string().required() }));
|
|
233
|
+
const template = templateInfo.templates.find((template2) => template2.namespace[template2.namespace.length - 1] === data._template);
|
|
234
|
+
if (!template) {
|
|
235
|
+
throw new Error(`Expected to find template named '${data._template}' for collection '${lastItem(collection.namespace)}'`);
|
|
236
|
+
}
|
|
237
|
+
return template;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
this.isMarkdownCollection = (collectionName) => {
|
|
241
|
+
const collection = this.getCollection(collectionName);
|
|
242
|
+
const format = collection.format;
|
|
243
|
+
if (!format) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
if (["markdown", "md"].includes(format)) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
};
|
|
251
|
+
this.getTemplatesForCollectable = (collection) => {
|
|
252
|
+
let extraFields = [];
|
|
253
|
+
if (collection.references) {
|
|
254
|
+
extraFields = collection.references;
|
|
255
|
+
}
|
|
256
|
+
if (collection.fields) {
|
|
257
|
+
const template = typeof collection.fields === "string" ? this.getGlobalTemplate(collection.fields) : collection;
|
|
258
|
+
if (typeof template.fields === "string" || typeof template.fields === "undefined") {
|
|
259
|
+
throw new Error("Exptected template to have fields but none were found");
|
|
260
|
+
}
|
|
316
261
|
return {
|
|
317
262
|
namespace: collection.namespace,
|
|
318
|
-
type: "
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
});
|
|
324
|
-
})
|
|
263
|
+
type: "object",
|
|
264
|
+
template: {
|
|
265
|
+
...template,
|
|
266
|
+
fields: [...template.fields, ...extraFields]
|
|
267
|
+
}
|
|
325
268
|
};
|
|
326
269
|
} else {
|
|
327
|
-
|
|
270
|
+
if (collection.templates) {
|
|
271
|
+
return {
|
|
272
|
+
namespace: collection.namespace,
|
|
273
|
+
type: "union",
|
|
274
|
+
templates: collection.templates.map((templateOrTemplateString) => {
|
|
275
|
+
const template = typeof templateOrTemplateString === "string" ? this.getGlobalTemplate(templateOrTemplateString) : templateOrTemplateString;
|
|
276
|
+
return {
|
|
277
|
+
...template,
|
|
278
|
+
fields: [...template.fields, ...extraFields]
|
|
279
|
+
};
|
|
280
|
+
})
|
|
281
|
+
};
|
|
282
|
+
} else {
|
|
283
|
+
throw new Error(`Expected either fields or templates array to be defined on collection ${collection.namespace.join("_")}`);
|
|
284
|
+
}
|
|
328
285
|
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
|
|
286
|
+
};
|
|
287
|
+
this.schema = config;
|
|
288
|
+
}
|
|
332
289
|
}
|
|
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
|
-
|
|
290
|
+
const resolveField = ({ namespace, ...field }, schema) => {
|
|
291
|
+
var _a;
|
|
292
|
+
field.parentTypename = NAMER.dataTypeName(namespace.filter((_, i) => i < namespace.length - 1));
|
|
293
|
+
const extraFields = field.ui || {};
|
|
294
|
+
switch (field.type) {
|
|
295
|
+
case "number":
|
|
296
|
+
return {
|
|
297
|
+
component: "number",
|
|
298
|
+
...field,
|
|
299
|
+
...extraFields
|
|
300
|
+
};
|
|
301
|
+
case "datetime":
|
|
302
|
+
return {
|
|
303
|
+
component: "date",
|
|
304
|
+
...field,
|
|
305
|
+
...extraFields
|
|
306
|
+
};
|
|
307
|
+
case "boolean":
|
|
308
|
+
return {
|
|
309
|
+
component: "toggle",
|
|
310
|
+
...field,
|
|
311
|
+
...extraFields
|
|
312
|
+
};
|
|
313
|
+
case "image":
|
|
314
|
+
return {
|
|
315
|
+
component: "image",
|
|
316
|
+
clearable: true,
|
|
317
|
+
...field,
|
|
318
|
+
...extraFields
|
|
319
|
+
};
|
|
320
|
+
case "string":
|
|
321
|
+
if (field.options) {
|
|
322
|
+
if (field.list) {
|
|
323
|
+
return {
|
|
324
|
+
component: "checkbox-group",
|
|
325
|
+
...field,
|
|
326
|
+
...extraFields,
|
|
327
|
+
options: field.options
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
component: "select",
|
|
332
|
+
...field,
|
|
333
|
+
...extraFields,
|
|
334
|
+
options: [{ label: `Choose an option`, value: "" }, ...field.options]
|
|
335
|
+
};
|
|
336
|
+
}
|
|
361
337
|
if (field.list) {
|
|
362
|
-
return
|
|
363
|
-
component: "
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
338
|
+
return {
|
|
339
|
+
component: "list",
|
|
340
|
+
field: {
|
|
341
|
+
component: "text"
|
|
342
|
+
},
|
|
343
|
+
...field,
|
|
344
|
+
...extraFields
|
|
345
|
+
};
|
|
367
346
|
}
|
|
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;
|
|
347
|
+
return {
|
|
348
|
+
component: "text",
|
|
349
|
+
...field,
|
|
350
|
+
...extraFields
|
|
351
|
+
};
|
|
352
|
+
case "object":
|
|
353
|
+
const templateInfo = schema.getTemplatesForCollectable({
|
|
354
|
+
...field,
|
|
355
|
+
namespace
|
|
407
356
|
});
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
357
|
+
if (templateInfo.type === "object") {
|
|
358
|
+
return {
|
|
359
|
+
...field,
|
|
360
|
+
component: field.list ? "group-list" : "group",
|
|
361
|
+
fields: templateInfo.template.fields.map((field2) => resolveField(field2, schema)),
|
|
362
|
+
...extraFields
|
|
363
|
+
};
|
|
364
|
+
} else if (templateInfo.type === "union") {
|
|
365
|
+
const templates2 = {};
|
|
366
|
+
const typeMap2 = {};
|
|
367
|
+
templateInfo.templates.forEach((template) => {
|
|
368
|
+
const extraFields2 = template.ui || {};
|
|
369
|
+
const templateName = lastItem(template.namespace);
|
|
370
|
+
typeMap2[templateName] = NAMER.dataTypeName(template.namespace);
|
|
371
|
+
templates2[lastItem(template.namespace)] = {
|
|
372
|
+
label: template.label || templateName,
|
|
373
|
+
key: templateName,
|
|
374
|
+
fields: template.fields.map((field2) => resolveField(field2, schema)),
|
|
375
|
+
...extraFields2
|
|
376
|
+
};
|
|
377
|
+
return true;
|
|
378
|
+
});
|
|
379
|
+
return {
|
|
380
|
+
...field,
|
|
381
|
+
typeMap: typeMap2,
|
|
382
|
+
component: field.list ? "blocks" : "not-implemented",
|
|
383
|
+
templates: templates2,
|
|
384
|
+
...extraFields
|
|
385
|
+
};
|
|
422
386
|
} 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;
|
|
387
|
+
throw new Error(`Unknown object for resolveField function`);
|
|
434
388
|
}
|
|
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
|
-
});
|
|
389
|
+
case "rich-text":
|
|
390
|
+
const templates = {};
|
|
391
|
+
(_a = field.templates) == null ? void 0 : _a.forEach((template) => {
|
|
392
|
+
if (typeof template === "string") {
|
|
393
|
+
throw new Error(`Global templates not yet supported for rich-text`);
|
|
394
|
+
} else {
|
|
395
|
+
const extraFields2 = template.ui || {};
|
|
396
|
+
const templateName = lastItem(template.namespace);
|
|
397
|
+
NAMER.dataTypeName(template.namespace);
|
|
398
|
+
templates[lastItem(template.namespace)] = {
|
|
399
|
+
label: template.label || templateName,
|
|
400
|
+
key: templateName,
|
|
401
|
+
inline: template.inline,
|
|
402
|
+
name: templateName,
|
|
403
|
+
fields: template.fields.map((field2) => resolveField(field2, schema)),
|
|
404
|
+
...extraFields2
|
|
405
|
+
};
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
return {
|
|
410
|
+
...field,
|
|
411
|
+
templates: Object.values(templates),
|
|
412
|
+
component: "rich-text",
|
|
413
|
+
...extraFields
|
|
414
|
+
};
|
|
415
|
+
case "reference":
|
|
416
|
+
return {
|
|
417
|
+
...field,
|
|
418
|
+
component: "reference",
|
|
419
|
+
...extraFields
|
|
420
|
+
};
|
|
421
|
+
default:
|
|
422
|
+
throw new Error(`Unknown field type ${field.type}`);
|
|
478
423
|
}
|
|
479
|
-
|
|
480
|
-
|
|
424
|
+
};
|
|
425
|
+
const resolveForm = ({
|
|
426
|
+
collection,
|
|
427
|
+
basename,
|
|
428
|
+
template,
|
|
429
|
+
schema
|
|
430
|
+
}) => {
|
|
481
431
|
return {
|
|
482
|
-
|
|
432
|
+
id: basename,
|
|
433
|
+
label: collection.label,
|
|
434
|
+
name: basename,
|
|
435
|
+
fields: template.fields.map((field) => {
|
|
436
|
+
return resolveField(field, schema);
|
|
437
|
+
})
|
|
483
438
|
};
|
|
439
|
+
};
|
|
440
|
+
const parseZodError = ({ zodError }) => {
|
|
441
|
+
var _a, _b, _c, _d;
|
|
442
|
+
const errors = zodError.flatten((issue) => {
|
|
443
|
+
const moreInfo = [];
|
|
444
|
+
if (issue.code === "invalid_union") {
|
|
445
|
+
issue.unionErrors.map((unionError) => {
|
|
446
|
+
moreInfo.push(parseZodError({ zodError: unionError }));
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
const errorMessage = `Error ${issue == null ? void 0 : issue.message} at path ${issue.path.join(".")}`;
|
|
450
|
+
const errorMessages = [errorMessage, ...moreInfo];
|
|
451
|
+
return {
|
|
452
|
+
errors: errorMessages
|
|
453
|
+
};
|
|
454
|
+
});
|
|
455
|
+
const formErrors = errors.formErrors.flatMap((x) => x.errors);
|
|
456
|
+
const parsedErrors = [
|
|
457
|
+
...((_b = (_a = errors.fieldErrors) == null ? void 0 : _a.collections) == null ? void 0 : _b.flatMap((x) => x.errors)) || [],
|
|
458
|
+
...((_d = (_c = errors.fieldErrors) == null ? void 0 : _c.config) == null ? void 0 : _d.flatMap((x) => x.errors)) || [],
|
|
459
|
+
...formErrors
|
|
460
|
+
];
|
|
461
|
+
return parsedErrors;
|
|
462
|
+
};
|
|
463
|
+
const name = z.z.string({
|
|
464
|
+
required_error: "Name is required but not provided",
|
|
465
|
+
invalid_type_error: "Name must be a string"
|
|
484
466
|
});
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
467
|
+
const TypeName = [
|
|
468
|
+
"string",
|
|
469
|
+
"boolean",
|
|
470
|
+
"number",
|
|
471
|
+
"datetime",
|
|
472
|
+
"image",
|
|
473
|
+
"object",
|
|
474
|
+
"reference",
|
|
475
|
+
"rich-text"
|
|
489
476
|
];
|
|
490
|
-
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
var import_zod2 = __toModule(require("zod"));
|
|
505
|
-
var TypeName = [
|
|
506
|
-
"string",
|
|
507
|
-
"boolean",
|
|
508
|
-
"number",
|
|
509
|
-
"datetime",
|
|
510
|
-
"image",
|
|
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(),
|
|
477
|
+
const typeTypeError = `type must be one of ${TypeName.join(", ")}`;
|
|
478
|
+
const typeRequiredError = `type is required and must be one of ${TypeName.join(", ")}`;
|
|
479
|
+
const nameProp = z.z.string({
|
|
480
|
+
required_error: "name must be provided",
|
|
481
|
+
invalid_type_error: "name must be a sting"
|
|
482
|
+
});
|
|
483
|
+
const Option = z.z.union([z.z.string(), z.z.object({ label: z.z.string(), value: z.z.string() })], {
|
|
484
|
+
errorMap: () => {
|
|
485
|
+
return {
|
|
486
|
+
message: "Invalid option array. Must be a string[] or {label: string, value: string}[]"
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
const TinaField = z.z.object({
|
|
580
491
|
name: nameProp,
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
585
|
-
}, {
|
|
586
|
-
message: "Fields must have a unique name"
|
|
492
|
+
label: z.z.string().optional(),
|
|
493
|
+
description: z.z.string().optional(),
|
|
494
|
+
required: z.z.boolean().optional()
|
|
587
495
|
});
|
|
588
|
-
const
|
|
589
|
-
|
|
496
|
+
const FieldWithList = TinaField.extend({ list: z.z.boolean().optional() });
|
|
497
|
+
const TinaScalerBase = FieldWithList.extend({
|
|
498
|
+
options: z.z.array(Option).optional()
|
|
499
|
+
});
|
|
500
|
+
const StringField = TinaScalerBase.extend({
|
|
501
|
+
type: z.z.literal("string", {
|
|
590
502
|
invalid_type_error: typeTypeError,
|
|
591
503
|
required_error: typeRequiredError
|
|
592
504
|
}),
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
505
|
+
isTitle: z.z.boolean().optional()
|
|
506
|
+
});
|
|
507
|
+
const BooleanField = TinaScalerBase.extend({
|
|
508
|
+
type: z.z.literal("boolean", {
|
|
509
|
+
invalid_type_error: typeTypeError,
|
|
510
|
+
required_error: typeRequiredError
|
|
511
|
+
})
|
|
512
|
+
});
|
|
513
|
+
const NumberField = TinaScalerBase.extend({
|
|
514
|
+
type: z.z.literal("number", {
|
|
515
|
+
invalid_type_error: typeTypeError,
|
|
516
|
+
required_error: typeRequiredError
|
|
517
|
+
})
|
|
518
|
+
});
|
|
519
|
+
const ImageField = TinaScalerBase.extend({
|
|
520
|
+
type: z.z.literal("image", {
|
|
521
|
+
invalid_type_error: typeTypeError,
|
|
522
|
+
required_error: typeRequiredError
|
|
598
523
|
})
|
|
599
524
|
});
|
|
600
|
-
const
|
|
601
|
-
type:
|
|
525
|
+
const DateTimeField = TinaScalerBase.extend({
|
|
526
|
+
type: z.z.literal("datetime", {
|
|
602
527
|
invalid_type_error: typeTypeError,
|
|
603
528
|
required_error: typeRequiredError
|
|
604
529
|
}),
|
|
605
|
-
|
|
606
|
-
|
|
530
|
+
dateFormat: z.z.string().optional(),
|
|
531
|
+
timeFormat: z.z.string().optional()
|
|
532
|
+
});
|
|
533
|
+
const ReferenceField = FieldWithList.extend({
|
|
534
|
+
type: z.z.literal("reference", {
|
|
535
|
+
invalid_type_error: typeTypeError,
|
|
536
|
+
required_error: typeRequiredError
|
|
607
537
|
})
|
|
608
538
|
});
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
ReferenceField,
|
|
616
|
-
ObjectField,
|
|
617
|
-
RichTextField
|
|
618
|
-
], {
|
|
619
|
-
errorMap: (issue, ctx) => {
|
|
539
|
+
const TinaFieldZod = z.z.lazy(() => {
|
|
540
|
+
const TemplateTemp = z.z.object({
|
|
541
|
+
label: z.z.string(),
|
|
542
|
+
name: nameProp,
|
|
543
|
+
fields: z.z.array(TinaFieldZod)
|
|
544
|
+
}).refine((val) => {
|
|
620
545
|
var _a;
|
|
621
|
-
|
|
546
|
+
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
547
|
+
}, {
|
|
548
|
+
message: "Fields must have a unique name"
|
|
549
|
+
});
|
|
550
|
+
const ObjectField = FieldWithList.extend({
|
|
551
|
+
type: z.z.literal("object", {
|
|
552
|
+
invalid_type_error: typeTypeError,
|
|
553
|
+
required_error: typeRequiredError
|
|
554
|
+
}),
|
|
555
|
+
fields: z.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
556
|
+
message: "Fields must have a unique name"
|
|
557
|
+
}),
|
|
558
|
+
templates: z.z.array(TemplateTemp).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
559
|
+
message: "Templates must have a unique name"
|
|
560
|
+
})
|
|
561
|
+
});
|
|
562
|
+
const RichTextField = FieldWithList.extend({
|
|
563
|
+
type: z.z.literal("rich-text", {
|
|
564
|
+
invalid_type_error: typeTypeError,
|
|
565
|
+
required_error: typeRequiredError
|
|
566
|
+
}),
|
|
567
|
+
templates: z.z.array(TemplateTemp).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
568
|
+
message: "Templates must have a unique name"
|
|
569
|
+
})
|
|
570
|
+
});
|
|
571
|
+
return z.z.discriminatedUnion("type", [
|
|
572
|
+
StringField,
|
|
573
|
+
BooleanField,
|
|
574
|
+
NumberField,
|
|
575
|
+
ImageField,
|
|
576
|
+
DateTimeField,
|
|
577
|
+
ReferenceField,
|
|
578
|
+
ObjectField,
|
|
579
|
+
RichTextField
|
|
580
|
+
], {
|
|
581
|
+
errorMap: (issue, ctx) => {
|
|
582
|
+
var _a;
|
|
583
|
+
if (issue.code === "invalid_union_discriminator") {
|
|
584
|
+
return {
|
|
585
|
+
message: `Invalid \`type\` property. In the schema is 'type: ${(_a = ctx.data) == null ? void 0 : _a.type}' and expected one of ${TypeName.join(", ")}`
|
|
586
|
+
};
|
|
587
|
+
}
|
|
622
588
|
return {
|
|
623
|
-
message:
|
|
589
|
+
message: issue.message
|
|
624
590
|
};
|
|
625
591
|
}
|
|
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
|
-
});
|
|
592
|
+
}).superRefine((val, ctx) => {
|
|
593
|
+
if (val.type === "string") {
|
|
594
|
+
if (val.isTitle) {
|
|
595
|
+
if (val.list) {
|
|
596
|
+
ctx.addIssue({
|
|
597
|
+
code: z.z.ZodIssueCode.custom,
|
|
598
|
+
message: "You can not have `list: true` when using `isTitle`"
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
if (!val.required) {
|
|
602
|
+
ctx.addIssue({
|
|
603
|
+
code: z.z.ZodIssueCode.custom,
|
|
604
|
+
message: "You must have { required: true } when using `isTitle`"
|
|
605
|
+
});
|
|
606
|
+
}
|
|
644
607
|
}
|
|
645
608
|
}
|
|
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));
|
|
609
|
+
if (val.type === "object") {
|
|
610
|
+
const message = "Must provide one of templates or fields in your collection";
|
|
611
|
+
let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
|
|
658
612
|
if (!isValid) {
|
|
659
613
|
ctx.addIssue({
|
|
660
|
-
code:
|
|
614
|
+
code: z.z.ZodIssueCode.custom,
|
|
661
615
|
message
|
|
662
616
|
});
|
|
617
|
+
return false;
|
|
618
|
+
} else {
|
|
619
|
+
isValid = !((val == null ? void 0 : val.templates) && (val == null ? void 0 : val.fields));
|
|
620
|
+
if (!isValid) {
|
|
621
|
+
ctx.addIssue({
|
|
622
|
+
code: z.z.ZodIssueCode.custom,
|
|
623
|
+
message
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
return isValid;
|
|
663
627
|
}
|
|
664
|
-
return isValid;
|
|
665
628
|
}
|
|
666
|
-
|
|
667
|
-
|
|
629
|
+
return true;
|
|
630
|
+
});
|
|
668
631
|
});
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
const
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
invalid_type_error: "label must be a string",
|
|
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"
|
|
632
|
+
const tinaConfigKey = z__default["default"].object({
|
|
633
|
+
publicFolder: z__default["default"].string(),
|
|
634
|
+
mediaRoot: z__default["default"].string()
|
|
635
|
+
}).strict().optional();
|
|
636
|
+
const tinaConfigZod = z__default["default"].object({
|
|
637
|
+
media: z__default["default"].object({
|
|
638
|
+
tina: tinaConfigKey,
|
|
639
|
+
loadCustomStore: z__default["default"].function().optional()
|
|
640
|
+
}).optional()
|
|
641
|
+
});
|
|
642
|
+
const validateTinaCloudSchemaConfig = (config) => {
|
|
643
|
+
const newConfig = tinaConfigZod.parse(config);
|
|
644
|
+
return newConfig;
|
|
645
|
+
};
|
|
646
|
+
const FORMATS = ["json", "md", "markdown", "mdx"];
|
|
647
|
+
const Template = z.z.object({
|
|
648
|
+
label: z.z.string({
|
|
649
|
+
invalid_type_error: "label must be a string",
|
|
650
|
+
required_error: "label was not provided but is required"
|
|
651
|
+
}),
|
|
652
|
+
name,
|
|
653
|
+
fields: z.z.array(TinaFieldZod)
|
|
710
654
|
}).refine((val) => {
|
|
711
|
-
|
|
712
|
-
return
|
|
655
|
+
var _a;
|
|
656
|
+
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
713
657
|
}, {
|
|
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"));
|
|
658
|
+
message: "Fields must have a unique name"
|
|
659
|
+
});
|
|
660
|
+
const TinaCloudCollectionBase = z.z.object({
|
|
661
|
+
label: z.z.string().optional(),
|
|
662
|
+
name,
|
|
663
|
+
format: z.z.enum(FORMATS).optional()
|
|
664
|
+
});
|
|
665
|
+
const TinaCloudCollection = TinaCloudCollectionBase.extend({
|
|
666
|
+
fields: z.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
667
|
+
message: "Fields must have a unique name"
|
|
668
|
+
}).refine((val) => {
|
|
669
|
+
const arr = (val == null ? void 0 : val.filter((x) => x.type === "string" && x.isTitle)) || [];
|
|
670
|
+
return arr.length < 2;
|
|
671
|
+
}, {
|
|
672
|
+
message: "Fields can only have one use of `isTitle`"
|
|
673
|
+
}),
|
|
674
|
+
templates: z.z.array(Template).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
675
|
+
message: "Templates must have a unique name"
|
|
676
|
+
})
|
|
677
|
+
}).refine((val) => {
|
|
678
|
+
let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
|
|
679
|
+
if (!isValid) {
|
|
680
|
+
return false;
|
|
751
681
|
} else {
|
|
752
|
-
|
|
682
|
+
isValid = !((val == null ? void 0 : val.templates) && (val == null ? void 0 : val.fields));
|
|
683
|
+
return isValid;
|
|
684
|
+
}
|
|
685
|
+
}, { message: "Must provide one of templates or fields in your collection" });
|
|
686
|
+
const TinaCloudSchemaZod = z.z.object({
|
|
687
|
+
collections: z.z.array(TinaCloudCollection),
|
|
688
|
+
config: tinaConfigZod.optional()
|
|
689
|
+
}).superRefine((val, ctx) => {
|
|
690
|
+
var _a;
|
|
691
|
+
if (hasDuplicates(val.collections.map((x) => x.name))) {
|
|
692
|
+
ctx.addIssue({
|
|
693
|
+
code: z.z.ZodIssueCode.custom,
|
|
694
|
+
message: "can not have two collections with the same name",
|
|
695
|
+
fatal: true
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
const media = (_a = val == null ? void 0 : val.config) == null ? void 0 : _a.media;
|
|
699
|
+
if (media && media.tina && media.loadCustomStore) {
|
|
700
|
+
ctx.addIssue({
|
|
701
|
+
code: z.z.ZodIssueCode.custom,
|
|
702
|
+
message: "can not have both loadCustomStore and tina. Must use one or the other",
|
|
703
|
+
fatal: true,
|
|
704
|
+
path: ["config", "media"]
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
class TinaSchemaValidationError extends Error {
|
|
709
|
+
constructor(message) {
|
|
710
|
+
super(message);
|
|
711
|
+
this.name = "TinaSchemaValidationError";
|
|
753
712
|
}
|
|
754
713
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
714
|
+
const validateSchema = ({
|
|
715
|
+
config
|
|
716
|
+
}) => {
|
|
717
|
+
try {
|
|
718
|
+
TinaCloudSchemaZod.parse(config);
|
|
719
|
+
} catch (e) {
|
|
720
|
+
if (e instanceof z.ZodError) {
|
|
721
|
+
const errors = parseZodError({ zodError: e });
|
|
722
|
+
throw new TinaSchemaValidationError(errors.join(", \n"));
|
|
723
|
+
} else {
|
|
724
|
+
throw new Error(e);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
exports2.TinaSchema = TinaSchema;
|
|
729
|
+
exports2.TinaSchemaValidationError = TinaSchemaValidationError;
|
|
730
|
+
exports2.addNamespaceToSchema = addNamespaceToSchema;
|
|
731
|
+
exports2.resolveField = resolveField;
|
|
732
|
+
exports2.resolveForm = resolveForm;
|
|
733
|
+
exports2.validateSchema = validateSchema;
|
|
734
|
+
exports2.validateTinaCloudSchemaConfig = validateTinaCloudSchemaConfig;
|
|
735
|
+
Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
765
736
|
});
|