diff --git a/src/backends/backend.js b/src/backends/backend.js index a5c4f7eb6bdd..15cb7e53890c 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -2,6 +2,7 @@ import { attempt, isError } from 'lodash'; import { resolveFormat } from "Formats/formats"; import { selectIntegration } from 'Reducers/integrations'; import { + selectIdentifier, selectListMethod, selectEntrySlug, selectEntryPath, @@ -45,15 +46,10 @@ const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => { const date = new Date(); const getIdentifier = (entryData) => { - const validIdentifierFields = ["title", "path"]; - const identifiers = validIdentifierFields.map((field) => - entryData.find((_, key) => key.toLowerCase().trim() === field) - ); - - const identifier = identifiers.find(ident => ident !== undefined); + const identifier = selectIdentifier(entryData.keys()); if (identifier === undefined) { - throw new Error(`Collection must have a field name that is a valid entry identifier. Please add one of these fields: ${ validIdentifierFields }`); + throw new Error("Collection must have a field name that is a valid entry identifier"); } return identifier; diff --git a/src/reducers/collections.js b/src/reducers/collections.js index 3a1abaf1f45b..590457993146 100644 --- a/src/reducers/collections.js +++ b/src/reducers/collections.js @@ -34,7 +34,8 @@ function validateCollection(configCollection) { files, format, extension, - frontmatter_delimiter: delimiter + frontmatter_delimiter: delimiter, + fields, } = configCollection.toJS(); if (!folder && !files) { @@ -51,8 +52,22 @@ function validateCollection(configCollection) { // Cannot set custom delimiter without explicit and proper frontmatter format declaration throw new Error(`Please set a proper frontmatter format for collection "${name}" to use a custom delimiter. Supported frontmatter formats are yaml-frontmatter, toml-frontmatter, and json-frontmatter.`); } + if (selectIdentifier(fields.map(f => f.name)) === undefined) { + // Verify that the collection has a slug field. + // TODO: Verify only for folder-type collections. + throw new Error(`Collection "${name}" must have a field that is a valid entry identifier. Supported fields are ${validIdentifierFields.join(',')}`); + } } +const validIdentifierFields = ["title", "path"]; +export const selectIdentifier = (entryData, collectionName) => { + const identifiers = validIdentifierFields.map((field) => + entryData.find(key => key.toLowerCase().trim() === field) + ); + + return identifiers.find(ident => ident !== undefined); +}; + const selectors = { [FOLDER]: { entryExtension(collection) {