From 7be292cd81edebb5596997e059a36e0cd7fd02fa Mon Sep 17 00:00:00 2001 From: James Date: Fri, 24 Jan 2020 19:22:32 -0500 Subject: [PATCH] validates missing useAsTitle --- src/collections/validate.js | 9 ++++++++- src/errors/MissingUseAsTitle.js | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/errors/MissingUseAsTitle.js diff --git a/src/collections/validate.js b/src/collections/validate.js index ecd6560979..1f19b79875 100644 --- a/src/collections/validate.js +++ b/src/collections/validate.js @@ -1,12 +1,19 @@ const errors = require('../errors'); -const { DuplicateCollection, MissingCollectionLabel } = errors; +const { DuplicateCollection, MissingCollectionLabel, MissingUseAsTitle } = errors; module.exports = function validateCollection(collection, collections) { if (!collection.labels.singular) { throw new MissingCollectionLabel(collection); } + const { useAsTitle } = collection; + const fieldToUseAsTitle = collection.fields.find(field => useAsTitle === field.name); + + if (!fieldToUseAsTitle) { + throw new MissingUseAsTitle(collection); + } + if (collections && collections[collection.labels.singular]) { throw new DuplicateCollection(collection); } diff --git a/src/errors/MissingUseAsTitle.js b/src/errors/MissingUseAsTitle.js new file mode 100644 index 0000000000..b2ec6d62d7 --- /dev/null +++ b/src/errors/MissingUseAsTitle.js @@ -0,0 +1,9 @@ +const APIError = require('./APIError'); + +class MissingUseAsTitle extends APIError { + constructor(collection) { + super(`${collection.labels.singular} collection is missing a useAsTitle property.`); + } +} + +module.exports = MissingCollectionLabel;