You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
941 B
32 lines
941 B
2 years ago
|
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<T> extends InjectionAware implements ZodifyRecipient {
|
||
|
__exZodifiedSchemata: number[] = []
|
||
|
|
||
|
protected getZod(): z.ZodType<T> {
|
||
|
// 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>(ZodifyRegistrar).get(id)
|
||
|
if ( !type ) {
|
||
|
throw new InvalidSchemaMappingError()
|
||
|
}
|
||
|
|
||
|
return type
|
||
|
}
|
||
|
}
|