@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
57 lines (56 loc) • 2.77 kB
JavaScript
import { isType, InvalidExpressionError } from "../../utils/index.js";
/**
* Assertion type-guard that ensures the `comparandValue` is valid for the given `operator`.
*
* @param operator The WhereQuery expression operator.
* @param comparandValue The value to check.
* @throws `InvalidExpressionError` if the value is not a valid comparand for the operator.
*/
export function assertIsValidWhereQueryComparand(operator, comparandValue) {
// Ensure the comparandValue is a valid comparand for the operator
if (operator === "beginsWith") {
// For "beginsWith", the comparandValue must be a string.
if (!isType.string(comparandValue)) {
throw new InvalidExpressionError({
expressionName: "KeyConditionExpression",
invalidValue: comparandValue,
invalidValueDescription: "WhereQuery comparison value",
problem: `The value provided to "beginsWith" is not of type "string"`,
});
}
}
else if (operator === "between") {
// For "between", comparandValue must be `[string, string]` or `[number, number]`
if (!isType.array(comparandValue)
|| comparandValue.length !== 2
|| !((isType.string(comparandValue[0]) && isType.string(comparandValue[1]))
|| (isType.number(comparandValue[0]) && isType.number(comparandValue[1])))) {
throw new InvalidExpressionError({
expressionName: "KeyConditionExpression",
invalidValue: comparandValue,
invalidValueDescription: "WhereQuery comparison value",
problem: `The value provided to "between" is not of type "[string, string]" nor "[number, number]"`,
});
}
}
else if (["eq", "lt", "lte", "gt", "gte"].includes(operator)) {
// If the operator is one of these, comparandValue must be a string or number
if (!isType.string(comparandValue) && !isType.number(comparandValue)) {
throw new InvalidExpressionError({
expressionName: "KeyConditionExpression",
invalidValue: comparandValue,
invalidValueDescription: "WhereQuery comparison value",
problem: `The value provided to "${operator}" is not of type "string" nor "number"`,
});
}
}
else {
// Else the OPERATOR is not a valid WhereQuery operator (shouldn't happen here, but just in case)
throw new InvalidExpressionError({
expressionName: "KeyConditionExpression",
invalidValue: operator,
invalidValueDescription: "WhereQuery comparison operator",
problem: "An invalid comparison operator was provided",
});
}
}