UNPKG

dynamodb-toolbox

Version:

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

37 lines (36 loc) 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setSchemaFormatter = setSchemaFormatter; const index_js_1 = require("../../../errors/index.js"); const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js"); const isSet_js_1 = require("../../../utils/validation/isSet.js"); const schema_js_1 = require("./schema.js"); function* setSchemaFormatter(schema, rawValue, { valuePath, ...options } = {}) { const { format = true, transform = true } = options; if (!(0, isSet_js_1.isSet)(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 } }); } // TODO: Remove this cast const formatters = [...rawValue.values()].map((value, index) => (0, schema_js_1.schemaFormatter)(schema.elements, value, { ...options, valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), index], attributes: undefined })); if (transform) { const transformedValue = new Set(formatters.map(formatter => formatter.next().value).filter(value => value !== undefined)); if (format) { yield transformedValue; } else { return transformedValue; } } const formattedValue = new Set(formatters.map(formatter => formatter.next().value).filter(value => value !== undefined)); return formattedValue; }