Start new validation system and zodified types with excc
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
b105a61ca2
commit
5ffb91329e
@ -0,0 +1,23 @@
|
|||||||
|
import { z } from 'zod'
|
||||||
|
import {Canonical, CanonicalDefinition} from '../service/Canonical'
|
||||||
|
import {InvalidCanonicalExportError} from '../service/CanonicalInstantiable'
|
||||||
|
import {Singleton} from '../di'
|
||||||
|
|
||||||
|
@Singleton()
|
||||||
|
export class ValidationUnit extends Canonical<z.ZodType<unknown>> {
|
||||||
|
protected appPath = ['types']
|
||||||
|
|
||||||
|
protected canonicalItem = 'type'
|
||||||
|
|
||||||
|
protected suffix = '.js'
|
||||||
|
|
||||||
|
public async initCanonicalItem(definition: CanonicalDefinition): Promise<z.Schema<unknown>> {
|
||||||
|
if ( !(definition.imported?.exZodifiedSchema) ) {
|
||||||
|
this.logging.debug('Type export is invalid: no exZodifiedSchema found')
|
||||||
|
this.logging.debug(definition.imported)
|
||||||
|
throw new InvalidCanonicalExportError(definition.originalName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return definition.imported.exZodifiedSchema
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
export interface ZodifyRecipient {
|
||||||
|
__exZodifiedSchemata: number[]
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue