UNPKG

dynamodb-toolbox

Version:

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

87 lines (86 loc) 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.recordSchemaFormatter = recordSchemaFormatter; const index_js_1 = require("../../../errors/index.js"); const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js"); const isObject_js_1 = require("../../../utils/validation/isObject.js"); const formatter_js_1 = require("./formatter.js"); const schema_js_1 = require("./schema.js"); const utils_js_1 = require("./utils.js"); function* recordSchemaFormatter(schema, rawValue, { attributes, valuePath, ...restOptions } = {}) { const { format = true, transform = true, partial = false } = restOptions; if (!(0, isObject_js_1.isObject)(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 } }); } const formatters = []; const missingEnumKeys = new Set(schema.keys.props.enum); for (const [key, element] of Object.entries(rawValue)) { if (element === undefined) { continue; } // NOTE: If transform is true, `key` is the transformed value which is what we want // If not, `key` should already be formatted, which is what we also want const elmtValuePath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), key]; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const formattedKey = new formatter_js_1.Formatter(schema.keys).format(key, { transform, valuePath: elmtValuePath }); missingEnumKeys.delete(formattedKey); const { isProjected, childrenAttributes } = (0, utils_js_1.matchMapProjection)(formattedKey, attributes); if (!isProjected) { continue; } formatters.push([ formattedKey, (0, schema_js_1.schemaFormatter)(schema.elements, element, { attributes: childrenAttributes, valuePath: elmtValuePath, ...restOptions }) ]); } if (!schema.props.partial && !partial) { for (const missingKey of missingEnumKeys) { const { isProjected, childrenAttributes } = (0, utils_js_1.matchMapProjection)(missingKey, attributes); if (!isProjected) { continue; } const elmtValuePath = transform && schema.keys.props.transform !== undefined ? [ ...(valuePath !== null && valuePath !== void 0 ? valuePath : []), schema.keys.props.transform.encode(missingKey) ] : [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), missingKey]; formatters.push([ missingKey, (0, schema_js_1.schemaFormatter)(schema.elements, undefined, { attributes: childrenAttributes, valuePath: elmtValuePath, ...restOptions }) ]); } } if (transform) { const transformedValue = Object.fromEntries(formatters .map(([key, formatter]) => [key, formatter.next().value]) .filter(([, element]) => element !== undefined)); if (format) { yield transformedValue; } else { return transformedValue; } } const formattedValue = Object.fromEntries(formatters .map(([key, formatter]) => [key, formatter.next().value]) .filter(([, element]) => element !== undefined)); return formattedValue; }