fix(core): update
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartguard',
|
||||
version: '3.0.1',
|
||||
version: '3.0.2',
|
||||
description: 'A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.'
|
||||
}
|
||||
|
@ -2,10 +2,17 @@ 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>;
|
||||
constructor(guardFunctionArg: TGuardFunction<T>) {
|
||||
public guardoOptions: IGuardOptions;
|
||||
constructor(guardFunctionArg: TGuardFunction<T>, optionsArg?: IGuardOptions) {
|
||||
this.guardFunction = guardFunctionArg;
|
||||
this.guardoOptions = optionsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -16,4 +23,13 @@ export class Guard<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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
import * as plugins from './smartguard.plugins.js';
|
||||
import { Guard, type TGuardFunction } from './smartguard.classes.guard.js';
|
||||
|
||||
export interface IExecOptions {
|
||||
mode?: 'parallel' | 'serial';
|
||||
stopOnFail?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended GuardSet that inherits from Guard
|
||||
* and provides additional functionalities.
|
||||
@ -19,12 +24,23 @@ export class GuardSet<T> extends Guard<T> {
|
||||
* executes all guards in all guardSets against a data argument
|
||||
* @param dataArg
|
||||
*/
|
||||
public async execAllWithData(dataArg: T) {
|
||||
public async execAllWithData(dataArg: T, optionsArg: IExecOptions = {
|
||||
mode: 'parallel',
|
||||
stopOnFail: false
|
||||
}): Promise<boolean[]> {
|
||||
const resultPromises: Array<Promise<boolean>> = [];
|
||||
|
||||
for (const guard of this.guards) {
|
||||
const guardResultPromise = guard.exec(dataArg);
|
||||
if (optionsArg.mode === 'serial') {
|
||||
await guardResultPromise;
|
||||
}
|
||||
resultPromises.push(guardResultPromise);
|
||||
if (optionsArg.stopOnFail) {
|
||||
if (!await guardResultPromise) {
|
||||
return await Promise.all(resultPromises);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const results = await Promise.all(resultPromises);
|
||||
@ -35,8 +51,11 @@ export class GuardSet<T> extends Guard<T> {
|
||||
* checks if all guards pass
|
||||
* @param dataArg
|
||||
*/
|
||||
public async allGuardsPass(dataArg: T): Promise<boolean> {
|
||||
const results = await this.execAllWithData(dataArg);
|
||||
public async allGuardsPass(dataArg: T, optionsArg: IExecOptions = {
|
||||
mode: 'parallel',
|
||||
stopOnFail: false
|
||||
}): Promise<boolean> {
|
||||
const results = await this.execAllWithData(dataArg, optionsArg);
|
||||
return results.every(result => result);
|
||||
}
|
||||
|
||||
@ -45,7 +64,24 @@ export class GuardSet<T> extends Guard<T> {
|
||||
* @param dataArg
|
||||
*/
|
||||
public async anyGuardsPass(dataArg: T): Promise<boolean> {
|
||||
const results = await this.execAllWithData(dataArg);
|
||||
const results = await this.execAllWithData(dataArg, {
|
||||
mode: 'parallel',
|
||||
stopOnFail: false
|
||||
});
|
||||
return results.some(result => result);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the first reason for why something fails
|
||||
* @param dataArg
|
||||
* @returns
|
||||
*/
|
||||
public getFailedHint (dataArg: T): Promise<string> {
|
||||
for (const guard of this.guards) {
|
||||
const failedHint = guard.getFailedHint(dataArg);
|
||||
if (failedHint) {
|
||||
return failedHint;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user