contentful-migration
Version:
Migration tooling for contentful
163 lines • 6.27 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFieldsSchema = void 0;
const Joi = __importStar(require("joi"));
const field_validations_schema_1 = __importDefault(require("./field-validations-schema"));
const allowed_resources_schema_1 = require("./allowed-resources-schema");
function createFieldsSchema(locales) {
const fieldSchema = Joi.object().keys({
id: Joi.string().required(),
newId: Joi.string()
.invalid(Joi.ref('id'))
.max(64)
// the empty case will be caught by joi by default, we don't want duplicate errors
.regex(/^$|^[a-zA-Z][a-zA-Z0-9_]*$/),
name: Joi.string().required(),
type: Joi.string()
.valid('Symbol', 'Text', 'Integer', 'Number', 'Date', 'Boolean', 'Object', 'Link', 'Array', 'Location', 'RichText', 'ResourceLink')
.required(),
linkType: enforceDependency({
valid: Joi.string().valid('Asset', 'Entry'),
when: 'type',
is: 'Link'
}),
items: enforceDependency({
valid: itemsValid,
when: 'type',
is: 'Array'
}),
omitted: Joi.boolean(),
deleted: Joi.boolean(),
localized: Joi.boolean(),
required: Joi.boolean(),
validations: Joi.array().unique().items(field_validations_schema_1.default),
allowedResources: allowed_resources_schema_1.allowedResourcesSchema,
defaultValue: Joi.object()
.strict()
.pattern(Joi.valid(...locales), Joi.when(Joi.ref('...type'), {
otherwise: Joi.forbidden(),
switch: [
{
is: 'Symbol',
then: Joi.string()
},
{
is: 'Text',
then: Joi.string()
},
{
is: 'Number',
then: Joi.number()
},
{
is: 'Integer',
then: Joi.number().integer()
},
{
is: 'Boolean',
then: Joi.boolean()
},
{
is: 'Date',
then: Joi.date().iso().strict(false)
},
{
is: 'Array',
then: Joi.when(Joi.ref('...items.type'), {
is: 'Symbol',
then: Joi.array().items(Joi.string()),
otherwise: Joi.forbidden()
})
}
]
})),
disabled: Joi.boolean()
});
const fieldsSchema = Joi.array().items(fieldSchema);
return fieldsSchema;
}
exports.createFieldsSchema = createFieldsSchema;
const enforceDependency = function ({ valid, when, is }) {
return valid
.when(when, {
is: is,
then: Joi.any().required(),
otherwise: Joi.any().forbidden()
})
.error((errors) => {
return errors.map((error) => {
const path = error.path;
// top level would be 0.foo
// anything nested would be 0.foo.bar
let subPath = [when];
if (path.length >= 3) {
subPath = path.slice(1, path.length - 1).concat(subPath);
}
const keyPath = subPath.join('.');
if (error.code === 'any.required') {
// When there are nested `enforceDependency` calls
// the error handler is invoked multiple times for every level
// but the same error `local` is propagated
// Thus we only assign this once, at the earliest failing level,
// and leave it in place for the rest of the chain
if (!error.local.isRequiredDependency) {
Object.assign(error.local, {
isRequiredDependency: true,
dependsOn: {
key: keyPath,
value: is
}
});
}
}
if (error.code === 'any.unknown' && error.flags.presence === 'forbidden') {
if (!error.local.isForbiddenDependency) {
Object.assign(error.local, {
isForbiddenDependency: true,
dependsOn: {
key: keyPath,
value: is
}
});
}
}
return error;
});
});
};
const itemsValid = Joi.object().keys({
type: Joi.string().valid('Symbol', 'Link', 'ResourceLink').required(),
linkType: enforceDependency({
valid: Joi.string().valid('Asset', 'Entry'),
when: 'type',
is: 'Link'
}),
validations: Joi.array().unique().items(field_validations_schema_1.default)
});
//# sourceMappingURL=fields-schema.js.map