dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
44 lines (43 loc) • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkPrimitiveSchema = void 0;
const index_js_1 = require("../../errors/index.js");
const isStaticDefault_js_1 = require("../../schema/utils/isStaticDefault.js");
const isValidPrimitive_js_1 = require("../../utils/validation/isValidPrimitive.js");
const checkSchemaProps_js_1 = require("../utils/checkSchemaProps.js");
const checkPrimitiveSchema = (schema, path) => {
(0, checkSchemaProps_js_1.checkSchemaProps)(schema.props, path);
const { type, props } = schema;
const { enum: enumValues } = props;
enumValues === null || enumValues === void 0 ? void 0 : enumValues.forEach(enumValue => {
if (!(0, isValidPrimitive_js_1.isValidPrimitive)(schema, enumValue)) {
throw new index_js_1.DynamoDBToolboxError('schema.primitive.invalidEnumValueType', {
message: `Invalid enum value type${path !== undefined ? ` at path '${path}'` : ''}. Expected: ${type}. Received: ${String(enumValue)}.`,
path,
payload: { expectedType: type, enumValue }
});
}
});
for (const defaultValue of [props.keyDefault, props.putDefault, props.updateDefault]) {
if (defaultValue === undefined) {
continue;
}
if ((0, isStaticDefault_js_1.isStaticDefault)(defaultValue)) {
if (!(0, isValidPrimitive_js_1.isValidPrimitive)(schema, defaultValue)) {
throw new index_js_1.DynamoDBToolboxError('schema.primitive.invalidDefaultValueType', {
message: `Invalid default value type${path !== undefined ? ` at path '${path}'` : ''}: Expected: ${type}. Received: ${String(defaultValue)}.`,
path,
payload: { expectedType: type, defaultValue }
});
}
if (enumValues !== undefined && !enumValues.some(enumValue => enumValue === defaultValue)) {
throw new index_js_1.DynamoDBToolboxError('schema.primitive.invalidDefaultValueRange', {
message: `Invalid default value${path !== undefined ? ` at path '${path}'` : ''}: Expected one of: ${enumValues.join(', ')}. Received: ${String(defaultValue)}.`,
path,
payload: { enumValues, defaultValue }
});
}
}
}
};
exports.checkPrimitiveSchema = checkPrimitiveSchema;