Throw config error if unsupported format.

This commit is contained in:
Caleb 2017-11-17 21:05:42 -07:00 committed by Shawn Erquhart
parent db14b9747a
commit d5ff92e47e
2 changed files with 18 additions and 4 deletions

View File

@ -3,6 +3,14 @@ import tomlFormatter from './toml';
import jsonFormatter from './json';
import FrontmatterFormatter from './frontmatter';
export const supportedFormats = [
'markdown',
'yaml',
'toml',
'json',
'html',
];
export const formatToExtension = format => ({
markdown: 'md',
yaml: 'yml',
@ -29,8 +37,11 @@ function formatByName(name) {
yaml: yamlFormatter,
toml: tomlFormatter,
json: jsonFormatter,
md: FrontmatterFormatter,
markdown: FrontmatterFormatter,
html: FrontmatterFormatter,
frontmatter: FrontmatterFormatter,
}[name] || FrontmatterFormatter;
}[name];
}
export function resolveFormat(collectionOrEntity, entry) {
@ -55,5 +66,5 @@ export function resolveFormat(collectionOrEntity, entry) {
}
// If no format is specified and it cannot be inferred, return the default.
return formatByName();
return formatByName('frontmatter');
}

View File

@ -1,10 +1,10 @@
import { OrderedMap, fromJS } from 'immutable';
import { has } from 'lodash';
import { has, get } from 'lodash';
import consoleError from '../lib/consoleError';
import { CONFIG_SUCCESS } from '../actions/config';
import { FILES, FOLDER } from '../constants/collectionTypes';
import { INFERABLE_FIELDS } from '../constants/fieldInference';
import { formatToExtension } from '../formats/formats';
import { formatToExtension, supportedFormats } from '../formats/formats';
const collections = (state = null, action) => {
const configCollections = action.payload && action.payload.collections;
@ -31,6 +31,9 @@ function validateCollection(configCollection) {
if (!has(configCollection, 'folder') && !has(configCollection, 'files')) {
throw new Error(`Unknown collection type for collection "${ collectionName }". Collections can be either Folder based or File based.`);
}
if (has(configCollection, 'format') && !supportedFormats.includes(get(configCollection, 'format'))) {
throw new Error(`Unknown collection format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`);
}
}
const selectors = {