forest-express
Version:
Official package for all Forest Express Lianas
68 lines (66 loc) • 4.74 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var SmartActionFieldValidator = /*#__PURE__*/function () {
function SmartActionFieldValidator() {
(0, _classCallCheck2["default"])(this, SmartActionFieldValidator);
(0, _defineProperty2["default"])(this, "validFieldPrimitifType", ['String', 'Number', 'Date', 'Boolean', 'File', 'Enum', 'Json', 'Dateonly']);
(0, _defineProperty2["default"])(this, "validFieldArrayType", ['String', 'Number', 'Date', 'boolean', 'File', 'Enum']);
}
(0, _createClass2["default"])(SmartActionFieldValidator, [{
key: "validateField",
value: function validateField(field, actionName) {
if (!field || Array.isArray(field) || (0, _typeof2["default"])(field) !== 'object') throw new Error("Field inside fields array on the smart action \"".concat(actionName, "\" must be an object."));
var fieldName = field.field,
description = field.description,
enums = field.enums,
isRequired = field.isRequired,
isReadOnly = field.isReadOnly,
reference = field.reference,
type = field.type;
if (!fieldName) throw new Error("field attribute inside fields array on the smart action \"".concat(actionName, "\" must be defined."));
if (typeof fieldName !== 'string') throw new Error("field attribute inside fields array on the smart action \"".concat(actionName, "\" must be a string."));
if (description && typeof description !== 'string') throw new Error("description of \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\" must be a string."));
if (enums) {
if (!Array.isArray(enums)) throw new Error("enums of \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\" must be an array."));
if (enums.some(function (option) {
return [null, undefined].includes(option);
})) throw new Error("Invalid null or undefined option inside \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\"."));
}
if (isRequired && typeof isRequired !== 'boolean') throw new Error("isRequired of \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\" must be a boolean."));
if (isReadOnly && typeof isReadOnly !== 'boolean') throw new Error("isReadOnly of \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\" must be a boolean."));
if (reference && typeof reference !== 'string') throw new Error("reference of \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\" must be a string."));
if (type !== undefined && (Array.isArray(type) ? !this.validFieldArrayType.includes(type[0]) : !this.validFieldPrimitifType.includes(type))) {
throw new Error("type of \"".concat(fieldName, "\" on the smart action \"").concat(actionName, "\" must be a valid type. See the documentation for more information. https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#available-field-options"));
}
}
// eslint-disable-next-line class-methods-use-this
}, {
key: "validateFieldChangeHook",
value: function validateFieldChangeHook(field, actionName) {
var hooks = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (field.hook && !hooks[field.hook]) {
throw new Error("The hook \"".concat(field.hook, "\" of \"").concat(field.field, "\" field on the smart action \"").concat(actionName, "\" is not defined."));
}
}
}, {
key: "validateSmartActionFields",
value: function validateSmartActionFields(action, collectionName) {
var _this = this;
if (!action.fields) return;
if (!Array.isArray(action.fields)) {
throw new Error("Cannot find the fields you defined for the Smart action \"".concat(action.name, "\" of your \"").concat(collectionName, "\" collection. The fields option must be an array."));
}
action.fields.forEach(function (field) {
var _action$hooks;
_this.validateField(field, action.name);
_this.validateFieldChangeHook(field, action.name, (_action$hooks = action.hooks) === null || _action$hooks === void 0 ? void 0 : _action$hooks.change);
});
}
}]);
return SmartActionFieldValidator;
}();
module.exports = SmartActionFieldValidator;