dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
125 lines (124 loc) • 6.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseNumberExtension = void 0;
const index_js_1 = require("../../../../../errors/index.js");
const index_js_2 = require("../../../../../schema/actions/parse/index.js");
const formatArrayPath_js_1 = require("../../../../../schema/actions/utils/formatArrayPath.js");
const schema_js_1 = require("../../../../../schema/number/schema.js");
const isArray_js_1 = require("../../../../../utils/validation/isArray.js");
const index_js_3 = require("../../symbols/index.js");
const reference_js_1 = require("./reference.js");
const parseNumberExtension = (schema, inputValue, { transform = true, valuePath } = {}) => {
const { props } = schema;
if ((0, index_js_3.isSum)(inputValue) && inputValue[index_js_3.$SUM] !== undefined) {
return {
isExtension: true,
*extensionParser() {
const sumElements = inputValue[index_js_3.$SUM];
const sumValuePath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$SUM'];
if (!(0, isArray_js_1.isArray)(sumElements) || sumElements.length !== 2) {
const path = (0, formatArrayPath_js_1.formatArrayPath)(sumValuePath);
throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', {
message: `Sum for number attribute ${path !== undefined ? `'${path}' ` : ''}should be a tuple of length 2`,
path,
payload: { received: inputValue[index_js_3.$SUM] }
});
}
const [left, right] = sumElements;
const parsers = [
new index_js_2.Parser(new schema_js_1.NumberSchema({ big: props.big })).start(left, {
fill: false,
transform,
parseExtension: reference_js_1.parseReferenceExtension,
valuePath: [...sumValuePath, 0]
}),
new index_js_2.Parser(new schema_js_1.NumberSchema({ big: props.big })).start(right, {
fill: false,
transform,
parseExtension: reference_js_1.parseReferenceExtension,
valuePath: [...sumValuePath, 1]
})
];
const parsedValue = { [index_js_3.$SUM]: parsers.map(parser => parser.next().value) };
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = { [index_js_3.$SUM]: parsers.map(parser => parser.next().value) };
return transformedValue;
}
};
}
if ((0, index_js_3.isSubtraction)(inputValue) && inputValue[index_js_3.$SUBTRACT] !== undefined) {
return {
isExtension: true,
*extensionParser() {
const subtractElements = inputValue[index_js_3.$SUBTRACT];
const subtractValuePath = [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$SUBTRACT'];
if (!(0, isArray_js_1.isArray)(subtractElements) || subtractElements.length !== 2) {
const path = (0, formatArrayPath_js_1.formatArrayPath)(subtractValuePath);
throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', {
message: `Subtraction for number attribute ${path !== undefined ? `'${path}' ` : ''}should be a tuple of length 2`,
path,
payload: {
received: inputValue[index_js_3.$SUBTRACT]
}
});
}
const [left, right] = subtractElements;
const parsers = [
new index_js_2.Parser(new schema_js_1.NumberSchema({ big: props.big })).start(left, {
fill: false,
transform,
parseExtension: reference_js_1.parseReferenceExtension,
valuePath: [...subtractValuePath, 0]
}),
new index_js_2.Parser(new schema_js_1.NumberSchema({ big: props.big })).start(right, {
fill: false,
transform,
parseExtension: reference_js_1.parseReferenceExtension,
valuePath: [...subtractValuePath, 1]
})
];
const parsedValue = { [index_js_3.$SUBTRACT]: parsers.map(parser => parser.next().value) };
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = { [index_js_3.$SUBTRACT]: parsers.map(parser => parser.next().value) };
return transformedValue;
}
};
}
if ((0, index_js_3.isAddition)(inputValue) && inputValue[index_js_3.$ADD] !== undefined) {
const parser = new index_js_2.Parser(new schema_js_1.NumberSchema({ big: props.big })).start(inputValue[index_js_3.$ADD], {
fill: false,
transform,
parseExtension: reference_js_1.parseReferenceExtension,
valuePath: [...(valuePath !== null && valuePath !== void 0 ? valuePath : []), '$ADD']
});
return {
isExtension: true,
*extensionParser() {
const parsedValue = { [index_js_3.$ADD]: parser.next().value };
if (transform) {
yield parsedValue;
}
else {
return parsedValue;
}
const transformedValue = { [index_js_3.$ADD]: parser.next().value };
return transformedValue;
}
};
}
return {
isExtension: false,
unextendedInput: inputValue
};
};
exports.parseNumberExtension = parseNumberExtension;