yarfl
Version:
Yet Another Redux Forms Library
106 lines • 4.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var _path = require("ramda/src/path");
var utils_1 = require("./utils");
exports.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) {
utils_1.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) {
utils_1.throwError("The 'name' property in the config object is required!");
}
switch (fieldsType) {
case 'object':
break;
case 'undefined':
utils_1.throwError("'The 'fields' property in the config object for '" + name + "' is required!");
default:
utils_1.throwError("The 'fields' property in the config object for '" + name + "' must be an object. Type '" + fieldsType + "' was given");
}
if (!Object.entries(fieldsType).length) {
utils_1.throwError('At least one field is required in \'fields\' property in of the config object!');
}
return config;
};
exports.checkActionForKey = function (action) {
if (!action.key) {
utils_1.throwError("Action of type " + action.type + " is missing a value for 'key!'");
}
if (typeof action.key !== 'string') {
utils_1.throwError("Key value for actions of type " + action.type + " must be of type 'string'");
}
return action;
};
exports.checkActionForInputValue = function (action) {
var valueType = typeof action.value;
if (valueType === 'undefined') {
utils_1.throwError("Action of type " + action.type + " is missing 'value'");
}
if (!['string', 'number', 'boolean'].includes(valueType) && !Array.isArray(action.value)) {
utils_1.throwError("'Value' for action " + action.type + " must be a string, number, boolean, string[] or number[], type " + valueType + " given.");
}
return action;
};
exports.checkActionForObjectValue = function (action) {
var type = action.type, value = action.value;
var valueType = typeof value;
if (valueType === 'undefined') {
utils_1.throwError("Action of type " + type + " is missing 'value'");
}
if (typeof value !== 'object' || value === {} || Array.isArray(value)) {
utils_1.throwError("'value' for action " + type + " must be a non-empty object type.");
}
return action;
};
exports.checkActionForKeyAndIndex = function (action) {
exports.checkActionForKey(action);
var valueType = typeof action.index;
if (valueType === 'undefined') {
utils_1.throwError("Action of type " + action.type + " is missing 'index'");
}
if (valueType !== 'number') {
utils_1.throwError("'index' for action " + action.type + " must be a number, type " + valueType + " given.");
}
return action;
};
exports.checkForRenderProp = function (props) {
if (typeof props.render === 'undefined') {
utils_1.throwError("Prop 'render' is required in FormProvider/LocalForm.");
}
if (typeof props.render !== 'function') {
utils_1.throwError("FormProvider/LocalForm's 'render' prop must be a function.");
}
return props;
};
exports.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 utils_1.throwError('Fields is invalid');
};
exports.checkPath = function (path, target, strFormat) {
if (typeof _path(path, target) === 'undefined') {
utils_1.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