UNPKG

yarfl

Version:

Yet Another Redux Forms Library

104 lines 4.08 kB
import * as _path from 'ramda/src/path'; import { throwError } from './utils'; export var checkConfigs = function (configs) { var checkedConfigs = configs.map(checkConfig); var duplicate = checkedConfigs.reduce(function (acc, curr) { return acc ? acc : checkedConfigs.filter(function (c) { return c.name === curr.name; }).length > 1 ? curr.name : ''; }, ''); if (duplicate) { throwError("The 'name' property must be unique for each form. A config", "object with name '" + duplicate + "' was given more than once."); } }; var checkConfig = function (config) { var fields = config.fields, name = config.name; var fieldsType = typeof fields; if (!name) { throwError("The 'name' property in the config object is required!"); } switch (fieldsType) { case 'object': break; case 'undefined': throwError("'The 'fields' property in the config object for '" + name + "' is required!"); default: throwError("The 'fields' property in the config object for '" + name + "' must be an object. Type '" + fieldsType + "' was given"); } if (!Object.entries(fieldsType).length) { throwError('At least one field is required in \'fields\' property in of the config object!'); } return config; }; export var checkActionForKey = function (action) { if (!action.key) { throwError("Action of type " + action.type + " is missing a value for 'key!'"); } if (typeof action.key !== 'string') { throwError("Key value for actions of type " + action.type + " must be of type 'string'"); } return action; }; export var checkActionForInputValue = function (action) { var valueType = typeof action.value; if (valueType === 'undefined') { throwError("Action of type " + action.type + " is missing 'value'"); } if (!['string', 'number', 'boolean'].includes(valueType) && !Array.isArray(action.value)) { throwError("'Value' for action " + action.type + " must be a string, number, boolean, string[] or number[], type " + valueType + " given."); } return action; }; export var checkActionForObjectValue = function (action) { var type = action.type, value = action.value; var valueType = typeof value; if (valueType === 'undefined') { throwError("Action of type " + type + " is missing 'value'"); } if (typeof value !== 'object' || value === {} || Array.isArray(value)) { throwError("'value' for action " + type + " must be a non-empty object type."); } return action; }; export var checkActionForKeyAndIndex = function (action) { checkActionForKey(action); var valueType = typeof action.index; if (valueType === 'undefined') { throwError("Action of type " + action.type + " is missing 'index'"); } if (valueType !== 'number') { throwError("'index' for action " + action.type + " must be a number, type " + valueType + " given."); } return action; }; export var checkForRenderProp = function (props) { if (typeof props.render === 'undefined') { throwError("Prop 'render' is required in FormProvider/LocalForm."); } if (typeof props.render !== 'function') { throwError("FormProvider/LocalForm's 'render' prop must be a function."); } return props; }; export var checkFieldType = function (field) { var fields = field.fields; if (typeof fields === 'undefined') { return field; } if (Array.isArray(fields)) { return field; } if (typeof fields === 'object') { return field; } return throwError('Fields is invalid'); }; export var checkPath = function (path, target, strFormat) { if (typeof _path(path, target) === 'undefined') { throwError("The given key '" + (strFormat || path.join('.')) + "' does not correspond", "to a defined part of the state tree. Please check that the key is valid"); } return path; }; //# sourceMappingURL=checkers.js.map