feat(core): Added GuardError
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartguard',
|
||||
version: '3.0.2',
|
||||
version: '3.1.0',
|
||||
description: 'A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.'
|
||||
}
|
||||
|
@@ -9,10 +9,10 @@ export interface IGuardOptions {
|
||||
|
||||
export class Guard<T> {
|
||||
private guardFunction: TGuardFunction<T>;
|
||||
public guardoOptions: IGuardOptions;
|
||||
public options: IGuardOptions;
|
||||
constructor(guardFunctionArg: TGuardFunction<T>, optionsArg?: IGuardOptions) {
|
||||
this.guardFunction = guardFunctionArg;
|
||||
this.guardoOptions = optionsArg;
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +27,7 @@ export class Guard<T> {
|
||||
public async getFailedHint(dataArg: T) {
|
||||
const result = await this.exec(dataArg);
|
||||
if (!result) {
|
||||
return this.guardoOptions.failedHint;
|
||||
return this.options.failedHint;
|
||||
} else {
|
||||
return null;
|
||||
}
|
8
ts/classes.guarderror.ts
Normal file
8
ts/classes.guarderror.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import * as plugins from './smartguard.plugins.js';
|
||||
|
||||
export class GuardError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'GuardError';
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
import * as plugins from './smartguard.plugins.js';
|
||||
import { Guard, type TGuardFunction } from './smartguard.classes.guard.js';
|
||||
import { Guard, type TGuardFunction } from './classes.guard.js';
|
||||
|
||||
export interface IExecOptions {
|
||||
mode?: 'parallel' | 'serial';
|
15
ts/index.ts
15
ts/index.ts
@@ -1,14 +1,19 @@
|
||||
import * as plugins from './smartguard.plugins.js';
|
||||
import { Guard } from './smartguard.classes.guard.js';
|
||||
import { GuardSet } from './smartguard.classes.guardset.js';
|
||||
export * from './smartguard.classes.guard.js';
|
||||
export * from './smartguard.classes.guardset.js';
|
||||
import { Guard } from './classes.guard.js';
|
||||
export * from './classes.guarderror.js';
|
||||
export * from './classes.guard.js';
|
||||
export * from './classes.guardset.js';
|
||||
|
||||
import { GuardSet } from './classes.guardset.js';
|
||||
import { GuardError } from './classes.guarderror.js';
|
||||
export const passGuardsOrReject = async <T>(dataArg: T, guards: Array<Guard<T>>) => {
|
||||
const guardSet = new GuardSet<T>(guards);
|
||||
const result = await guardSet.allGuardsPass(dataArg);
|
||||
if (!result) {
|
||||
throw new Error('Guard failed');
|
||||
const failedHint = await guardSet.getFailedHint(dataArg);
|
||||
throw new GuardError(`Guards failed:
|
||||
${failedHint}
|
||||
`);
|
||||
}
|
||||
return ;
|
||||
};
|
||||
|
Reference in New Issue
Block a user