UNPKG

@nerdware/ddb-single-table

Version:

A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡

40 lines (39 loc) 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkRequired = void 0; const ts_type_safety_utils_1 = require("@nerdware/ts-type-safety-utils"); const index_js_1 = require("../utils/index.js"); /** * This `IOAction` performs nullish-value `item` validation checks: * * @throws {ItemInputError} If an attr is `required`, and the value is missing/`undefined`. * @throws {ItemInputError} If an attr is _**NOT**_ `nullable`, and the value is `null`. */ const checkRequired = function (item, { schemaEntries, modelName, ...ctx }) { // Iterate over schemaEntries for (let i = 0; i < schemaEntries.length; i++) { const [attrName, attrConfig] = schemaEntries[i]; // Check if item is missing a required field if (attrConfig.required === true && (!Object.hasOwn(item, attrName) || (0, ts_type_safety_utils_1.isUndefined)(item[attrName]))) { // Throw error if required field is missing throw new index_js_1.ItemInputError(`A value is required for ${(0, index_js_1.getAttrErrID)(modelName, attrName, attrConfig)}.`); } // Check if item has a non-nullable field with a null value if (attrConfig.nullable !== true && Object.hasOwn(item, attrName) && item[attrName] === null) { // Throw error if non-nullable field is null throw new index_js_1.ItemInputError(`A non-null value is required for ${(0, index_js_1.getAttrErrID)(modelName, attrName, attrConfig)}.`); } // Run recursively on nested attributes if parent exists if (attrConfig.schema && (0, index_js_1.hasDefinedProperty)(item, attrName)) { this.recursivelyApplyIOAction(this.checkRequired, item[attrName], { parentItem: item, modelName, ...ctx, schema: attrConfig.schema, // <-- overwrites ctx.schema with the nested schema }); } } return item; }; exports.checkRequired = checkRequired;