UNPKG

dynamodb-toolbox

Version:

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

46 lines (45 loc) 1.85 kB
import { DynamoDBToolboxError } from '../../../errors/index.js'; import { formatArrayPath } from '../../../schema/actions/utils/formatArrayPath.js'; import { isString } from '../../../utils/validation/isString.js'; export const defaultParseExtension = (_, input) => ({ isExtension: false, unextendedInput: input }); export const isRequired = (schema, mode) => { var _a, _b; switch (mode) { case 'put': return ((_a = schema.props) === null || _a === void 0 ? void 0 : _a.required) !== 'never'; case 'key': case 'update': return ((_b = schema.props) === null || _b === void 0 ? void 0 : _b.required) === 'always'; } }; const getValidator = (schema, mode) => { if (schema.props.key) { return schema.props.keyValidator; } switch (mode) { case 'key': return schema.props.keyValidator; case 'put': return schema.props.putValidator; case 'update': return schema.props.updateValidator; } }; export const applyCustomValidation = (schema, inputValue, options = {}) => { const { mode = 'put', valuePath } = options; const customValidator = getValidator(schema, mode); if (customValidator !== undefined) { const validationResult = customValidator(inputValue, schema); if (validationResult !== true) { const path = valuePath !== undefined ? formatArrayPath(valuePath) : undefined; throw new DynamoDBToolboxError('parsing.customValidationFailed', { message: `Custom validation${path !== undefined ? ` for attribute '${path}'` : ''} failed${isString(validationResult) ? ` with message: ${validationResult}` : ''}.`, path, payload: { received: inputValue, validationResult } }); } } };