UNPKG

@prismicio/types-internal

Version:
168 lines (167 loc) 7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Link = exports.LinkConfig = exports.LinkFieldType = exports.CustomTypes = void 0; const tslib_1 = require("tslib"); const Either_1 = require("fp-ts/lib/Either"); const t = (0, tslib_1.__importStar)(require("io-ts")); const withFallback_1 = require("io-ts-types/lib/withFallback"); const validators_1 = require("../../../validators"); const arrayString = (entries) => { if (entries instanceof Array) { const isValidEntries = entries.reduce((acc, l) => acc && typeof l === "string", true); if (isValidEntries) return t.success(entries); } return; }; const plainString = (entries) => { if (typeof entries === "string") { return t.success([entries]); } return; }; const MasksArrayString = new t.Type("MasksArrayString", (u) => { return u instanceof Array; }, (u, context) => { return Either_1.either.chain(t.union([t.array(t.string), t.string]).validate(u, context), (masks) => { return arrayString(masks) || plainString(masks) || t.failure(u, context); }); }, (res) => res); // Field selection in content relationship fields doesn't support nested groups. const CustomTypeLevel2FieldGroupFields = new t.Type("CustomTypeLevel2FieldGroupFields", (u) => Array.isArray(u) && u.every((item) => typeof item === "string"), (u, context) => Either_1.either.chain(t.array(t.string).validate(u, context), (fields) => { // duplicates not allowed const filtered = new Set(fields); return filtered.size === fields.length ? t.success(fields) : t.failure(u, context, "Fields have duplicates."); }), (a) => a); const CustomTypeLevel2Field = t.union([ t.strict({ id: t.string, fields: CustomTypeLevel2FieldGroupFields, }), t.string, ]); const CustomTypeLevel2Fields = new t.Type("CustomTypeLevel2Fields", (u) => Array.isArray(u) && u.every((item) => CustomTypeLevel2Field.is(item)), (u, context) => Either_1.either.chain(t.array(CustomTypeLevel2Field).validate(u, context), (fields) => { // duplicates not allowed const filtered = new Set(fields.map((field) => (typeof field === "string" ? field : field.id))); return filtered.size === fields.length ? t.success(fields) : t.failure(u, context, "Fields have duplicates."); }), (a) => a); const CustomTypeLevel2 = t.union([ t.string, t.strict({ id: t.string, fields: CustomTypeLevel2Fields, }), ]); const CustomTypesLevel2 = new t.Type("CustomTypesLevel2", (u) => Array.isArray(u) && u.every((item) => CustomTypeLevel2.is(item)), (u, context) => Either_1.either.chain(t.array(CustomTypeLevel2).validate(u, context), (cts) => { // duplicates not allowed const filtered = new Set(cts.map((ct) => (typeof ct === "string" ? ct : ct.id))); return filtered.size === cts.length ? t.success(cts) : t.failure(u, context, "Custom types have duplicates."); }), (a) => a); const CustomTypeLevel1FieldCustomTypes = t.strict({ id: t.string, customtypes: CustomTypesLevel2, }); const CustomTypeLevel1FieldGroupField = t.union([ CustomTypeLevel1FieldCustomTypes, t.string, ]); const CustomTypeLevel1FieldGroupFields = new t.Type("CustomTypeLevel1FieldGroupFields", (u) => Array.isArray(u) && u.every((item) => CustomTypeLevel1FieldGroupField.is(item)), (u, context) => Either_1.either.chain(t.array(CustomTypeLevel1FieldGroupField).validate(u, context), (fields) => { // duplicates not allowed const filtered = new Set(fields.map((field) => (typeof field === "string" ? field : field.id))); return filtered.size === fields.length ? t.success(fields) : t.failure(u, context, "Fields have duplicates."); }), (a) => a); const CustomTypeLevel1Field = t.union([ t.strict({ id: t.string, fields: CustomTypeLevel1FieldGroupFields, }), CustomTypeLevel1FieldCustomTypes, t.string, ]); const CustomTypeLevel1Fields = new t.Type("CustomTypeLevel1Fields", (u) => Array.isArray(u) && u.every((item) => CustomTypeLevel1Field.is(item)), (u, context) => Either_1.either.chain(t.array(CustomTypeLevel1Field).validate(u, context), (fields) => { // duplicates not allowed const filtered = new Set(fields.map((field) => (typeof field === "string" ? field : field.id))); return filtered.size === fields.length ? t.success(fields) : t.failure(u, context, "Fields have duplicates."); }), (a) => a); const CustomTypeLevel1 = t.union([ t.string, t.strict({ id: t.string, fields: CustomTypeLevel1Fields, }), ]); exports.CustomTypes = new t.Type("CustomTypes", (u) => Array.isArray(u) && u.every((item) => CustomTypeLevel1.is(item)), (u, context) => Either_1.either.chain(t.array(CustomTypeLevel1).validate(u, context), (cts) => { // if a ct appears more than once as a string, we allow it (legacy) // if a ct appears once as a string and then again via object (or vice versa), we don't allow it // if a ct appears more than once as an object, we don't allow it const strings = new Set(); const objects = new Set(); for (const ct of cts) { let failed = false; if (typeof ct === "string") { failed = objects.has(ct); strings.add(ct); } else { const { id } = ct; failed = strings.has(id) || objects.has(id); objects.add(id); } if (failed) return t.failure(u, context, "Custom types have duplicates."); } if (objects.size > 1) { return t.failure(u, context, "Cannot have multiple custom types with fields selection."); } if (strings.size > 0 && objects.size > 0) { return t.failure(u, context, "Cannot mix custom types as strings and objects with fields."); } return t.success(cts); }), (a) => a); exports.LinkFieldType = "Link"; exports.LinkConfig = t.exact(t.partial({ label: validators_1.StringOrNull, useAsTitle: t.boolean, placeholder: t.string, select: (0, withFallback_1.withFallback)(t.union([ t.literal("media"), t.literal("document"), t.literal("web"), t.null, ]), null), customtypes: exports.CustomTypes, masks: MasksArrayString, tags: MasksArrayString, allowTargetBlank: t.boolean, allowText: t.boolean, /** * `repeat` property is used to allow multiple links to be added. * `undefined` means that the field is not repeatable (hence repeat = false). */ repeat: t.boolean, /** * `variants` allows an option to be picked from a list (e.g. "primary"). To * be considered, the list must have at least one item. */ variants: t.array(t.string), })); exports.Link = t.exact(t.intersection([ t.type({ type: t.literal(exports.LinkFieldType), }), t.partial({ fieldset: validators_1.StringOrNull, config: exports.LinkConfig, }), ]));