-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check for title/slug field on config load. #1203
Changes from 6 commits
7608ed1
6ea068f
ce48fdd
4138a23
bbae4b5
78f0f50
4044766
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ import yaml from "js-yaml"; | |
import { Map, List, fromJS } from "immutable"; | ||
import { trimStart, flow, isBoolean, get } from "lodash"; | ||
import { authenticateUser } from "Actions/auth"; | ||
import { formatByExtension, supportedFormats, frontmatterFormats } from "Formats/formats"; | ||
import { selectIdentifier } from "Reducers/collections"; | ||
import { IDENTIFIER_FIELDS } from "Constants/fieldInference"; | ||
import * as publishModes from "Constants/publishModes"; | ||
|
||
export const CONFIG_REQUEST = "CONFIG_REQUEST"; | ||
|
@@ -40,6 +43,38 @@ export function applyDefaults(config) { | |
}); | ||
} | ||
|
||
function validateCollection(collection) { | ||
const { | ||
name, | ||
folder, | ||
files, | ||
format, | ||
extension, | ||
frontmatter_delimiter: delimiter, | ||
fields, | ||
} = collection.toJS(); | ||
|
||
if (!folder && !files) { | ||
throw new Error(`Unknown collection type for collection "${name}". Collections can be either Folder based or File based.`); | ||
} | ||
if (format && !supportedFormats.includes(format)) { | ||
throw new Error(`Unknown collection format for collection "${name}". Supported formats are ${supportedFormats.join(',')}`); | ||
} | ||
if (!format && extension && !formatByExtension(extension)) { | ||
// Cannot infer format from extension. | ||
throw new Error(`Please set a format for collection "${name}". Supported formats are ${supportedFormats.join(',')}`); | ||
} | ||
if (delimiter && !frontmatterFormats.includes(format)) { | ||
// 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 (!!folder && !selectIdentifier(collection)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the |
||
// Verify that folder-type collections have an identifier field for slug creation. | ||
throw new Error(`Collection "${name}" must have a field that is a valid entry identifier. Supported fields are ${IDENTIFIER_FIELDS.join(', ')}.`); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This last conditional was added for this PR. |
||
} | ||
|
||
|
||
export function validateConfig(config) { | ||
if (!config.get('backend')) { | ||
throw new Error("Error in configuration file: A `backend` wasn't found. Check your config.yml file."); | ||
|
@@ -70,6 +105,12 @@ export function validateConfig(config) { | |
if (!List.isList(collections) || collections.isEmpty() || !collections.first()) { | ||
throw new Error("Error in configuration file: Your `collections` must be an array with at least one element. Check your config.yml file."); | ||
} | ||
|
||
/** | ||
* Validate Collections | ||
*/ | ||
config.get('collections').forEach(validateCollection); | ||
|
||
return config; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { | |
selectEntryPath, | ||
selectAllowNewEntries, | ||
selectAllowDeletion, | ||
selectFolderEntryExtension | ||
selectFolderEntryExtension, | ||
selectIdentifier, | ||
} from "Reducers/collections"; | ||
import { createEntry } from "ValueObjects/Entry"; | ||
import { sanitizeSlug } from "Lib/urlHelper"; | ||
|
@@ -41,23 +42,14 @@ class LocalStorageAuthStore { | |
} | ||
} | ||
|
||
const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => { | ||
const slugFormatter = (collection, entryData, slugConfig) => { | ||
const template = collection.get('slug') || "{{slug}}"; | ||
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); | ||
|
||
if (identifier === undefined) { | ||
throw new Error("Collection must have a field name that is a valid entry identifier"); | ||
} | ||
|
||
return identifier; | ||
}; | ||
const identifier = entryData.get(selectIdentifier(collection)); | ||
if (identifier === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was just copying existing code, but if we're going for 2.0 this should be fine. |
||
throw new Error("Collection must have a field name that is a valid entry identifier"); | ||
} | ||
|
||
const slug = template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => { | ||
switch (field) { | ||
|
@@ -74,7 +66,7 @@ const slugFormatter = (template = "{{slug}}", entryData, slugConfig) => { | |
case "second": | ||
return (`0${ date.getSeconds() }`).slice(-2); | ||
case "slug": | ||
return getIdentifier(entryData).trim(); | ||
return identifier.trim(); | ||
default: | ||
return entryData.get(field, "").trim(); | ||
} | ||
|
@@ -248,7 +240,7 @@ class Backend { | |
if (!selectAllowNewEntries(collection)) { | ||
throw (new Error("Not allowed to create new entries in this collection")); | ||
} | ||
const slug = slugFormatter(collection.get("slug"), entryDraft.getIn(["entry", "data"]), config.get("slug")); | ||
const slug = slugFormatter(collection, entryDraft.getIn(["entry", "data"]), config.get("slug")); | ||
const path = selectEntryPath(collection, slug); | ||
entryObj = { | ||
path, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a new function, relocated from the collections reducer (because it validates configuration), with the exception of the last conditional.