dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
41 lines (40 loc) • 1.28 kB
JavaScript
import { DynamoDBToolboxError } from '../../../errors/index.js';
import { SchemaAction } from '../../../schema/index.js';
import { itemFormatter } from './item.js';
import { schemaFormatter } from './schema.js';
export class Formatter extends SchemaAction {
start(inputValue, options = {}) {
if (this.schema.type === 'item') {
return itemFormatter(this.schema, inputValue, options);
}
else {
return 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 DynamoDBToolboxError &&
DynamoDBToolboxError.match(error, 'formatter.')) {
return false;
}
throw error;
}
return true;
}
}
Formatter.actionName = 'format';