UNPKG

@commodo/fields-storage-ref

Version:

We're working hard to get all the docs in order. New articles will be added daily.

43 lines (33 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fields = require("@commodo/fields"); var _default = ({ instanceOf, list, using }) => { if (!instanceOf) { throw new _fields.WithFieldsError(`When defining a "ref" field, "instanceOf" argument must be set.`, _fields.WithFieldsError.MODEL_FIELD_INSTANCEOF_NOT_SET); } if (!(0, _fields.hasFields)(instanceOf)) { if (!Array.isArray(instanceOf)) { throw new _fields.WithFieldsError(`When defining a "ref" field, "instanceOf" must represent an object with fields.`, _fields.WithFieldsError.MODEL_FIELD_INSTANCEOF_NOT_SET); } // Deciding between `instanceOf: [A, B, C]` and `instanceOf: [[A, B, C], "fieldName"]` const instancesOf = typeof instanceOf[1] === "string" ? instanceOf[0] : instanceOf; for (let i = 0; i < instancesOf.length; i++) { let instanceOfElement = instanceOf[i]; if (!(0, _fields.hasFields)(instanceOfElement)) { throw new _fields.WithFieldsError(`When defining a "ref" field, an "instanceOf" array must contain refs with fields.`, _fields.WithFieldsError.MODEL_FIELD_INSTANCEOF_NOT_SET); } } } if (list && using) { const usingInstanceOf = Array.isArray(using) ? using[0] : using; if (!(0, _fields.hasFields)(usingInstanceOf)) throw new _fields.WithFieldsError(`When defining a "ref" field with "using" ref, and, "using" must represent an object with fields.`, _fields.WithFieldsError.MODEL_FIELD_INSTANCEOF_NOT_SET); } }; exports.default = _default; //# sourceMappingURL=validateArguments.js.map