@indec/form-builder
Version:
Form builder
41 lines (40 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var Yup = _interopRequireWildcard(require("yup"));
var _buildYupSchema = _interopRequireDefault(require("./buildYupSchema"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var getSchema = function getSchema(sections, section, initialValues, schemaType) {
var schema = function schema(sectionValues) {
return section.questions.reduce(function (acc, currentValue) {
return (0, _buildYupSchema["default"])(acc, currentValue, sections, section, initialValues, sectionValues, {
schemaType: schemaType
});
}, {});
};
return Yup.object(_defineProperty({}, section.name, Yup.array().of(Yup.lazy(function (values) {
return Yup.object(schema(values));
}))));
};
var getSchemas = function getSchemas(_ref) {
var _ref$section = _ref.section,
section = _ref$section === void 0 ? {} : _ref$section,
_ref$sections = _ref.sections,
sections = _ref$sections === void 0 ? [] : _ref$sections,
initialValues = _ref.initialValues;
var errorSchema = getSchema(sections, section, initialValues, 'error');
var warningSchema = getSchema(sections, section, initialValues, 'warning');
return {
errorSchema: errorSchema,
warningSchema: warningSchema
};
};
var _default = exports["default"] = getSchemas;