Skip to content

Commit

Permalink
Verify slug field exists on config load.
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 committed Mar 28, 2018
1 parent 8b9afcc commit fec276b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { attempt, isError } from 'lodash';
import { resolveFormat } from "Formats/formats";
import { selectIntegration } from 'Reducers/integrations';
import {
selectIdentifier,
selectListMethod,
selectEntrySlug,
selectEntryPath,
Expand Down Expand Up @@ -45,15 +46,10 @@ const slugFormatter = (template = "{{slug}}", entryData) => {
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;
Expand Down
17 changes: 16 additions & 1 deletion src/reducers/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function validateCollection(configCollection) {
files,
format,
extension,
frontmatter_delimiter: delimiter
frontmatter_delimiter: delimiter,
fields,
} = configCollection.toJS();

if (!folder && !files) {
Expand All @@ -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) {
Expand Down

0 comments on commit fec276b

Please sign in to comment.