alinea
Version:
Headless git-based CMS
83 lines (81 loc) • 2.49 kB
JavaScript
import "../chunks/chunk-NZLE2WMY.js";
// src/core/Schema.ts
import { getType } from "./Internal.js";
import { Type } from "./Type.js";
import { isValidIdentifier } from "./util/Identifiers.js";
import { entries, fromEntries, values } from "./util/Objects.js";
var shapesCache = /* @__PURE__ */ new WeakMap();
var Schema;
((Schema2) => {
function referencedViews(schema2) {
return values(schema2).flatMap((type) => Type.referencedViews(type));
}
Schema2.referencedViews = referencedViews;
function validate(schema2) {
const keyOfType = typeNames(schema2);
for (const [key, type] of entries(schema2)) {
switch (key) {
case "Entry":
throw new Error(`${key} is a reserved Type name`);
default: {
if (!isValidIdentifier(key))
throw new Error(
`Invalid Type name "${key}", use only a-z, A-Z, 0-9, and _`
);
const { contains } = getType(type);
if (contains) {
for (const inner of contains) {
if (typeof inner === "string") {
if (!schema2[inner])
throw new Error(
`Type "${key}" contains "${inner}", but that Type does not exist`
);
} else {
const hasType = keyOfType.has(inner);
if (!hasType)
throw new Error(
`Type "${key}" contains "${Type.label(
inner
)}", but that Type does not exist`
);
}
}
}
}
}
}
}
Schema2.validate = validate;
function shapes(schema2) {
if (!shapesCache.has(schema2))
shapesCache.set(
schema2,
fromEntries(
entries(schema2).map(([key, type]) => {
return [key, Type.shape(type)];
})
)
);
return shapesCache.get(schema2);
}
Schema2.shapes = shapes;
function typeNames(schema2) {
return new Map(entries(schema2).map(([key, type]) => [type, key]));
}
Schema2.typeNames = typeNames;
function contained(schema2, contains) {
const keyOfType = typeNames(schema2);
return contains.map((inner) => {
if (typeof inner === "string") return inner;
return keyOfType.get(inner);
});
}
Schema2.contained = contained;
})(Schema || (Schema = {}));
function schema(options) {
return options.types ?? options;
}
export {
Schema,
schema
};