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.
lib/src/validation/ZodifyRegistrar.ts

32 lines
851 B

import { z } from 'zod'
import {Container, Singleton} from '../di'
import {Collection, Maybe} from '../util'
type ZodifyRegistrant = { id: number, schema: z.ZodType<any> }
export function registerZodifiedSchema<T>(id: number, schema: z.ZodType<T>): z.ZodType<T> {
Container.getContainer()
.make<ZodifyRegistrar>(ZodifyRegistrar)
.register(id, schema)
return schema
}
@Singleton()
export class ZodifyRegistrar {
protected registeredSchemata: Collection<ZodifyRegistrant> = new Collection<ZodifyRegistrant>()
public register(id: number, schema: z.ZodType<any>): this {
this.registeredSchemata.push({
id,
schema,
})
return this
}
public get(id: number): Maybe<z.ZodType<any>> {
return this.registeredSchemata.firstWhere('id', '=', id)?.schema
}
}