UNPKG

dynamodb-toolbox

Version:

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

45 lines (44 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Formatter = void 0; const index_js_1 = require("../../../errors/index.js"); const index_js_2 = require("../../../schema/index.js"); const item_js_1 = require("./item.js"); const schema_js_1 = require("./schema.js"); class Formatter extends index_js_2.SchemaAction { start(inputValue, options = {}) { if (this.schema.type === 'item') { return (0, item_js_1.itemFormatter)(this.schema, inputValue, options); } else { return (0, schema_js_1.schemaFormatter)(this.schema, inputValue, options); } } format(inputValue, options = {}) { const formatter = this.start(inputValue, options); let done = false; let value; do { const nextProps = formatter.next(); done = Boolean(nextProps.done); // TODO: Not cast value = nextProps.value; } while (!done); return value; } validate(inputValue) { try { this.format(inputValue, { format: false }); } catch (error) { if (error instanceof index_js_1.DynamoDBToolboxError && index_js_1.DynamoDBToolboxError.match(error, 'formatter.')) { return false; } throw error; } return true; } } exports.Formatter = Formatter; Formatter.actionName = 'format';