Start new validation system and zodified types with excc
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -14,6 +14,11 @@ export * from './lifecycle/Application'
|
||||
export * from './lifecycle/AppClass'
|
||||
export * from './lifecycle/Unit'
|
||||
|
||||
export * from './validation/ZodifyRecipient'
|
||||
export * from './validation/ZodifyRegistrar'
|
||||
export * from './validation/Validator'
|
||||
export * from './validation/ValidationUnit'
|
||||
|
||||
export * from './http/HTTPError'
|
||||
|
||||
export * from './http/kernel/module/InjectSessionHTTPModule'
|
||||
@@ -94,6 +99,6 @@ export * from './views/PugViewEngine'
|
||||
|
||||
export * from './cli'
|
||||
export * from './i18n'
|
||||
export * from './forms'
|
||||
// export * from './forms'
|
||||
export * from './orm'
|
||||
export * from './auth'
|
||||
|
||||
@@ -23,3 +23,10 @@ export function isKeyof<T>(key: unknown, obj: T): key is keyof T {
|
||||
export function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> { // eslint-disable-line @typescript-eslint/ban-types
|
||||
return Object.hasOwnProperty.call(obj, prop)
|
||||
}
|
||||
|
||||
/**
|
||||
* TypeScript helper for creating tagged-types.
|
||||
*/
|
||||
export interface TypeTag<S extends string> {
|
||||
readonly __typeTag: S
|
||||
}
|
||||
|
||||
23
src/validation/ValidationUnit.ts
Normal file
23
src/validation/ValidationUnit.ts
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
31
src/validation/Validator.ts
Normal file
31
src/validation/Validator.ts
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
4
src/validation/ZodifyRecipient.ts
Normal file
4
src/validation/ZodifyRecipient.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
export interface ZodifyRecipient {
|
||||
__exZodifiedSchemata: number[]
|
||||
}
|
||||
31
src/validation/ZodifyRegistrar.ts
Normal file
31
src/validation/ZodifyRegistrar.ts
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user