dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
39 lines (38 loc) • 1.88 kB
JavaScript
import { DynamoDBToolboxError } from '../../../errors/index.js';
import { formatArrayPath } from '../../../schema/actions/utils/formatArrayPath.js';
import { isArray } from '../../../utils/validation/isArray.js';
import { schemaFormatter } from './schema.js';
import { matchListProjection } from './utils.js';
export function* listSchemaFormatter(schema, rawValue, { attributes, valuePath, ...restOptions } = {}) {
const { format = true, transform = true } = restOptions;
if (!isArray(rawValue)) {
const { type } = schema;
const path = valuePath !== undefined ? formatArrayPath(valuePath) : undefined;
throw new 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 } = matchListProjection(attributes);
const formatters = rawValue.map((element, index) => 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;
}