UNPKG

dynamodb-toolbox

Version:

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

43 lines (42 loc) 1.37 kB
import { DynamoDBToolboxError } from '../../../errors/index.js'; import { SchemaAction } from '../../../schema/index.js'; import { itemParser } from './item.js'; import { schemaParser } from './schema.js'; export class Parser extends SchemaAction { start(inputValue, options = {}) { if (this.schema.type === 'item') { return itemParser(this.schema, inputValue, options); } else { return schemaParser(this.schema, inputValue, options); } } parse(inputValue, options = {}) { const parser = this.start(inputValue, options); let done = false; let value; do { const nextProps = parser.next(); done = Boolean(nextProps.done); // TODO: Not cast value = nextProps.value; } while (!done); return value; } reparse(inputValue, options = {}) { return this.parse(inputValue, options); } validate(inputValue, options = {}) { try { this.parse(inputValue, { ...options, fill: false, transform: false }); } catch (error) { if (error instanceof DynamoDBToolboxError && DynamoDBToolboxError.match(error, 'parsing.')) { return false; } throw error; } return true; } } Parser.actionName = 'parse';