dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
101 lines (100 loc) • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordSchemaParser = recordSchemaParser;
const index_js_1 = require("../../../errors/index.js");
const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js");
const cloneDeep_js_1 = require("../../../utils/cloneDeep.js");
const isObject_js_1 = require("../../../utils/validation/isObject.js");
const schema_js_1 = require("./schema.js");
const utils_js_1 = require("./utils.js");
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 = (0, isObject_js_1.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([
(0, schema_js_1.schemaParser)(schema.keys, key, { ...restOptions, valuePath: nextValuePath }),
(0, schema_js_1.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([
(0, schema_js_1.schemaParser)(schema.keys, missingKey, { ...restOptions, valuePath: nextValuePath }),
(0, schema_js_1.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 = (0, cloneDeep_js_1.cloneDeep)(inputValue);
yield defaultedValue;
const linkedValue = defaultedValue;
yield linkedValue;
}
}
if (!isInputValueObject) {
const { type } = schema;
const path = valuePath !== undefined ? (0, formatArrayPath_js_1.formatArrayPath)(valuePath) : undefined;
throw new index_js_1.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) {
(0, utils_js_1.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;
}