@tinacms/schema-tools 0.0.4 → 0.0.7
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 -686
- package/dist/types/SchemaTypes.d.ts +12 -1
- package/dist/validate/index.d.ts +1 -0
- package/dist/validate/schema.d.ts +79 -0
- package/dist/validate/tinaCloudSchemaConfig.d.ts +58 -0
- package/package.json +20 -13
- package/CHANGELOG.md +0 -47
package/dist/index.js
CHANGED
|
@@ -1,746 +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
|
-
});
|
|
63
|
-
|
|
64
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/schema/addNamespaceToSchema.ts
|
|
65
|
-
function addNamespaceToSchema(maybeNode, namespace = []) {
|
|
66
|
-
if (typeof maybeNode === "string") {
|
|
67
|
-
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 };
|
|
68
7
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
+
});
|
|
83
22
|
}
|
|
84
|
-
const value = element.name || element.value;
|
|
85
|
-
return addNamespaceToSchema(element, [...namespace, value]);
|
|
86
23
|
});
|
|
87
|
-
} else {
|
|
88
|
-
if (!m) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (!m.hasOwnProperty("name")) {
|
|
92
|
-
newNode[key] = m;
|
|
93
|
-
} else {
|
|
94
|
-
newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
|
|
95
|
-
}
|
|
96
24
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/validate.ts
|
|
102
|
-
var yup = __toModule(require("yup"));
|
|
103
|
-
function assertShape(value, yupSchema, errorMessage) {
|
|
104
|
-
const shape = yupSchema(yup);
|
|
105
|
-
try {
|
|
106
|
-
shape.validateSync(value);
|
|
107
|
-
} catch (e) {
|
|
108
|
-
const message = errorMessage || `Failed to assertShape - ${e.message}`;
|
|
109
|
-
throw new Error(message);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/lastItem.ts
|
|
114
|
-
var lastItem = (arr) => {
|
|
115
|
-
if (typeof arr === "undefined") {
|
|
116
|
-
throw new Error("Can not call lastItem when arr is undefined");
|
|
117
|
-
}
|
|
118
|
-
return arr[arr.length - 1];
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/namer.ts
|
|
122
|
-
var capitalize = (s) => {
|
|
123
|
-
if (typeof s !== "string")
|
|
124
|
-
return "";
|
|
125
|
-
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
126
|
-
};
|
|
127
|
-
var generateNamespacedFieldName = (names, suffix = "") => {
|
|
128
|
-
return (suffix ? [...names, suffix] : names).map(capitalize).join("");
|
|
129
|
-
};
|
|
130
|
-
var NAMER = {
|
|
131
|
-
dataFilterTypeNameOn: (namespace) => {
|
|
132
|
-
return generateNamespacedFieldName(namespace, "_FilterOn");
|
|
133
|
-
},
|
|
134
|
-
dataFilterTypeName: (namespace) => {
|
|
135
|
-
return generateNamespacedFieldName(namespace, "Filter");
|
|
136
|
-
},
|
|
137
|
-
dataMutationTypeNameOn: (namespace) => {
|
|
138
|
-
return generateNamespacedFieldName(namespace, "_MutationOn");
|
|
139
|
-
},
|
|
140
|
-
dataMutationTypeName: (namespace) => {
|
|
141
|
-
return generateNamespacedFieldName(namespace, "Mutation");
|
|
142
|
-
},
|
|
143
|
-
updateName: (namespace) => {
|
|
144
|
-
return "update" + generateNamespacedFieldName(namespace, "Document");
|
|
145
|
-
},
|
|
146
|
-
createName: (namespace) => {
|
|
147
|
-
return "create" + generateNamespacedFieldName(namespace, "Document");
|
|
148
|
-
},
|
|
149
|
-
queryName: (namespace) => {
|
|
150
|
-
return "get" + generateNamespacedFieldName(namespace, "Document");
|
|
151
|
-
},
|
|
152
|
-
generateQueryListName: (namespace) => {
|
|
153
|
-
return "get" + generateNamespacedFieldName(namespace, "List");
|
|
154
|
-
},
|
|
155
|
-
fragmentName: (namespace) => {
|
|
156
|
-
return generateNamespacedFieldName(namespace, "") + "Parts";
|
|
157
|
-
},
|
|
158
|
-
collectionTypeName: (namespace) => {
|
|
159
|
-
return generateNamespacedFieldName(namespace, "Collection");
|
|
160
|
-
},
|
|
161
|
-
documentTypeName: (namespace) => {
|
|
162
|
-
return generateNamespacedFieldName(namespace, "Document");
|
|
163
|
-
},
|
|
164
|
-
dataTypeName: (namespace) => {
|
|
165
|
-
return generateNamespacedFieldName(namespace, "");
|
|
166
|
-
},
|
|
167
|
-
referenceConnectionType: (namespace) => {
|
|
168
|
-
return generateNamespacedFieldName(namespace, "Connection");
|
|
169
|
-
},
|
|
170
|
-
referenceConnectionEdgesTypeName: (namespace) => {
|
|
171
|
-
return generateNamespacedFieldName(namespace, "ConnectionEdges");
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/hasDuplicates.ts
|
|
176
|
-
function hasDuplicates(array) {
|
|
177
|
-
if (!array) {
|
|
178
|
-
return false;
|
|
179
|
-
} else {
|
|
180
|
-
return new Set(array).size !== array.length;
|
|
25
|
+
n["default"] = e;
|
|
26
|
+
return Object.freeze(n);
|
|
181
27
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (!collection) {
|
|
203
|
-
throw new Error(`Expected to find collection named ${collectionName}`);
|
|
204
|
-
}
|
|
205
|
-
const extraFields = {};
|
|
206
|
-
const templateInfo = this.getTemplatesForCollectable(collection);
|
|
207
|
-
switch (templateInfo.type) {
|
|
208
|
-
case "object":
|
|
209
|
-
extraFields["fields"] = templateInfo.template.fields;
|
|
210
|
-
break;
|
|
211
|
-
case "union":
|
|
212
|
-
extraFields["templates"] = templateInfo.templates;
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
return __spreadProps(__spreadValues(__spreadValues({
|
|
216
|
-
slug: collection.name
|
|
217
|
-
}, extraFields), collection), {
|
|
218
|
-
format: collection.format || "md"
|
|
219
|
-
});
|
|
220
|
-
};
|
|
221
|
-
this.getCollections = () => {
|
|
222
|
-
return this.schema.collections.map((collection) => this.getCollection(collection.name)) || [];
|
|
223
|
-
};
|
|
224
|
-
this.getGlobalTemplate = (templateName) => {
|
|
225
|
-
var _a;
|
|
226
|
-
const globalTemplate = (_a = this.schema.templates) == null ? void 0 : _a.find((template) => template.name === templateName);
|
|
227
|
-
if (!globalTemplate) {
|
|
228
|
-
throw new Error(`Expected to find global template of name ${templateName}`);
|
|
229
|
-
}
|
|
230
|
-
return globalTemplate;
|
|
231
|
-
};
|
|
232
|
-
this.getCollectionByFullPath = (filepath) => {
|
|
233
|
-
const collection = this.getCollections().find((collection2) => {
|
|
234
|
-
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
235
|
-
});
|
|
236
|
-
if (!collection) {
|
|
237
|
-
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
238
|
-
}
|
|
239
|
-
return collection;
|
|
240
|
-
};
|
|
241
|
-
this.getCollectionAndTemplateByFullPath = (filepath, templateName) => {
|
|
242
|
-
let template;
|
|
243
|
-
const collection = this.getCollections().find((collection2) => {
|
|
244
|
-
return filepath.replace("\\", "/").startsWith(collection2.path);
|
|
245
|
-
});
|
|
246
|
-
if (!collection) {
|
|
247
|
-
throw new Error(`Unable to find collection for file at ${filepath}`);
|
|
248
|
-
}
|
|
249
|
-
const templates = this.getTemplatesForCollectable(collection);
|
|
250
|
-
if (templates.type === "union") {
|
|
251
|
-
if (templateName) {
|
|
252
|
-
template = templates.templates.find((template2) => lastItem(template2.namespace) === templateName);
|
|
253
|
-
if (!template) {
|
|
254
|
-
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;
|
|
255
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;
|
|
256
58
|
} else {
|
|
257
|
-
|
|
59
|
+
newNode[key] = addNamespaceToSchema(m, [...namespace, m.name]);
|
|
258
60
|
}
|
|
259
61
|
}
|
|
260
|
-
|
|
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
|
-
|
|
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) {
|
|
294
134
|
return false;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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;
|
|
305
168
|
}
|
|
306
169
|
return {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
})
|
|
170
|
+
slug: collection.name,
|
|
171
|
+
...extraFields,
|
|
172
|
+
...collection,
|
|
173
|
+
format: collection.format || "md"
|
|
312
174
|
};
|
|
313
|
-
}
|
|
314
|
-
|
|
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
|
+
}
|
|
315
261
|
return {
|
|
316
262
|
namespace: collection.namespace,
|
|
317
|
-
type: "
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
});
|
|
323
|
-
})
|
|
263
|
+
type: "object",
|
|
264
|
+
template: {
|
|
265
|
+
...template,
|
|
266
|
+
fields: [...template.fields, ...extraFields]
|
|
267
|
+
}
|
|
324
268
|
};
|
|
325
269
|
} else {
|
|
326
|
-
|
|
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
|
+
}
|
|
327
285
|
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
286
|
+
};
|
|
287
|
+
this.schema = config;
|
|
288
|
+
}
|
|
331
289
|
}
|
|
332
|
-
}
|
|
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
|
-
|
|
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
|
+
}
|
|
360
337
|
if (field.list) {
|
|
361
|
-
return
|
|
362
|
-
component: "
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
338
|
+
return {
|
|
339
|
+
component: "list",
|
|
340
|
+
field: {
|
|
341
|
+
component: "text"
|
|
342
|
+
},
|
|
343
|
+
...field,
|
|
344
|
+
...extraFields
|
|
345
|
+
};
|
|
366
346
|
}
|
|
367
|
-
return
|
|
368
|
-
component: "
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
field: {
|
|
377
|
-
component: "text"
|
|
378
|
-
}
|
|
379
|
-
}, field), extraFields);
|
|
380
|
-
}
|
|
381
|
-
return __spreadValues(__spreadValues({
|
|
382
|
-
component: "text"
|
|
383
|
-
}, field), extraFields);
|
|
384
|
-
case "object":
|
|
385
|
-
const templateInfo = schema.getTemplatesForCollectable(__spreadProps(__spreadValues({}, field), {
|
|
386
|
-
namespace
|
|
387
|
-
}));
|
|
388
|
-
if (templateInfo.type === "object") {
|
|
389
|
-
return __spreadValues(__spreadProps(__spreadValues({}, field), {
|
|
390
|
-
component: field.list ? "group-list" : "group",
|
|
391
|
-
fields: templateInfo.template.fields.map((field2) => resolveField(field2, schema))
|
|
392
|
-
}), extraFields);
|
|
393
|
-
} else if (templateInfo.type === "union") {
|
|
394
|
-
const templates2 = {};
|
|
395
|
-
const typeMap2 = {};
|
|
396
|
-
templateInfo.templates.forEach((template) => {
|
|
397
|
-
const extraFields2 = template.ui || {};
|
|
398
|
-
const templateName = lastItem(template.namespace);
|
|
399
|
-
typeMap2[templateName] = NAMER.dataTypeName(template.namespace);
|
|
400
|
-
templates2[lastItem(template.namespace)] = __spreadValues({
|
|
401
|
-
label: template.label || templateName,
|
|
402
|
-
key: templateName,
|
|
403
|
-
fields: template.fields.map((field2) => resolveField(field2, schema))
|
|
404
|
-
}, extraFields2);
|
|
405
|
-
return true;
|
|
347
|
+
return {
|
|
348
|
+
component: "text",
|
|
349
|
+
...field,
|
|
350
|
+
...extraFields
|
|
351
|
+
};
|
|
352
|
+
case "object":
|
|
353
|
+
const templateInfo = schema.getTemplatesForCollectable({
|
|
354
|
+
...field,
|
|
355
|
+
namespace
|
|
406
356
|
});
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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
|
+
};
|
|
421
386
|
} else {
|
|
422
|
-
|
|
423
|
-
const templateName = lastItem(template.namespace);
|
|
424
|
-
typeMap[templateName] = NAMER.dataTypeName(template.namespace);
|
|
425
|
-
templates[lastItem(template.namespace)] = __spreadValues({
|
|
426
|
-
label: template.label || templateName,
|
|
427
|
-
key: templateName,
|
|
428
|
-
inline: template.inline,
|
|
429
|
-
name: templateName,
|
|
430
|
-
fields: template.fields.map((field2) => resolveField(field2, schema))
|
|
431
|
-
}, extraFields2);
|
|
432
|
-
return true;
|
|
387
|
+
throw new Error(`Unknown object for resolveField function`);
|
|
433
388
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
templates:
|
|
437
|
-
|
|
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
|
-
// pnp:/home/runner/work/tinacms/tinacms/packages/@tinacms/schema-tools/src/util/parseZodErrors.ts
|
|
469
|
-
var parseZodError = ({ zodError }) => {
|
|
470
|
-
var _a;
|
|
471
|
-
const errors = zodError.flatten((issue) => {
|
|
472
|
-
const moreInfo = [];
|
|
473
|
-
if (issue.code === "invalid_union") {
|
|
474
|
-
issue.unionErrors.map((unionError) => {
|
|
475
|
-
moreInfo.push(parseZodError({ zodError: unionError }));
|
|
476
|
-
});
|
|
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}`);
|
|
477
423
|
}
|
|
478
|
-
|
|
479
|
-
|
|
424
|
+
};
|
|
425
|
+
const resolveForm = ({
|
|
426
|
+
collection,
|
|
427
|
+
basename,
|
|
428
|
+
template,
|
|
429
|
+
schema
|
|
430
|
+
}) => {
|
|
480
431
|
return {
|
|
481
|
-
|
|
432
|
+
id: basename,
|
|
433
|
+
label: collection.label,
|
|
434
|
+
name: basename,
|
|
435
|
+
fields: template.fields.map((field) => {
|
|
436
|
+
return resolveField(field, schema);
|
|
437
|
+
})
|
|
482
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"
|
|
483
466
|
});
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
467
|
+
const TypeName = [
|
|
468
|
+
"string",
|
|
469
|
+
"boolean",
|
|
470
|
+
"number",
|
|
471
|
+
"datetime",
|
|
472
|
+
"image",
|
|
473
|
+
"object",
|
|
474
|
+
"reference",
|
|
475
|
+
"rich-text"
|
|
488
476
|
];
|
|
489
|
-
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
var import_zod2 = __toModule(require("zod"));
|
|
504
|
-
var TypeName = [
|
|
505
|
-
"string",
|
|
506
|
-
"boolean",
|
|
507
|
-
"number",
|
|
508
|
-
"datetime",
|
|
509
|
-
"image",
|
|
510
|
-
"object",
|
|
511
|
-
"reference",
|
|
512
|
-
"rich-text"
|
|
513
|
-
];
|
|
514
|
-
var typeTypeError = `type must be one of ${TypeName.join(", ")}`;
|
|
515
|
-
var typeRequiredError = `type is required and must be one of ${TypeName.join(", ")}`;
|
|
516
|
-
var nameProp = import_zod2.z.string({
|
|
517
|
-
required_error: "name must be provided",
|
|
518
|
-
invalid_type_error: "name must be a sting"
|
|
519
|
-
});
|
|
520
|
-
var Option = import_zod2.z.union([import_zod2.z.string(), import_zod2.z.object({ label: import_zod2.z.string(), value: import_zod2.z.string() })], {
|
|
521
|
-
errorMap: () => {
|
|
522
|
-
return {
|
|
523
|
-
message: "Invalid option array. Must be a string[] or {label: string, value: string}[]"
|
|
524
|
-
};
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
var TinaField = import_zod2.z.object({
|
|
528
|
-
name: nameProp,
|
|
529
|
-
label: import_zod2.z.string().optional(),
|
|
530
|
-
description: import_zod2.z.string().optional(),
|
|
531
|
-
required: import_zod2.z.boolean().optional()
|
|
532
|
-
});
|
|
533
|
-
var FieldWithList = TinaField.extend({ list: import_zod2.z.boolean().optional() });
|
|
534
|
-
var TinaScalerBase = FieldWithList.extend({
|
|
535
|
-
options: import_zod2.z.array(Option).optional()
|
|
536
|
-
});
|
|
537
|
-
var StringField = TinaScalerBase.extend({
|
|
538
|
-
type: import_zod2.z.literal("string", {
|
|
539
|
-
invalid_type_error: typeTypeError,
|
|
540
|
-
required_error: typeRequiredError
|
|
541
|
-
}),
|
|
542
|
-
isTitle: import_zod2.z.boolean().optional()
|
|
543
|
-
});
|
|
544
|
-
var BooleanField = TinaScalerBase.extend({
|
|
545
|
-
type: import_zod2.z.literal("boolean", {
|
|
546
|
-
invalid_type_error: typeTypeError,
|
|
547
|
-
required_error: typeRequiredError
|
|
548
|
-
})
|
|
549
|
-
});
|
|
550
|
-
var NumberField = TinaScalerBase.extend({
|
|
551
|
-
type: import_zod2.z.literal("number", {
|
|
552
|
-
invalid_type_error: typeTypeError,
|
|
553
|
-
required_error: typeRequiredError
|
|
554
|
-
})
|
|
555
|
-
});
|
|
556
|
-
var ImageField = TinaScalerBase.extend({
|
|
557
|
-
type: import_zod2.z.literal("image", {
|
|
558
|
-
invalid_type_error: typeTypeError,
|
|
559
|
-
required_error: typeRequiredError
|
|
560
|
-
})
|
|
561
|
-
});
|
|
562
|
-
var DateTimeField = TinaScalerBase.extend({
|
|
563
|
-
type: import_zod2.z.literal("datetime", {
|
|
564
|
-
invalid_type_error: typeTypeError,
|
|
565
|
-
required_error: typeRequiredError
|
|
566
|
-
}),
|
|
567
|
-
dateFormat: import_zod2.z.string().optional(),
|
|
568
|
-
timeFormat: import_zod2.z.string().optional()
|
|
569
|
-
});
|
|
570
|
-
var ReferenceField = FieldWithList.extend({
|
|
571
|
-
type: import_zod2.z.literal("reference", {
|
|
572
|
-
invalid_type_error: typeTypeError,
|
|
573
|
-
required_error: typeRequiredError
|
|
574
|
-
})
|
|
575
|
-
});
|
|
576
|
-
var TinaFieldZod = import_zod2.z.lazy(() => {
|
|
577
|
-
const TemplateTemp = import_zod2.z.object({
|
|
578
|
-
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({
|
|
579
491
|
name: nameProp,
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
492
|
+
label: z.z.string().optional(),
|
|
493
|
+
description: z.z.string().optional(),
|
|
494
|
+
required: z.z.boolean().optional()
|
|
495
|
+
});
|
|
496
|
+
const FieldWithList = TinaField.extend({ list: z.z.boolean().optional() });
|
|
497
|
+
const TinaScalerBase = FieldWithList.extend({
|
|
498
|
+
options: z.z.array(Option).optional()
|
|
586
499
|
});
|
|
587
|
-
const
|
|
588
|
-
type:
|
|
500
|
+
const StringField = TinaScalerBase.extend({
|
|
501
|
+
type: z.z.literal("string", {
|
|
589
502
|
invalid_type_error: typeTypeError,
|
|
590
503
|
required_error: typeRequiredError
|
|
591
504
|
}),
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
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
|
|
597
511
|
})
|
|
598
512
|
});
|
|
599
|
-
const
|
|
600
|
-
type:
|
|
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
|
|
523
|
+
})
|
|
524
|
+
});
|
|
525
|
+
const DateTimeField = TinaScalerBase.extend({
|
|
526
|
+
type: z.z.literal("datetime", {
|
|
601
527
|
invalid_type_error: typeTypeError,
|
|
602
528
|
required_error: typeRequiredError
|
|
603
529
|
}),
|
|
604
|
-
|
|
605
|
-
|
|
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
|
|
606
537
|
})
|
|
607
538
|
});
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
ReferenceField,
|
|
615
|
-
ObjectField,
|
|
616
|
-
RichTextField
|
|
617
|
-
], {
|
|
618
|
-
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) => {
|
|
619
545
|
var _a;
|
|
620
|
-
|
|
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
|
+
}
|
|
621
588
|
return {
|
|
622
|
-
message:
|
|
589
|
+
message: issue.message
|
|
623
590
|
};
|
|
624
591
|
}
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
code: import_zod2.z.ZodIssueCode.custom,
|
|
641
|
-
message: "You must have { required: true } when using `isTitle`"
|
|
642
|
-
});
|
|
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
|
+
}
|
|
643
607
|
}
|
|
644
608
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
let isValid = Boolean(val == null ? void 0 : val.templates) || Boolean(val == null ? void 0 : val.fields);
|
|
649
|
-
if (!isValid) {
|
|
650
|
-
ctx.addIssue({
|
|
651
|
-
code: import_zod2.z.ZodIssueCode.custom,
|
|
652
|
-
message
|
|
653
|
-
});
|
|
654
|
-
return false;
|
|
655
|
-
} else {
|
|
656
|
-
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);
|
|
657
612
|
if (!isValid) {
|
|
658
613
|
ctx.addIssue({
|
|
659
|
-
code:
|
|
614
|
+
code: z.z.ZodIssueCode.custom,
|
|
660
615
|
message
|
|
661
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;
|
|
662
627
|
}
|
|
663
|
-
return isValid;
|
|
664
628
|
}
|
|
665
|
-
|
|
666
|
-
|
|
629
|
+
return true;
|
|
630
|
+
});
|
|
667
631
|
});
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
var TinaCloudCollection = TinaCloudCollectionBase.extend({
|
|
691
|
-
fields: import_zod3.z.array(TinaFieldZod).min(1).optional().refine((val) => !hasDuplicates(val == null ? void 0 : val.map((x) => x.name)), {
|
|
692
|
-
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)
|
|
693
654
|
}).refine((val) => {
|
|
694
|
-
|
|
695
|
-
return
|
|
655
|
+
var _a;
|
|
656
|
+
return !hasDuplicates((_a = val.fields) == null ? void 0 : _a.map((x) => x.name));
|
|
696
657
|
}, {
|
|
697
|
-
message: "Fields
|
|
698
|
-
})
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
}, {
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
})
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
super(message);
|
|
721
|
-
this.name = "TinaSchemaValidationError";
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
var validateSchema = ({
|
|
725
|
-
config
|
|
726
|
-
}) => {
|
|
727
|
-
try {
|
|
728
|
-
TinaCloudSchemaZod.parse(config);
|
|
729
|
-
} catch (e) {
|
|
730
|
-
if (e instanceof import_zod4.ZodError) {
|
|
731
|
-
const errors = parseZodError({ zodError: e });
|
|
732
|
-
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;
|
|
733
681
|
} else {
|
|
734
|
-
|
|
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";
|
|
735
712
|
}
|
|
736
713
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
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" } });
|
|
746
736
|
});
|