UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

56 lines (55 loc) 2.33 kB
import { DynamoDBToolboxError } from '../../errors/index.js'; import { isBoolean } from '../../utils/validation/isBoolean.js'; import { isString } from '../../utils/validation/isString.js'; export const schemaRequiredPropSet = new Set(['never', 'atLeastOnce', 'always']); /** * Validates an attribute shared properties * * @param props Schema Props * @param path Path of the instance in the related schema (string) * @return void */ export const checkSchemaProps = (props, path) => { const { required, hidden, key, savedAs } = props; if (required !== undefined && !schemaRequiredPropSet.has(required)) { throw new DynamoDBToolboxError('schema.invalidProp', { message: `Invalid prop type${path !== undefined ? ` at path '${path}'` : ''}. Property: 'required'. Expected: ${[...schemaRequiredPropSet].join(', ')}. Received: ${String(required)}.`, path, payload: { propName: 'required', expected: [...schemaRequiredPropSet].join(', '), received: required } }); } if (hidden !== undefined && !isBoolean(hidden)) { throw new DynamoDBToolboxError('schema.invalidProp', { message: `Invalid prop type${path !== undefined ? ` at path '${path}'` : ''}. Property: 'hidden'. Expected: boolean. Received: ${String(hidden)}.`, path, payload: { propName: 'hidden', received: hidden } }); } if (key !== undefined && !isBoolean(key)) { throw new DynamoDBToolboxError('schema.invalidProp', { message: `Invalid prop type${path !== undefined ? ` at path '${path}'` : ''}. Property: 'key'. Expected: boolean. Received: ${String(key)}.`, path, payload: { propName: 'key', received: key } }); } if (savedAs !== undefined && !isString(savedAs)) { throw new DynamoDBToolboxError('schema.invalidProp', { message: `Invalid prop type${path !== undefined ? ` at path '${path}'` : ''}. Property: 'savedAs'. Expected: string. Received: ${String(savedAs)}.`, path, payload: { propName: 'savedAs', received: savedAs } }); } };