UNPKG

@dvsa/smc-encounter-schema

Version:

Schemas required for Encounter submission as well as custom validators ## Developing ### Pre-requisites

36 lines (29 loc) 1.18 kB
import 'reflect-metadata'; import { plainToInstance } from 'class-transformer'; import { validate as validateEncounter } from 'class-validator'; import type { ValidatorOptions } from 'class-validator/types/validation/ValidatorOptions'; import { EncounterReq } from './models/request/EncounterReq'; export abstract class ValidatorBase { private static _encounterReq: Readonly<Partial<EncounterReq>>; protected constructor(encounterReq: Readonly<Partial<EncounterReq>>) { ValidatorBase._encounterReq = encounterReq; } static get encounterReq(): Readonly<Partial<EncounterReq>> { return ValidatorBase._encounterReq; } } export class EncounterSchema extends ValidatorBase { private static readonly _defaultValidatorOptions: ValidatorOptions = { validationError: { target: false, value: false }, }; constructor( encounterReq: Partial<EncounterReq>, private validationOptions: ValidatorOptions = EncounterSchema._defaultValidatorOptions ) { super(encounterReq); } async validate() { const encounterReqInstance = plainToInstance(EncounterReq, ValidatorBase.encounterReq); return await validateEncounter(encounterReqInstance, this.validationOptions); } }