import { z } from 'zod' import {InjectionAware} from '../di' import {ZodifyRecipient} from './ZodifyRecipient' import {ZodifyRegistrar} from './ZodifyRegistrar' export class InvalidSchemaMappingError extends Error { constructor(message = 'Unable to resolve schema for validator.') { super(message) } } export class Validator extends InjectionAware implements ZodifyRecipient { __exZodifiedSchemata: number[] = [] protected getZod(): z.ZodType { // eslint-disable-next-line no-underscore-dangle if ( this.__exZodifiedSchemata.length < 1 ) { throw new InvalidSchemaMappingError() } // eslint-disable-next-line no-underscore-dangle const id = this.__exZodifiedSchemata[0] const type = this.make(ZodifyRegistrar).get(id) if ( !type ) { throw new InvalidSchemaMappingError() } return type } }