UNPKG

dynamodb-toolbox

Version:

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

58 lines (57 loc) 2.56 kB
import { DynamoDBToolboxError } from '../../errors/index.js'; import { isArray } from '../../utils/validation/isArray.js'; import { checkSchemaProps } from '../utils/checkSchemaProps.js'; export class TupleSchema { constructor(elements, props) { this.type = 'tuple'; this.elements = elements; this.props = props; } get checked() { return Object.isFrozen(this.props); } check(path) { if (this.checked) { return; } checkSchemaProps(this.props, path); if (!isArray(this.elements)) { throw new DynamoDBToolboxError('schema.tuple.invalidElements', { message: `Invalid tuple elements${path !== undefined ? ` at path '${path}'` : ''}: Tuple elements must be an array.`, path }); } if (this.elements.length === 0) { throw new DynamoDBToolboxError('schema.tuple.missingElements', { message: `Invalid tuple elements${path !== undefined ? ` at path '${path}'` : ''}: Tuple elements must have at least one element.`, path }); } for (const element of this.elements) { const { required, hidden, savedAs } = element.props; if (required !== undefined && required !== 'atLeastOnce' && required !== 'always') { throw new DynamoDBToolboxError('schema.tuple.optionalElements', { message: `Invalid tuple elements${path !== undefined ? ` at path '${path}'` : ''}: Tuple elements must be required.`, path }); } if (hidden !== undefined && hidden !== false) { throw new DynamoDBToolboxError('schema.tuple.hiddenElements', { message: `Invalid tuple elements${path !== undefined ? ` at path '${path}'` : ''}: Tuple elements cannot be hidden.`, path }); } if (savedAs !== undefined) { throw new DynamoDBToolboxError('schema.tuple.savedAsElements', { message: `Invalid tuple elements${path !== undefined ? ` at path '${path}'` : ''}: Tuple elements cannot be renamed (have savedAs prop).`, path }); } } this.elements.forEach((element, index) => { element.check(`${path !== null && path !== void 0 ? path : ''}[${index}]`); }); Object.freeze(this.props); Object.freeze(this.elements); } }