@push.rocks/smartguard
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -6,5 +6,5 @@ /** | ||
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.' | ||
}; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx3QkFBd0I7SUFDOUIsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLGtIQUFrSDtDQUNoSSxDQUFBIn0= |
export type TGuardFunction<T> = (dataArg: T) => Promise<boolean>; | ||
export interface IGuardOptions { | ||
name?: string; | ||
failedHint?: string; | ||
} | ||
export declare class Guard<T> { | ||
private guardFunction; | ||
constructor(guardFunctionArg: TGuardFunction<T>); | ||
guardoOptions: IGuardOptions; | ||
constructor(guardFunctionArg: TGuardFunction<T>, optionsArg?: IGuardOptions); | ||
/** | ||
@@ -10,2 +15,3 @@ * executes the guard against a data argument; | ||
exec(dataArg: T): Promise<boolean>; | ||
getFailedHint(dataArg: T): Promise<string>; | ||
} |
import * as plugins from './smartguard.plugins.js'; | ||
export class Guard { | ||
constructor(guardFunctionArg) { | ||
constructor(guardFunctionArg, optionsArg) { | ||
this.guardFunction = guardFunctionArg; | ||
this.guardoOptions = optionsArg; | ||
} | ||
@@ -14,3 +15,12 @@ /** | ||
} | ||
async getFailedHint(dataArg) { | ||
const result = await this.exec(dataArg); | ||
if (!result) { | ||
return this.guardoOptions.failedHint; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
} | ||
//# sourceM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MUIsTUFBTSxNQUFNLEdBQUcsTUFBTSxJQUFJLENBQUMsYUFBYSxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ2pELE9BQU8sTUFBTSxDQUFDO0lBQ2hCLENBQUM7SUFFTSxLQUFLLENBQUMsYUFBYSxDQUFDLE9BQVU7UUFDbkMsTUFBTSxNQUFNLEdBQUcsTUFBTSxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ3hDLElBQUksQ0FBQyxNQUFNLEVBQUUsQ0FBQztZQUNaLE9BQU8sSUFBSSxDQUFDLGFBQWEsQ0FBQyxVQUFVLENBQUM7UUFDdkMsQ0FBQzthQUFNLENBQUM7WUFDTixPQUFPLElBQUksQ0FBQztRQUNkLENBQUM7SUFDSCxDQUFDO0NBQ0YifQ== |
import { Guard } from './smartguard.classes.guard.js'; | ||
export interface IExecOptions { | ||
mode?: 'parallel' | 'serial'; | ||
stopOnFail?: boolean; | ||
} | ||
/** | ||
@@ -13,3 +17,3 @@ * Extended GuardSet that inherits from Guard | ||
*/ | ||
execAllWithData(dataArg: T): Promise<boolean[]>; | ||
execAllWithData(dataArg: T, optionsArg?: IExecOptions): Promise<boolean[]>; | ||
/** | ||
@@ -19,3 +23,3 @@ * checks if all guards pass | ||
*/ | ||
allGuardsPass(dataArg: T): Promise<boolean>; | ||
allGuardsPass(dataArg: T, optionsArg?: IExecOptions): Promise<boolean>; | ||
/** | ||
@@ -26,2 +30,8 @@ * checks if any guard passes | ||
anyGuardsPass(dataArg: T): Promise<boolean>; | ||
/** | ||
* returns the first reason for why something fails | ||
* @param dataArg | ||
* @returns | ||
*/ | ||
getFailedHint(dataArg: T): Promise<string>; | ||
} |
@@ -18,7 +18,18 @@ import * as plugins from './smartguard.plugins.js'; | ||
*/ | ||
async execAllWithData(dataArg) { | ||
async execAllWithData(dataArg, optionsArg = { | ||
mode: 'parallel', | ||
stopOnFail: false | ||
}) { | ||
const resultPromises = []; | ||
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); | ||
} | ||
} | ||
} | ||
@@ -32,4 +43,7 @@ const results = await Promise.all(resultPromises); | ||
*/ | ||
async allGuardsPass(dataArg) { | ||
const results = await this.execAllWithData(dataArg); | ||
async allGuardsPass(dataArg, optionsArg = { | ||
mode: 'parallel', | ||
stopOnFail: false | ||
}) { | ||
const results = await this.execAllWithData(dataArg, optionsArg); | ||
return results.every(result => result); | ||
@@ -42,6 +56,22 @@ } | ||
async anyGuardsPass(dataArg) { | ||
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 | ||
*/ | ||
getFailedHint(dataArg) { | ||
for (const guard of this.guards) { | ||
const failedHint = guard.getFailedHint(dataArg); | ||
if (failedHint) { | ||
return failedHint; | ||
} | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5jbGFzc2VzLmd1YXJkc2V0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRndWFyZC5jbGFzc2VzLmd1YXJkc2V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0seUJBQXlCLENBQUM7QUFDbkQsT0FBTyxFQUFFLEtBQUssRUFBdUIsTUFBTSwrQkFBK0IsQ0FBQztBQUUzRTs7O0dBR0c7QUFDSCxNQUFNLE9BQU8sUUFBWSxTQUFRLEtBQVE7SUFHdkMsWUFBWSxhQUE4QixFQUFFO1FBQzFDL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T0FBTyxPQUFPLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsTUFBTSxDQUFDLENBQUM7SUFDeEMsQ0FBQztJQUVEOzs7O09BSUc7SUFDSSxhQUFhLENBQUUsT0FBVTtRQUM5QixLQUFLLE1BQU0sS0FBSyxJQUFJLElBQUksQ0FBQyxNQUFNLEVBQUUsQ0FBQztZQUNoQyxNQUFNLFVBQVUsR0FBRyxLQUFLLENBQUMsYUFBYSxDQUFDLE9BQU8sQ0FBQyxDQUFDO1lBQ2hELElBQUksVUFBVSxFQUFFLENBQUM7Z0JBQ2YsT0FBTyxVQUFVLENBQUM7WUFDcEIsQ0FBQztRQUNILENBQUM7SUFDSCxDQUFDO0NBQ0YifQ== |
@@ -13,5 +13,5 @@ { | ||
"keywords": [ | ||
"typescript", | ||
"validation", | ||
"guards", | ||
"typescript", | ||
"async", | ||
@@ -29,3 +29,5 @@ "nodejs", | ||
"server-side validation", | ||
"backend validation" | ||
"backend validation", | ||
"smartrequest", | ||
"typedserver" | ||
] | ||
@@ -32,0 +34,0 @@ } |
{ | ||
"name": "@push.rocks/smartguard", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"private": false, | ||
@@ -39,5 +39,5 @@ "description": "A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.", | ||
"keywords": [ | ||
"typescript", | ||
"validation", | ||
"guards", | ||
"typescript", | ||
"async", | ||
@@ -55,3 +55,5 @@ "nodejs", | ||
"server-side validation", | ||
"backend validation" | ||
"backend validation", | ||
"smartrequest", | ||
"typedserver" | ||
], | ||
@@ -58,0 +60,0 @@ "homepage": "https://code.foss.global/push.rocks/smartguard", |
@@ -58,6 +58,6 @@ # @push.rocks/smartguard | ||
To execute a guard or a set of guards against data, you use the `executeGuardWithData` method for a single guard, or `executeGuardsWithData` method for a `GuardSet`. | ||
To execute a guard or a set of guards against data, you use the `execGuardWithData` method for a single guard, or `execGuardsWithData` method for a `GuardSet`. | ||
```typescript | ||
const isValidString = await isStringGuard.executeGuardWithData('Hello World!'); | ||
const isValidString = await isStringGuard.execGuardWithData('Hello World!'); | ||
console.log(isValidString); // true | ||
@@ -88,3 +88,3 @@ | ||
```typescript | ||
import * as express from 'express'; | ||
import express from 'express'; | ||
import { Guard } from '@push.rocks/smartguard'; | ||
@@ -99,3 +99,3 @@ | ||
app.use(async (req, res, next) => { | ||
const isAuthorized = await isAuthorizedUserGuard.executeGuardWithData(req); | ||
const isAuthorized = await isAuthorizedUserGuard.execGuardWithData(req); | ||
if (!isAuthorized) { | ||
@@ -151,3 +151,3 @@ res.status(403).send('Unauthorized'); | ||
const isValidResponse = await validApiResponseGuard.executeGuardWithData('https://example.com/api/data'); | ||
const isValidResponse = await validApiResponseGuard.execGuardWithData('https://example.com/api/data'); | ||
console.log(isValidResponse); // true if the API response status is 200 | ||
@@ -179,5 +179,5 @@ ``` | ||
const registrationValidationSet = new GuardSet<{ username: string, email: string, password: string }>([ | ||
new Guard(async (data) => isUsernameValid.executeGuardWithData(data.username)), | ||
new Guard(async (data) => isEmailValid.executeGuardWithData(data.email)), | ||
new Guard(async (data) => isPasswordStrong.executeGuardWithData(data.password)) | ||
new Guard(async (data) => isUsernameValid.execGuardWithData(data.username)), | ||
new Guard(async (data) => isEmailValid.execGuardWithData(data.email)), | ||
new Guard(async (data) => isPasswordStrong.execGuardWithData(data.password)) | ||
]); | ||
@@ -226,5 +226,5 @@ | ||
const isAddressValid = new Guard<UserProfile['address']>(async (address) => { | ||
const streetValid = await isStreetValid.executeGuardWithData(address.street); | ||
const cityValid = await isCityValid.executeGuardWithData(address.city); | ||
const postalCodeValid = await isPostalCodeValid.executeGuardWithData(address.postalCode); | ||
const streetValid = await isStreetValid.execGuardWithData(address.street); | ||
const cityValid = await isCityValid.execGuardWithData(address.city); | ||
const postalCodeValid = await isPostalCodeValid.execGuardWithData(address.postalCode); | ||
return streetValid && cityValid && postalCodeValid; | ||
@@ -243,5 +243,5 @@ }); | ||
const userProfileValidationSet = new GuardSet<UserProfile>([ | ||
new Guard(async (data) => isUsernameValid.executeGuardWithData(data.username)), | ||
new Guard(async (data) => isEmailValid.executeGuardWithData(data.email)), | ||
new Guard(async (data) => isAddressValid.executeGuardWithData(data.address)) | ||
new Guard(async (data) => isUsernameValid.execGuardWithData(data.username)), | ||
new Guard(async (data) => isEmailValid.execGuardWithData(data.email)), | ||
new Guard(async (data) => isAddressValid.execGuardWithData(data.address)) | ||
]); | ||
@@ -306,3 +306,3 @@ | ||
const isNonEmptyStringGuard = new Guard<string>(async (data) => { | ||
return await isStringGuard.executeGuardWithData(data) && data.trim().length > 0; | ||
return await isStringGuard.execGuardWithData(data) && data.trim().length > 0; | ||
}); | ||
@@ -320,10 +320,10 @@ | ||
const isAuthorGuardSet = new GuardSet<BlogPost['author']>([ | ||
new Guard(async (data) => await isNonEmptyStringGuard.executeGuardWithData(data.name)), | ||
new Guard(async (data) => await isEmailGuard.executeGuardWithData(data.email)) | ||
new Guard(async (data) => await isNonEmptyStringGuard.execGuardWithData(data.name)), | ||
new Guard(async (data) => await isEmailGuard.execGuardWithData(data.email)) | ||
]); | ||
const isBlogPostGuardSet = new GuardSet<BlogPost>([ | ||
new Guard(async (data) => await isNonEmptyStringGuard.executeGuardWithData(data.title)), | ||
new Guard(async (data) => await isNonEmptyStringGuard.executeGuardWithData(data.content)), | ||
new Guard(async (data) => await isStringArrayGuard.executeGuardWithData(data.tags)), | ||
new Guard(async (data) => await isNonEmptyStringGuard.execGuardWithData(data.title)), | ||
new Guard(async (data) => await isNonEmptyStringGuard.execGuardWithData(data.content)), | ||
new Guard(async (data) => await isStringArrayGuard.execGuardWithData(data.tags)), | ||
new Guard(async (data) => await isAuthorGuardSet.executeGuardsWithData(data.author).then(results => results.every(result => result))) | ||
@@ -362,3 +362,3 @@ ]); | ||
const apiKey = 'some-api-key'; | ||
const isApiKeyValid = await isApiKeyValidGuard.executeGuardWithData(apiKey); | ||
const isApiKeyValid = await isApiKeyValidGuard.execGuardWithData(apiKey); | ||
console.log(isApiKeyValid); // true if the API key is valid | ||
@@ -412,3 +412,3 @@ ``` | ||
const isLongEnough = await minLengthGuard.executeGuardWithData('Hello, world!'); | ||
const isLongEnough = await minLengthGuard.execGuardWithData('Hello, world!'); | ||
console.log(isLongEnough); // true because the length of 'Hello, world!' is more than 10 | ||
@@ -419,8 +419,21 @@ ``` | ||
### Conclusion | ||
## License and Legal Information | ||
`@push.rocks/smartguard` provides a powerful framework for creating and managing validation guards in JavaScript and TypeScript applications. The library's flexibility allows it to handle simple boolean checks, asynchronous operations, integration with external APIs, and complex composite validations. Its use of `Guard` and `GuardSet` classes ensures that validations are both modular and reusable. | ||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. | ||
Whether you are validating form inputs, securing APIs, or ensuring data integrity in your backend services, `@push.rocks/smartguard` simplifies the process and makes your code cleaner and more maintainable. | ||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. | ||
### Trademarks | ||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. | ||
### Company Information | ||
Task Venture Capital GmbH | ||
Registered at District court Bremen HRB 35230 HB, Germany | ||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. | ||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works. | ||
## License and Legal Information | ||
@@ -427,0 +440,0 @@ |
@@ -6,4 +6,4 @@ /** | ||
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.' | ||
} |
@@ -5,6 +5,13 @@ import * as plugins from './smartguard.plugins.js'; | ||
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; | ||
} | ||
@@ -20,2 +27,11 @@ | ||
} | ||
public async getFailedHint(dataArg: T) { | ||
const result = await this.exec(dataArg); | ||
if (!result) { | ||
return this.guardoOptions.failedHint; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
import * as plugins from './smartguard.plugins.js'; | ||
import { Guard, type TGuardFunction } from './smartguard.classes.guard.js'; | ||
export interface IExecOptions { | ||
mode?: 'parallel' | 'serial'; | ||
stopOnFail?: boolean; | ||
} | ||
/** | ||
@@ -22,3 +27,6 @@ * Extended GuardSet that inherits from Guard | ||
*/ | ||
public async execAllWithData(dataArg: T) { | ||
public async execAllWithData(dataArg: T, optionsArg: IExecOptions = { | ||
mode: 'parallel', | ||
stopOnFail: false | ||
}): Promise<boolean[]> { | ||
const resultPromises: Array<Promise<boolean>> = []; | ||
@@ -28,3 +36,11 @@ | ||
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); | ||
} | ||
} | ||
} | ||
@@ -40,4 +56,7 @@ | ||
*/ | ||
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); | ||
@@ -51,5 +70,22 @@ } | ||
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; | ||
} | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38970
379
445