Start routing and pipeline rewrite
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:
@@ -1,17 +1,92 @@
|
||||
import { z } from 'zod'
|
||||
import {InjectionAware} from '../di'
|
||||
import {ErrorWithContext, TypeTag} from '../util'
|
||||
import {ZodifyRecipient} from './ZodifyRecipient'
|
||||
import {ZodifyRegistrar} from './ZodifyRegistrar'
|
||||
import {Logging} from '../service/Logging'
|
||||
|
||||
/** Type tag for a validated runtime type. */
|
||||
export type Valid<T> = TypeTag<'@extollo/lib:Valid'> & T
|
||||
|
||||
/**
|
||||
* Error thrown if the schema for a validator cannot be located.
|
||||
*/
|
||||
export class InvalidSchemaMappingError extends Error {
|
||||
constructor(message = 'Unable to resolve schema for validator.') {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface defining validation error messages.
|
||||
*/
|
||||
export interface ValidationFormErrors<T> {
|
||||
formErrors: string[],
|
||||
fieldErrors: {
|
||||
[k in keyof T]: string[]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Error thrown when data validation has failed.
|
||||
*/
|
||||
export class ValidationError<T> extends ErrorWithContext {
|
||||
constructor(
|
||||
public readonly formErrors: ValidationFormErrors<T>,
|
||||
message = 'Invalid form data',
|
||||
) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates input data against a schema at runtime.
|
||||
*/
|
||||
export class Validator<T> extends InjectionAware implements ZodifyRecipient {
|
||||
__exZodifiedSchemata: number[] = []
|
||||
|
||||
/**
|
||||
* Parse the input data against the schema.
|
||||
* @throws ValidationError
|
||||
* @param from
|
||||
*/
|
||||
public parse(from: unknown): Valid<T> {
|
||||
try {
|
||||
return this.getZod().parse(from) as Valid<T>
|
||||
} catch (e: unknown) {
|
||||
if ( e instanceof z.ZodError ) {
|
||||
throw new ValidationError<T>(e.formErrors as ValidationFormErrors<T>)
|
||||
}
|
||||
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Typeguard for the input schema.
|
||||
* @param what
|
||||
*/
|
||||
public is(what: unknown): what is Valid<T> {
|
||||
try {
|
||||
this.parse(what)
|
||||
return true
|
||||
} catch (e: unknown) {
|
||||
this.log().verbose(`Error during validation: ${e}`)
|
||||
|
||||
if ( e instanceof ValidationError ) {
|
||||
return false
|
||||
}
|
||||
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the logging service. */
|
||||
protected log(): Logging {
|
||||
return this.make<Logging>(Logging)
|
||||
}
|
||||
|
||||
/** Get the Zod schema. */
|
||||
protected getZod(): z.ZodType<T> {
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
if ( this.__exZodifiedSchemata.length < 1 ) {
|
||||
|
||||
Reference in New Issue
Block a user