dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
87 lines (86 loc) • 3.7 kB
JavaScript
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* mapSchemaParser(schema, inputValue, options = {}) {
const { valuePath, ...restOptions } = options;
const { mode = 'put', fill = true, transform = true } = restOptions;
const parsers = {};
let restEntries = [];
const isInputValueObject = isObject(inputValue);
if (isInputValueObject) {
const additionalAttributeNames = new Set(Object.keys(inputValue));
Object.entries(schema.attributes)
.filter(([, attr]) => mode !== 'key' || attr.props.key)
.forEach(([attrName, attr]) => {
parsers[attrName] = schemaParser(attr, inputValue[attrName], {
...restOptions,
valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), attrName],
defined: false
});
additionalAttributeNames.delete(attrName);
});
restEntries = [...additionalAttributeNames.values()].map(attrName => [
attrName,
cloneDeep(inputValue[attrName])
]);
}
if (fill) {
if (isInputValueObject) {
const defaultedValue = Object.fromEntries([
...Object.entries(parsers)
.map(([attrName, attr]) => [attrName, attr.next().value])
.filter(([, filledAttrValue]) => filledAttrValue !== undefined),
...restEntries
]);
const itemInput = yield defaultedValue;
const linkedValue = Object.fromEntries([
...Object.entries(parsers)
.map(([attrName, parser]) => [attrName, parser.next(itemInput).value])
.filter(([, linkedAttrValue]) => linkedAttrValue !== undefined),
...restEntries
]);
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(Object.entries(parsers)
.map(([attrName, schemaParser]) => [attrName, schemaParser.next().value])
.filter(([, attrValue]) => attrValue !== undefined));
if (parsedValue !== undefined) {
applyCustomValidation(schema, parsedValue, options);
}
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = Object.fromEntries(Object.entries(parsers)
.map(([attrName, schemaParser]) => {
var _a;
return [
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(_a = schema.attributes[attrName].props.savedAs) !== null && _a !== void 0 ? _a : attrName,
schemaParser.next().value
];
})
.filter(([, attrValue]) => attrValue !== undefined));
return transformedValue;
}