import {cond, empty, flow, includedIn, isNot, on, isDefined, filter, and, Map} from 'tsfun'; import {map, forEach, keysAndValues} from 'tsfun/associative'; import {remove} from 'tsfun/collection'; import {ConfigurationErrors} from './configuration-errors'; import {LibraryFieldDefinition} from '../model/library-category-definition'; import {CustomFieldDefinition} from '../model/custom-category-definition'; import {BaseFieldDefinition} from '../model/base-category-definition'; const VALID_INPUT_TYPES = [ 'input', 'multiInput', 'text', 'dropdown', 'dropdownRange', 'radio', 'checkboxes', 'unsignedInt', 'float', 'unsignedFloat', 'dating', 'dimension', 'literature', 'boolean', 'date' ]; export function assertFieldsAreValid(fields: Map|Map, validFieldKeys: string[], source: 'custom'|'library') { assertInputTypesAreValid(fields); assertFieldKeysAreValid(fields, validFieldKeys, source); } function assertInputTypesAreValid(fields: Map|Map) { const isIllegal = and( isDefined, isNot(includedIn(VALID_INPUT_TYPES)) ); flow( keysAndValues(fields), filter(on([1, BaseFieldDefinition.INPUTTYPE], isIllegal)), forEach(([fieldName, field]: any) => { throw [ConfigurationErrors.ILLEGAL_FIELD_INPUT_TYPE, field.inputType, fieldName]; }) ); } function assertFieldKeysAreValid(fields: Map|Map, validFieldKeys: string[], source: 'custom'|'library') { function throwIllegalFieldProperty(keys: string) { throw [ConfigurationErrors.ILLEGAL_FIELD_PROPERTY, source, keys[0]] } const throwIllegalFieldPropertyIfNotEmpty = cond(isNot(empty), throwIllegalFieldProperty); flow( fields, map(Object.keys), map(remove(includedIn(validFieldKeys))), forEach(throwIllegalFieldPropertyIfNotEmpty) ); }