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:
parent
b105a61ca2
commit
5ffb91329e
@ -45,7 +45,8 @@
|
|||||||
"typedoc-plugin-pages-fork": "^0.0.1",
|
"typedoc-plugin-pages-fork": "^0.0.1",
|
||||||
"typedoc-plugin-sourcefile-url": "^1.0.6",
|
"typedoc-plugin-sourcefile-url": "^1.0.6",
|
||||||
"typescript": "^4.2.3",
|
"typescript": "^4.2.3",
|
||||||
"uuid": "^8.3.2"
|
"uuid": "^8.3.2",
|
||||||
|
"zod": "^3.11.6"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",
|
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",
|
||||||
|
@ -48,6 +48,7 @@ specifiers:
|
|||||||
typedoc-plugin-sourcefile-url: ^1.0.6
|
typedoc-plugin-sourcefile-url: ^1.0.6
|
||||||
typescript: ^4.2.3
|
typescript: ^4.2.3
|
||||||
uuid: ^8.3.2
|
uuid: ^8.3.2
|
||||||
|
zod: ^3.11.6
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
'@atao60/fse-cli': 0.1.6
|
'@atao60/fse-cli': 0.1.6
|
||||||
@ -88,6 +89,7 @@ dependencies:
|
|||||||
typedoc-plugin-sourcefile-url: 1.0.6_typedoc@0.20.36
|
typedoc-plugin-sourcefile-url: 1.0.6_typedoc@0.20.36
|
||||||
typescript: 4.2.3
|
typescript: 4.2.3
|
||||||
uuid: 8.3.2
|
uuid: 8.3.2
|
||||||
|
zod: 3.11.6
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@types/chai': 4.2.22
|
'@types/chai': 4.2.22
|
||||||
@ -3081,3 +3083,7 @@ packages:
|
|||||||
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
||||||
engines: {node: '>=10'}
|
engines: {node: '>=10'}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/zod/3.11.6:
|
||||||
|
resolution: {integrity: sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==}
|
||||||
|
dev: false
|
||||||
|
@ -14,6 +14,11 @@ export * from './lifecycle/Application'
|
|||||||
export * from './lifecycle/AppClass'
|
export * from './lifecycle/AppClass'
|
||||||
export * from './lifecycle/Unit'
|
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/HTTPError'
|
||||||
|
|
||||||
export * from './http/kernel/module/InjectSessionHTTPModule'
|
export * from './http/kernel/module/InjectSessionHTTPModule'
|
||||||
@ -94,6 +99,6 @@ export * from './views/PugViewEngine'
|
|||||||
|
|
||||||
export * from './cli'
|
export * from './cli'
|
||||||
export * from './i18n'
|
export * from './i18n'
|
||||||
export * from './forms'
|
// export * from './forms'
|
||||||
export * from './orm'
|
export * from './orm'
|
||||||
export * from './auth'
|
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
|
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)
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user