UNPKG

dynamodb-toolbox

Version:

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

98 lines (97 loc) 4.15 kB
import { DynamoDBToolboxError } from '../../../errors/index.js'; import { formatArrayPath } from '../../../schema/actions/utils/formatArrayPath.js'; import { cloneDeep } from '../../../utils/cloneDeep.js'; import { isObject } from '../../../utils/validation/isObject.js'; import { schemaParser } from './schema.js'; import { applyCustomValidation } from './utils.js'; export function* recordSchemaParser(schema, inputValue, options = {}) { const { valuePath, ...restOptions } = options; const { fill = true, transform = true } = restOptions; const parsers = []; const undefinedEntries = []; const missingEnumKeys = new Set(schema.keys.props.enum); const isInputValueObject = isObject(inputValue); if (isInputValueObject) { for (const [key, element] of Object.entries(inputValue)) { if (element === undefined) { undefinedEntries.push([key, undefined]); continue; } missingEnumKeys.delete(key); const nextValuePath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), key]; parsers.push([ schemaParser(schema.keys, key, { ...restOptions, valuePath: nextValuePath }), schemaParser(schema.elements, element, { ...restOptions, defined: false, valuePath: nextValuePath }) ]); } } if (!schema.props.partial && options.mode !== 'update') { for (const missingKey of missingEnumKeys) { const nextValuePath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), missingKey]; parsers.push([ schemaParser(schema.keys, missingKey, { ...restOptions, valuePath: nextValuePath }), schemaParser(schema.elements, undefined, { ...restOptions, defined: false, valuePath: nextValuePath }) ]); } } if (fill) { if (isInputValueObject) { const defaultedValue = Object.fromEntries([ ...parsers .map(([keyParser, elementParser]) => [keyParser.next().value, elementParser.next().value]) .filter(([, element]) => element !== undefined), ...undefinedEntries ]); const itemInput = yield defaultedValue; const linkedValue = Object.fromEntries([ ...parsers .map(([keyParser, elementParser]) => [ keyParser.next().value, elementParser.next(itemInput).value ]) .filter(([, element]) => element !== undefined), ...undefinedEntries ]); yield linkedValue; } else { const defaultedValue = cloneDeep(inputValue); yield defaultedValue; const linkedValue = defaultedValue; yield linkedValue; } } if (!isInputValueObject) { const { type } = schema; const path = valuePath !== undefined ? formatArrayPath(valuePath) : undefined; throw new DynamoDBToolboxError('parsing.invalidAttributeInput', { message: `Attribute${path !== undefined ? ` '${path}'` : ''} should be a ${type}.`, path, payload: { received: inputValue, expected: type } }); } const parsedValue = Object.fromEntries(parsers .map(([keyParser, elementParser]) => [keyParser.next().value, elementParser.next().value]) .filter(([, element]) => element !== undefined)); if (parsedValue !== undefined) { applyCustomValidation(schema, parsedValue, options); } if (transform) { yield parsedValue; } else { return parsedValue; } const transformedValue = Object.fromEntries(parsers .map(([keyParser, elementParser]) => [keyParser.next().value, elementParser.next().value]) .filter(([, element]) => element !== undefined)); return transformedValue; }