UNPKG

dynamodb-toolbox

Version:

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

42 lines (41 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listSchemaFormatter = listSchemaFormatter; const index_js_1 = require("../../../errors/index.js"); const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js"); const isArray_js_1 = require("../../../utils/validation/isArray.js"); const schema_js_1 = require("./schema.js"); const utils_js_1 = require("./utils.js"); function* listSchemaFormatter(schema, rawValue, { attributes, valuePath, ...restOptions } = {}) { const { format = true, transform = true } = restOptions; if (!(0, isArray_js_1.isArray)(rawValue)) { const { type } = schema; const path = valuePath !== undefined ? (0, formatArrayPath_js_1.formatArrayPath)(valuePath) : undefined; throw new index_js_1.DynamoDBToolboxError('formatter.invalidAttribute', { message: `Invalid attribute detected while formatting${path !== undefined ? `: '${path}'` : ''}. Should be a ${type}.`, path, payload: { received: rawValue, expected: type } }); } // We don't need isProjected: // - Either whole list is projected and we already know => projectedAttributes undefined // - Either some elements are projected => childrenAttributes undefined // - Either projection is deep => childrenAttributes defined const { childrenAttributes } = (0, utils_js_1.matchListProjection)(attributes); const formatters = rawValue.map((element, index) => (0, schema_js_1.schemaFormatter)(schema.elements, element, { attributes: childrenAttributes, valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), index], ...restOptions })); if (transform) { const transformedValue = formatters.map(formatter => formatter.next().value); if (format) { yield transformedValue; } else { return transformedValue; } } const formattedValue = formatters.map(formatter => formatter.next().value); return formattedValue; }