fix(core): update
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartguard',
|
||||
version: '2.0.2',
|
||||
version: '2.0.3',
|
||||
description: 'A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.'
|
||||
}
|
||||
|
15
ts/index.ts
15
ts/index.ts
@ -3,16 +3,11 @@ import { Guard } from './smartguard.classes.guard.js';
|
||||
import { GuardSet } from './smartguard.classes.guardset.js';
|
||||
export * from './smartguard.classes.guard.js';
|
||||
|
||||
export const passGuards = async <T>(dataArg: T, guards: Array<Guard<T>>) => {
|
||||
const done = plugins.smartpromise.defer();
|
||||
export const passGuardsOrReject = async <T>(dataArg: T, guards: Array<Guard<T>>) => {
|
||||
const guardSet = new GuardSet<T>(guards);
|
||||
const results = await guardSet.executeGuardsWithData(dataArg);
|
||||
for (const result of results) {
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
const result = await guardSet.allGuardsPass(dataArg);
|
||||
if (!result) {
|
||||
throw new Error('Guard failed');
|
||||
}
|
||||
done.resolve();
|
||||
await done.promise;
|
||||
return;
|
||||
return ;
|
||||
};
|
||||
|
@ -1,23 +1,51 @@
|
||||
import * as plugins from './smartguard.plugins.js';
|
||||
import { Guard } from './smartguard.classes.guard.js';
|
||||
import { Guard, type TGuardFunction } from './smartguard.classes.guard.js';
|
||||
|
||||
/**
|
||||
* a guardSet is a set of guards that need to be fulfilled
|
||||
* Extended GuardSet that inherits from Guard
|
||||
* and provides additional functionalities.
|
||||
*/
|
||||
export class GuardSet<T> {
|
||||
export class GuardSet<T> extends Guard<T> {
|
||||
public guards: Array<Guard<T>>;
|
||||
public passed: boolean;
|
||||
constructor(guardsArrayArg: Array<Guard<T>>) {
|
||||
this.guards = guardsArrayArg;
|
||||
|
||||
constructor(guardArray: Array<Guard<T>> = []) {
|
||||
super(async (dataArg: T) => {
|
||||
return this.allGuardsPass(dataArg);
|
||||
})
|
||||
this.guards = guardArray;
|
||||
}
|
||||
|
||||
public async executeGuardsWithData(dataArg: T) {
|
||||
/**
|
||||
* executes all guards in all guardSets against a data argument
|
||||
* @param dataArg
|
||||
*/
|
||||
public async executeAllGuardsWithData(dataArg: T) {
|
||||
const resultPromises: Array<Promise<boolean>> = [];
|
||||
|
||||
for (const guard of this.guards) {
|
||||
const resultPromise = guard.executeGuardWithData(dataArg);
|
||||
resultPromises.push(resultPromise);
|
||||
const guardResultPromise = guard.executeGuardWithData(dataArg);
|
||||
resultPromises.push(guardResultPromise);
|
||||
}
|
||||
const results = Promise.all(resultPromises);
|
||||
|
||||
const results = await Promise.all(resultPromises);
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if all guards pass
|
||||
* @param dataArg
|
||||
*/
|
||||
public async allGuardsPass(dataArg: T): Promise<boolean> {
|
||||
const results = await this.executeAllGuardsWithData(dataArg);
|
||||
return results.every(result => result);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if any guard passes
|
||||
* @param dataArg
|
||||
*/
|
||||
public async anyGuardsPass(dataArg: T): Promise<boolean> {
|
||||
const results = await this.executeAllGuardsWithData(dataArg);
|
||||
return results.some(result => result);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user