UNPKG

dynamodb-toolbox

Version:

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

97 lines (96 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseTupleExtension = void 0; const index_js_1 = require("../../../../../errors/index.js"); const index_js_2 = require("../../../../../schema/actions/parse/index.js"); const formatArrayPath_js_1 = require("../../../../../schema/actions/utils/formatArrayPath.js"); const isArray_js_1 = require("../../../../../utils/validation/isArray.js"); const isInteger_js_1 = require("../../../../../utils/validation/isInteger.js"); const isObject_js_1 = require("../../../../../utils/validation/isObject.js"); const index_js_3 = require("../../symbols/index.js"); const attribute_js_1 = require("./attribute.js"); function* tupleElementParser(elementSchema, inputValue, { transform = true, valuePath }) { if (inputValue === undefined) { const parsedValue = undefined; if (transform) { yield parsedValue; } else { return parsedValue; } const transformedValue = undefined; return transformedValue; } return yield* new index_js_2.Parser(elementSchema).start(inputValue, { mode: 'update', fill: false, transform, parseExtension: attribute_js_1.parseUpdateExtension, valuePath }); } const parseTupleExtension = (schema, input, { transform = true, valuePath }) => { if ((0, index_js_3.isSetting)(input) && input[index_js_3.$SET] !== undefined) { return { isExtension: true, *extensionParser() { const parser = new index_js_2.Parser(schema).start(input[index_js_3.$SET], { fill: false, transform, valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$SET'] }); const parsedValue = { [index_js_3.$SET]: parser.next().value }; if (transform) { yield parsedValue; } else { return parsedValue; } const transformedValue = { [index_js_3.$SET]: parser.next().value }; return transformedValue; } }; } const isInputObject = (0, isObject_js_1.isObject)(input); if (isInputObject || (0, isArray_js_1.isArray)(input)) { return { isExtension: true, *extensionParser() { if (isInputObject) { for (const inputKey in input) { const parsedInputKey = parseFloat(inputKey); if (!(0, isInteger_js_1.isInteger)(parsedInputKey) || parsedInputKey < 0 || parsedInputKey > schema.elements.length - 1) { const path = valuePath !== undefined ? (0, formatArrayPath_js_1.formatArrayPath)(valuePath) : undefined; throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', { message: `Index of tuple attribute ${path !== undefined ? `'${path}' ` : ''}is not a valid tuple index`, path, payload: { received: parsedInputKey } }); } } } const parsers = schema.elements.map((elementSchema, index) => tupleElementParser(elementSchema, /** * @debt type "Fix this cast" */ input[index], { transform, valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), index] })); const parsedValue = parsers.map(parser => parser.next().value); if (transform) { yield parsedValue; } else { return parsedValue; } const transformedValue = parsers.map(parser => parser.next().value); return transformedValue; } }; } return { isExtension: false, unextendedInput: input }; }; exports.parseTupleExtension = parseTupleExtension;