Skip to content

Commit

Permalink
split config and value validation, move selector to reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
erquhart committed May 15, 2018
1 parent 62cc80c commit 1d08984
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
18 changes: 13 additions & 5 deletions src/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 { 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";
Expand Down Expand Up @@ -50,7 +52,7 @@ function validateCollection(collection) {
extension,
frontmatter_delimiter: delimiter,
fields,
} = configCollection.toJS();
} = collection.toJS();

if (!folder && !files) {
throw new Error(`Unknown collection type for collection "${name}". Collections can be either Folder based or File based.`);
Expand All @@ -66,9 +68,9 @@ function validateCollection(collection) {
// 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(fields.map(f => f.name)) === undefined)) {
// Verify that folder-type collections have a "slug"-type field.
throw new Error(`Collection "${name}" must have a field that is a valid entry identifier. Supported fields are ${validIdentifierFields.join(',')}`);
if (!!folder && !selectIdentifier(collection)) {
// 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(', ')}.`);
}
}

Expand Down Expand Up @@ -103,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;
}

Expand Down
25 changes: 10 additions & 15 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
selectEntryPath,
selectAllowNewEntries,
selectAllowDeletion,
selectFolderEntryExtension
selectFolderEntryExtension,
selectIdentifier,
} from "Reducers/collections";
import { selectIdentifier } from "Reducers/entryDraft";
import { createEntry } from "ValueObjects/Entry";
import { sanitizeSlug } from "Lib/urlHelper";
import TestRepoBackend from "./test-repo/implementation";
Expand Down Expand Up @@ -42,19 +42,14 @@ class LocalStorageAuthStore {
}
}

const slugFormatter = (template = "{{slug}}", entryDraft, slugConfig) => {
const slugFormatter = (collection, entryData, slugConfig) => {
const template = collection.get('slug') || "{{slug}}";
const date = new Date();
const entryData = entryDraft.getIn(['entry', 'data']);

const getIdentifier = (entryDraft) => {
const identifier = selectIdentifier(entryDraft);

if (identifier === undefined) {
throw new Error("Collection must have a field name that is a valid entry identifier");
}

return entryData.get(identifier);
};
const identifier = entryData.get(selectIdentifier(collection));
if (identifier === undefined) {
throw new Error("Collection must have a field name that is a valid entry identifier");
}

const slug = template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => {
switch (field) {
Expand All @@ -71,7 +66,7 @@ const slugFormatter = (template = "{{slug}}", entryDraft, slugConfig) => {
case "second":
return (`0${ date.getSeconds() }`).slice(-2);
case "slug":
return getIdentifier(entryDraft).trim();
return identifier.trim();
default:
return entryData.get(field, "").trim();
}
Expand Down Expand Up @@ -245,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, config.get("slug"));
const slug = slugFormatter(collection, entryDraft.getIn(["entry", "data"]), config.get("slug"));
const path = selectEntryPath(collection, slug);
entryObj = {
path,
Expand Down
4 changes: 4 additions & 0 deletions src/reducers/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export const selectListMethod = collection => selectors[collection.get('type')].
export const selectAllowNewEntries = collection => selectors[collection.get('type')].allowNewEntries(collection);
export const selectAllowDeletion = collection => selectors[collection.get('type')].allowDeletion(collection);
export const selectTemplateName = (collection, slug) => selectors[collection.get('type')].templateName(collection, slug);
export const selectIdentifier = collection => {
const fieldNames = collection.get('fields').map(field => field.get('name'));
return IDENTIFIER_FIELDS.find(id => fieldNames.find(name => name.toLowerCase().trim() === id));
};
export const selectInferedField = (collection, fieldName) => {
const inferableField = INFERABLE_FIELDS[fieldName];
const fields = collection.get('fields');
Expand Down
4 changes: 0 additions & 4 deletions src/reducers/entryDraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,4 @@ const entryDraftReducer = (state = Map(), action) => {
}
};

export const selectIdentifier = (entryDraft) => IDENTIFIER_FIELDS
.map(field => entryData.keySeq().find(key => key.toLowerCase().trim() === field))
.find(key => key !== undefined);

export default entryDraftReducer;

0 comments on commit 1d08984

Please sign in to comment.