UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

90 lines (89 loc) 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapSchemaParser = mapSchemaParser; const index_js_1 = require("../../../errors/index.js"); const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js"); const cloneDeep_js_1 = require("../../../utils/cloneDeep.js"); const isObject_js_1 = require("../../../utils/validation/isObject.js"); const schema_js_1 = require("./schema.js"); const utils_js_1 = require("./utils.js"); function* mapSchemaParser(schema, inputValue, options = {}) { const { valuePath, ...restOptions } = options; const { mode = 'put', fill = true, transform = true } = restOptions; const parsers = {}; let restEntries = []; const isInputValueObject = (0, isObject_js_1.isObject)(inputValue); if (isInputValueObject) { const additionalAttributeNames = new Set(Object.keys(inputValue)); Object.entries(schema.attributes) .filter(([, attr]) => mode !== 'key' || attr.props.key) .forEach(([attrName, attr]) => { parsers[attrName] = (0, schema_js_1.schemaParser)(attr, inputValue[attrName], { ...restOptions, valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), attrName], defined: false }); additionalAttributeNames.delete(attrName); }); restEntries = [...additionalAttributeNames.values()].map(attrName => [ attrName, (0, cloneDeep_js_1.cloneDeep)(inputValue[attrName]) ]); } if (fill) { if (isInputValueObject) { const defaultedValue = Object.fromEntries([ ...Object.entries(parsers) .map(([attrName, attr]) => [attrName, attr.next().value]) .filter(([, filledAttrValue]) => filledAttrValue !== undefined), ...restEntries ]); const itemInput = yield defaultedValue; const linkedValue = Object.fromEntries([ ...Object.entries(parsers) .map(([attrName, parser]) => [attrName, parser.next(itemInput).value]) .filter(([, linkedAttrValue]) => linkedAttrValue !== undefined), ...restEntries ]); yield linkedValue; } else { const defaultedValue = (0, cloneDeep_js_1.cloneDeep)(inputValue); yield defaultedValue; const linkedValue = defaultedValue; yield linkedValue; } } if (!isInputValueObject) { const { type } = schema; const path = valuePath !== undefined ? (0, formatArrayPath_js_1.formatArrayPath)(valuePath) : undefined; throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', { message: `Attribute${path !== undefined ? ` '${path}'` : ''} should be a ${type}.`, path, payload: { received: inputValue, expected: type } }); } const parsedValue = Object.fromEntries(Object.entries(parsers) .map(([attrName, schemaParser]) => [attrName, schemaParser.next().value]) .filter(([, attrValue]) => attrValue !== undefined)); if (parsedValue !== undefined) { (0, utils_js_1.applyCustomValidation)(schema, parsedValue, options); } if (transform) { yield parsedValue; } else { return parsedValue; } const transformedValue = Object.fromEntries(Object.entries(parsers) .map(([attrName, schemaParser]) => { var _a; return [ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion (_a = schema.attributes[attrName].props.savedAs) !== null && _a !== void 0 ? _a : attrName, schemaParser.next().value ]; }) .filter(([, attrValue]) => attrValue !== undefined)); return transformedValue; }