UNPKG

@bitblit/ratchet-epsilon-common

Version:

Tiny adapter to simplify building API gateway Lambda APIS

87 lines 3.06 kB
import { Logger } from '@bitblit/ratchet-common/logger/logger'; import { ErrorRatchet } from '@bitblit/ratchet-common/lang/error-ratchet'; import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet'; export class BackgroundValidator { cfg; modelValidator; constructor(cfg, modelValidator) { this.cfg = cfg; this.modelValidator = modelValidator; } findProcessor(typeName) { const rval = this.cfg.processors.find((s) => s.typeName === typeName); return rval; } validType(type) { return !!this.findProcessor(type); } validateEntry(entry) { const rval = []; if (!entry) { rval.push('Entry is null'); } else if (!StringRatchet.trimToNull(entry.type)) { rval.push('Entry type is null or empty'); const proc = this.findProcessor(entry.type); if (!proc) { rval.push('Entry type is invalid'); } } return rval; } validateEntryAndThrowException(entry) { const errors = this.validateEntry(entry); if (errors.length > 0) { Logger.warn('Invalid entry %j : errors : %j', entry, errors); ErrorRatchet.throwFormattedErr('Invalid entry %j : errors : %j', entry, errors); } } static validateAndMapProcessors(processorInput, _modelValidator) { const rval = new Map(); processorInput.forEach((p, idx) => { if (!p) { ErrorRatchet.throwFormattedErr('Null processor provided at index %d', idx); } if (!StringRatchet.trimToNull(p.typeName)) { ErrorRatchet.throwFormattedErr('Processor at index %d defines no name', idx); } if (rval.has(p.typeName)) { ErrorRatchet.throwFormattedErr('More than one processor defined for type %s', p.typeName); } rval.set(p.typeName, p); }); return rval; } static validateAwsConfig(cfg) { const rval = []; if (!cfg) { rval.push('Null config'); } else { if (!cfg.notificationArn) { rval.push('AWS config missing notificationArn'); } if (!cfg.queueUrl) { rval.push('AWS config missing queueUrl'); } if ((cfg.sendNotificationOnBackgroundError || cfg.sendNotificationOnBackgroundValidationFailure) && !cfg.backgroundProcessFailureSnsArn) { rval.push('At least one send notification flag set to true but no sns arn set'); } } return rval; } static validateConfig(cfg) { const rval = []; if (!cfg) { rval.push('Null config'); } else { if (!cfg.processors || cfg.processors.length === 0) { rval.push('No processes specified'); } } return rval; } } //# sourceMappingURL=background-validator.js.map