UNPKG

@nerdware/ddb-single-table

Version:

A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡

60 lines (59 loc) 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assertIsValidWhereQueryComparand = assertIsValidWhereQueryComparand; const index_js_1 = require("../../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. */ 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 (!index_js_1.isType.string(comparandValue)) { throw new index_js_1.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 (!index_js_1.isType.array(comparandValue) || comparandValue.length !== 2 || !((index_js_1.isType.string(comparandValue[0]) && index_js_1.isType.string(comparandValue[1])) || (index_js_1.isType.number(comparandValue[0]) && index_js_1.isType.number(comparandValue[1])))) { throw new index_js_1.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 (!index_js_1.isType.string(comparandValue) && !index_js_1.isType.number(comparandValue)) { throw new index_js_1.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 index_js_1.InvalidExpressionError({ expressionName: "KeyConditionExpression", invalidValue: operator, invalidValueDescription: "WhereQuery comparison operator", problem: "An invalid comparison operator was provided", }); } }