Start new validation system and zodified types with excc
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Garrett Mills 2022-01-14 01:04:13 -06:00
parent b105a61ca2
commit 5ffb91329e
8 changed files with 110 additions and 2 deletions

View File

@ -45,7 +45,8 @@
"typedoc-plugin-pages-fork": "^0.0.1",
"typedoc-plugin-sourcefile-url": "^1.0.6",
"typescript": "^4.2.3",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"zod": "^3.11.6"
},
"scripts": {
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",

View File

@ -48,6 +48,7 @@ specifiers:
typedoc-plugin-sourcefile-url: ^1.0.6
typescript: ^4.2.3
uuid: ^8.3.2
zod: ^3.11.6
dependencies:
'@atao60/fse-cli': 0.1.6
@ -88,6 +89,7 @@ dependencies:
typedoc-plugin-sourcefile-url: 1.0.6_typedoc@0.20.36
typescript: 4.2.3
uuid: 8.3.2
zod: 3.11.6
devDependencies:
'@types/chai': 4.2.22
@ -3081,3 +3083,7 @@ packages:
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
engines: {node: '>=10'}
dev: true
/zod/3.11.6:
resolution: {integrity: sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==}
dev: false

View File

@ -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'

View File

@ -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
}

View 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
}
}

View 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
}
}

View File

@ -0,0 +1,4 @@
export interface ZodifyRecipient {
__exZodifiedSchemata: number[]
}

View 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
}
}