smartguard/ts/smartguard.classes.guard.ts
2024-05-30 22:09:37 +02:00

36 lines
857 B
TypeScript

import * as plugins from './smartguard.plugins.js';
export type TGuardFunction<T> = (dataArg: T) => Promise<boolean>;
export interface IGuardOptions {
name?: string;
failedHint?: string;
}
export class Guard<T> {
private guardFunction: TGuardFunction<T>;
public guardoOptions: IGuardOptions;
constructor(guardFunctionArg: TGuardFunction<T>, optionsArg?: IGuardOptions) {
this.guardFunction = guardFunctionArg;
this.guardoOptions = optionsArg;
}
/**
* executes the guard against a data argument;
* @param dataArg
*/
public async exec(dataArg: T) {
const result = await this.guardFunction(dataArg);
return result;
}
public async getFailedHint(dataArg: T) {
const result = await this.exec(dataArg);
if (!result) {
return this.guardoOptions.failedHint;
} else {
return null;
}
}
}