UNPKG

@telstra/iot-connectivity-manager

Version:
22 lines (19 loc) 577 B
import { AssertionError } from '@telstra/core'; import Ajv from 'ajv'; import { ServiceErrorCode } 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({ ...ServiceErrorCode.InvalidSchema, message: errors?.map((e) => e.message).join('\n') || '', }); } } }