UNPKG

@telstra/messaging

Version:
22 lines (19 loc) 581 B
import { AssertionError } from '@telstra/core'; import Ajv from 'ajv'; import { MessagingErrorCode } from '../errors/ErrorCode.js'; const ajv = new Ajv({ allErrors: true, format: false }); export class Validator<T> { constructor(public data: T) {} public schemaInline(ref: any): this { let valid = ajv.validate(ref, this.data); let errors = ajv.errors; if (valid) { return this; } else { throw new AssertionError({ ...MessagingErrorCode.InvalidSchema, message: errors?.map((e) => e.message).join('\n') || '', }); } } }