@push.rocks/smartguard
Advanced tools
Comparing version 2.0.1 to 2.0.2
import { Guard } from './smartguard.classes.guard.js'; | ||
export * from './smartguard.classes.guard.js'; | ||
export declare const passGuards: <T>(dataArg: T, guards: Guard<T>[]) => Promise<void>; | ||
export declare const passGuards: <T>(dataArg: T, guards: Array<Guard<T>>) => Promise<void>; |
import * as plugins from './smartguard.plugins.js'; | ||
import './smartguard.classes.guard.js'; | ||
import { Guard } from './smartguard.classes.guard.js'; | ||
import { GuardSet } from './smartguard.classes.guardset.js'; | ||
@@ -18,2 +18,2 @@ export * from './smartguard.classes.guard.js'; | ||
}; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLHlCQUF5QixDQUFDO0FBQ25ELE9BQXNCLCtCQUErQixDQUFDO0FBQ3RELE9BQU8sRUFBRSxRQUFRLEVBQUUsTUFBTSxrQ0FBa0MsQ0FBQztBQUM1RCxjQUFjLCtCQUErQixDQUFDO0FBRTlDLE1BQU0sQ0FBQyxNQUFNLFVBQVUsR0FBRyxLQUFLLEVBQUssT0FBVSxFQUFFLE1BQXVCLEVBQUUsRUFBRTtJQUN6RSxNQUFNLElBQUksR0FBRyxPQUFPLENBQUMsWUFBWSxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzFDLE1BQU0sUUFBUSxHQUFHLElBQUksUUFBUSxDQUFJLE1BQU0sQ0FBQyxDQUFDO0lBQ3pDLE1BQU0sT0FBTyxHQUFHLE1BQU0sUUFBUSxDQUFDLHFCQUFxQixDQUFDLE9BQU8sQ0FBQyxDQUFDO0lBQzlELEtBQUssTUFBTSxNQUFNLElBQUksT0FBTyxFQUFFO1FBQzVCLElBQUksQ0FBQyxNQUFNLEVBQUU7WUFDWCxPQU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QyxDQUFDIn0= |
@@ -1,2 +0,2 @@ | ||
import './smartguard.plugins.js'; | ||
import * as plugins from './smartguard.plugins.js'; | ||
/** | ||
@@ -7,2 +7,2 @@ * a block handler is used | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5jbGFzc2VzLmJsb2NraGFuZGxlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Z3VhcmQuY2xhc3Nlcy5ibG9ja2hhbmRsZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBeUIseUJBQXlCLENBQUM7QUFFbkQ7O0dBRUc7QUFDSCxNQUFNLE9BQU8sWUFBWTtDQUFHIn0= | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5jbGFzc2VzLmJsb2NraGFuZGxlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Z3VhcmQuY2xhc3Nlcy5ibG9ja2hhbmRsZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLE9BQU8sTUFBTSx5QkFBeUIsQ0FBQztBQUVuRDs7R0FFRztBQUNILE1BQU0sT0FBTyxZQUFZO0NBQUcifQ== |
@@ -1,2 +0,2 @@ | ||
export declare type TGuardFunction<T> = (dataArg: T) => Promise<boolean>; | ||
export type TGuardFunction<T> = (dataArg: T) => Promise<boolean>; | ||
export declare class Guard<T> { | ||
@@ -3,0 +3,0 @@ private guardFunction; |
@@ -1,4 +0,3 @@ | ||
import './smartguard.plugins.js'; | ||
import * as plugins from './smartguard.plugins.js'; | ||
export class Guard { | ||
guardFunction; | ||
constructor(guardFunctionArg) { | ||
@@ -16,2 +15,2 @@ this.guardFunction = guardFunctionArg; | ||
} | ||
//# sourceMappingURL=data:application/json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xDQUFDO1FBQ2pELE9BQU8sTUFBTSxDQUFDO0lBQ2hCLENBQUM7Q0FDRiJ9 |
@@ -1,3 +0,3 @@ | ||
import './smartguard.plugins.js'; | ||
import './smartguard.classes.guard.js'; | ||
import * as plugins from './smartguard.plugins.js'; | ||
import { Guard } from './smartguard.classes.guard.js'; | ||
/** | ||
@@ -7,4 +7,2 @@ * a guardSet is a set of guards that need to be fulfilled | ||
export class GuardSet { | ||
guards; | ||
passed; | ||
constructor(guardsArrayArg) { | ||
@@ -23,2 +21,2 @@ this.guards = guardsArrayArg; | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5jbGFzc2VzLmd1YXJkc2V0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRndWFyZC5jbGFzc2VzLmd1YXJkc2V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQXlCLHlCQUF5QixDQUFDO0FBQ25ELE9BQXNCLCtCQUErQixDQUFDO0FBRXREOztHQUVHO0FBQ0gsTUFBTSxPQUFPLFFBQVE7SUFDWixNQUFNLENBQWtCO0lBQ3hCLE1BQU0sQ0FBVTtJQUN2QixZQUFZLGNBQStCO1FBQ3pDLElBQUksQ0FBQyxNQUFNLEdBQUcsY0FBYyxDQUFDO0lBQy9CLENBQUM7SUFFTSxLQUFLLENBQUMscUJBQXFCLENBQUMsT0FBVTtRQUMzQyxNQUFNLGNBQWMsR0FBNEIsRUFBRSxDQUFDO1FBQ25ELEtBQUssTUFBTSxLQUFLLElBQUksSUFBSSxDQUFDLE1BQU0sRUFBRTtZQUMvQixNQUFNLGFBQWEsR0FBRyxLQUFLLENBQUMsb0JBQW9CLENBQUMsT0FBTyxDQUFDLENBQUM7WUFDMUQsY0FBYyxDQUFDLElBQUksQ0FBQyxhQUFhLENBQUMsQ0FBQztTQUNwQztRQUNELE1BQU0sT0FBTyxHQUFHLE9BQU8sQ0FBQyxHQUFHLENBQUMsY0FBYyxDQUFDLENBQUM7UUFDNUMsT0FBTyxPQUFPLENBQUM7SUFDakIsQ0FBQztDQUNGIn0= | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5jbGFzc2VzLmd1YXJkc2V0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRndWFyZC5jbGFzc2VzLmd1YXJkc2V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0seUJBQXlCLENBQUM7QUFDbkQsT0FBTyxFQUFFLEtBQUssRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBRXREOztHQUVHO0FBQ0gsTUFBTSxPQUFPLFFBQVE7SUFHbkIsWUFBWSxjQUErQjtRQUN6QyxJQUFJLENBQUMsTUFBTSxHQUFHLGNBQWMsQ0FBQztJQUMvQixDQUFDO0lBRU0sS0FBSyxDQUFDLHFCQUFxQixDQUFDLE9BQVU7UUFDM0MsTUFBTSxjQUFjLEdBQTRCLEVBQUUsQ0FBQztRQUNuRCxLQUFLLE1BQU0sS0FBSyxJQUFJLElBQUksQ0FBQyxNQUFNLEVBQUUsQ0FBQztZQUNoQyxNQUFNLGFBQWEsR0FBRyxLQUFLLENBQUMsb0JBQW9CLENBQUMsT0FBTyxDQUFDLENBQUM7WUFDMUQsY0FBYyxDQUFDLElBQUksQ0FBQyxhQUFhLENBQUMsQ0FBQztRQUNyQyxDQUFDO1FBQ0QsTUFBTSxPQUFPLEdBQUcsT0FBTyxDQUFDLEdBQUcsQ0FBQyxjQUFjLENBQUMsQ0FBQztRQUM1QyxPQUFPLE9BQU8sQ0FBQztJQUNqQixDQUFDO0NBQ0YifQ== |
@@ -1,2 +0,2 @@ | ||
import * as smartpromise from '@pushrocks/smartpromise'; | ||
import * as smartpromise from '@push.rocks/smartpromise'; | ||
export { smartpromise }; |
@@ -1,4 +0,4 @@ | ||
import * as smartpromise from '@pushrocks/smartpromise'; | ||
import * as smartpromise from '@push.rocks/smartpromise'; | ||
// pushrocks scope | ||
export { smartpromise }; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRndWFyZC5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxZQUFZLE1BQU0seUJBQXlCLENBQUM7QUFFeEQsa0JBQWtCO0FBQ2xCLE9BQU8sRUFBRSxZQUFZLEVBQUUsQ0FBQyJ9 | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRndWFyZC5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRndWFyZC5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxZQUFZLE1BQU0sMEJBQTBCLENBQUM7QUFFekQsa0JBQWtCO0FBQ2xCLE9BQU8sRUFBRSxZQUFZLEVBQUUsQ0FBQyJ9 |
@@ -5,9 +5,24 @@ { | ||
"module": { | ||
"githost": "gitlab.com", | ||
"gitscope": "pushrocks", | ||
"githost": "code.foss.global", | ||
"gitscope": "push.rocks", | ||
"gitrepo": "smartguard", | ||
"description": "smart guards for validations", | ||
"npmPackagename": "@pushrocks/smartguard", | ||
"description": "A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.", | ||
"npmPackagename": "@push.rocks/smartguard", | ||
"license": "MIT", | ||
"projectDomain": "push.rocks" | ||
"projectDomain": "push.rocks", | ||
"keywords": [ | ||
"validation", | ||
"guards", | ||
"typescript", | ||
"async", | ||
"nodejs", | ||
"express", | ||
"middleware", | ||
"data validation", | ||
"security", | ||
"input validation", | ||
"API", | ||
"custom validation", | ||
"composite validation" | ||
] | ||
} | ||
@@ -18,3 +33,6 @@ }, | ||
"npmAccessLevel": "public" | ||
}, | ||
"tsdoc": { | ||
"legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n" | ||
} | ||
} |
{ | ||
"name": "@push.rocks/smartguard", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"private": false, | ||
"description": "smart guards for validations", | ||
"description": "A TypeScript library for creating and managing validation guards, aiding in data validation and security checks.", | ||
"main": "dist_ts/index.js", | ||
@@ -11,19 +11,13 @@ "typings": "dist_ts/index.d.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "(tstest test/)", | ||
"build": "(tsbuild)", | ||
"format": "(gitzone format)" | ||
}, | ||
"devDependencies": { | ||
"@gitzone/tsbuild": "^2.1.60", | ||
"@gitzone/tstest": "^1.0.69", | ||
"@pushrocks/smartexpress": "^3.0.110", | ||
"@pushrocks/tapbundle": "^5.0.2", | ||
"@types/node": "^17.0.22", | ||
"tslint": "^6.1.3", | ||
"tslint-config-prettier": "^1.15.0" | ||
"@api.global/typedserver": "^3.0.50", | ||
"@git.zone/tsbuild": "^2.1.66", | ||
"@git.zone/tsrun": "^1.2.46", | ||
"@git.zone/tstest": "^1.0.77", | ||
"@push.rocks/tapbundle": "^5.0.8", | ||
"@types/node": "^20.12.13" | ||
}, | ||
"dependencies": { | ||
"@pushrocks/smartpromise": "^3.0.2", | ||
"@pushrocks/smartrequest": "^1.1.56" | ||
"@push.rocks/smartpromise": "^4.0.2", | ||
"@push.rocks/smartrequest": "^2.0.15" | ||
}, | ||
@@ -44,3 +38,28 @@ "files": [ | ||
"last 1 chrome versions" | ||
] | ||
], | ||
"keywords": [ | ||
"validation", | ||
"guards", | ||
"typescript", | ||
"async", | ||
"nodejs", | ||
"express", | ||
"middleware", | ||
"data validation", | ||
"security", | ||
"input validation", | ||
"API", | ||
"custom validation", | ||
"composite validation" | ||
], | ||
"homepage": "https://code.foss.global/push.rocks/smartguard", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://code.foss.global/push.rocks/smartguard.git" | ||
}, | ||
"scripts": { | ||
"test": "(tstest test/)", | ||
"build": "(tsbuild)", | ||
"format": "(gitzone format)" | ||
} | ||
} |
297
readme.md
@@ -1,38 +0,281 @@ | ||
# @pushrocks/smartguard | ||
smart guards for validations | ||
# @push.rocks/smartguard | ||
A library for creating and managing validation guards. | ||
## Availabililty and Links | ||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartguard) | ||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartguard) | ||
* [github.com (source mirror)](https://github.com/pushrocks/smartguard) | ||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartguard/) | ||
## Install | ||
## Status for master | ||
To install `@push.rocks/smartguard`, run the following command in your terminal: | ||
Status Category | Status Badge | ||
-- | -- | ||
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartguard/badges/master/pipeline.svg)](https://lossless.cloud) | ||
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartguard/badges/master/coverage.svg)](https://lossless.cloud) | ||
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartguard)](https://lossless.cloud) | ||
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartguard)](https://lossless.cloud) | ||
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) | ||
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) | ||
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) | ||
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartguard)](https://lossless.cloud) | ||
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartguard)](https://lossless.cloud) | ||
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartguard)](https://lossless.cloud) | ||
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud) | ||
```bash | ||
npm install @push.rocks/smartguard --save | ||
``` | ||
This will add `@push.rocks/smartguard` to your project's dependencies. | ||
## Usage | ||
`@push.rocks/smartguard` provides a robust and easy way to validate data by using guards. Guards are functions that return a boolean value indicating whether the data meets certain criteria. This package is highly beneficial for input validation, security checks, or any scenario where data needs to conform to specific rules or patterns. | ||
## Contribution | ||
### Basics | ||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) | ||
At the core of `@push.rocks/smartguard` are two main classes: `Guard` and `GuardSet`. A `Guard` represents a single rule or validation step, while a `GuardSet` allows you to combine multiple `Guard` instances and evaluate them together. | ||
For further information read the linked docs at the top of this readme. | ||
### Creating a Guard | ||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) | ||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) | ||
A `Guard` is an object that encapsulates a validation rule. You define a guard by providing a function that takes an input and returns a Promise, resolving to a boolean value indicating if the input meets the criteria. | ||
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com) | ||
```typescript | ||
import { Guard } from '@push.rocks/smartguard'; | ||
const isStringGuard = new Guard<string>(async (data) => { | ||
return typeof data === 'string'; | ||
}); | ||
``` | ||
In the example above, we define a simple guard that checks if the input is a string. | ||
### Using GuardSets for Composite Validations | ||
When you have multiple validation rules, you can combine them using `GuardSet`. This allows you to evaluate all guards on a piece of data and only pass if all guards return true. | ||
```typescript | ||
import { Guard, GuardSet } from '@push.rocks/smartguard'; | ||
const isStringGuard = new Guard<string>(async (data) => { | ||
return typeof data === 'string'; | ||
}); | ||
const isNotEmptyGuard = new Guard<string>(async (data) => { | ||
return data.length > 0; | ||
}); | ||
const stringValidationSet = new GuardSet<string>([isStringGuard, isNotEmptyGuard]); | ||
// Now you can use stringValidationSet.executeGuardsWithData(data) to validate your data | ||
``` | ||
### Executing Guards | ||
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`. | ||
```typescript | ||
const isValidString = await isStringGuard.executeGuardWithData('Hello World!'); | ||
console.log(isValidString); // true | ||
const areValidStrings = await stringValidationSet.executeGuardsWithData('Hello World!'); | ||
console.log(areValidStrings.every(result => result)); // true if all validations passed | ||
``` | ||
### Advanced Usage: Custom Guard Functions | ||
Guards can perform any asynchronous operation inside their validation function, making them incredibly versatile. For instance, you could call an API to validate an address, check if a username already exists in a database, or even integrate with third-party validation services. | ||
```typescript | ||
import { Guard } from '@push.rocks/smartguard'; | ||
import { someApiRequestFunction } from './myApiFunctions'; | ||
const isValidAddressGuard = new Guard<string>(async (address) => { | ||
const response = await someApiRequestFunction(address); | ||
return response.isValid; | ||
}); | ||
``` | ||
### Integrating with Express Middleware | ||
`@push.rocks/smartguard` can easily integrate with frameworks like Express by utilizing guards within middleware functions. This allows you to perform validations before a request reaches your route handlers. | ||
```typescript | ||
import * as express from 'express'; | ||
import { Guard } from '@push.rocks/smartguard'; | ||
const app = express(); | ||
const isAuthorizedUserGuard = new Guard<express.Request>(async (req) => { | ||
// your logic here, return true if authorized | ||
return req.headers.authorization === 'Bearer some-token'; | ||
}); | ||
app.use(async (req, res, next) => { | ||
const isAuthorized = await isAuthorizedUserGuard.executeGuardWithData(req); | ||
if (!isAuthorized) { | ||
res.status(403).send('Unauthorized'); | ||
return; | ||
} | ||
next(); | ||
}); | ||
app.listen(3000, () => console.log('Server running on port 3000')); | ||
``` | ||
In the example above, we use a guard to check if a request has a valid authorization header. This demonstrates how `@push.rocks/smartguard` can be seamlessly integrated into existing server applications to enforce security or input validations. | ||
### Combining Guards with `GuardSet` | ||
One of the strengths of `@push.rocks/smartguard` is its ability to combine multiple guards into a `GuardSet`. This is particularly useful when you need to validate data against several criteria. For example, to validate a string that must be non-empty and start with a specific prefix: | ||
```typescript | ||
import { Guard, GuardSet } from '@push.rocks/smartguard'; | ||
const isStringGuard = new Guard<string>(async (data) => { | ||
return typeof data === 'string'; | ||
}); | ||
const isNotEmptyGuard = new Guard<string>(async (data) => { | ||
return data.length > 0; | ||
}); | ||
const startsWithPrefixGuard = new Guard<string>(async (data) => { | ||
return data.startsWith('prefix'); | ||
}); | ||
const combinedValidationSet = new GuardSet<string>([isStringGuard, isNotEmptyGuard, startsWithPrefixGuard]); | ||
const validationResults = await combinedValidationSet.executeGuardsWithData('prefix: Valid String'); | ||
console.log(validationResults.every(result => result)); // true if all validations passed | ||
``` | ||
### Integration with Other Libraries | ||
To demonstrate the versatility and integration capabilities of `@push.rocks/smartguard`, let's integrate it with another popular library, `@push.rocks/smartrequest`, for validating API response data. | ||
```typescript | ||
import { Guard } from '@push.rocks/smartguard'; | ||
import { smartrequest } from '@push.rocks/smartrequest'; | ||
const validApiResponseGuard = new Guard(async (url: string) => { | ||
const response = await smartrequest.request(url, { method: 'GET' }); | ||
return response.status === 200; | ||
}); | ||
const isValidResponse = await validApiResponseGuard.executeGuardWithData('https://example.com/api/data'); | ||
console.log(isValidResponse); // true if the API response status is 200 | ||
``` | ||
### Real-World Example: Form Validation | ||
Let's create a real-world example where we use `@push.rocks/smartguard` to validate form data in a Node.js application. Suppose we have a user registration form with fields for `username`, `email`, and `password`. | ||
```typescript | ||
import { Guard, GuardSet } from '@push.rocks/smartguard'; | ||
// Guards for individual fields | ||
const isUsernameValid = new Guard<string>(async (username) => { | ||
return typeof username === 'string' && username.length >= 3; | ||
}); | ||
const isEmailValid = new Guard<string>(async (email) => { | ||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
return typeof email === 'string' && emailRegex.test(email); | ||
}); | ||
const isPasswordStrong = new Guard<string>(async (password) => { | ||
return typeof password === 'string' && password.length >= 8; | ||
}); | ||
// Combining guards using GuardSet | ||
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)) | ||
]); | ||
// Form data to validate | ||
const formData = { | ||
username: 'exampleUser', | ||
email: 'user@example.com', | ||
password: 'strongpassword123' | ||
}; | ||
const formValidationResults = await registrationValidationSet.executeGuardsWithData(formData); | ||
console.log(formValidationResults.every(result => result)); // true if all fields are valid | ||
``` | ||
In this example, we used guards to validate each form field. We then combined these guards into a `GuardSet` to validate the entire form data object. | ||
### Validating Nested Objects | ||
`@push.rocks/smartguard` can also handle validation of nested objects. Suppose you need to validate a user profile that includes nested address information. | ||
```typescript | ||
interface UserProfile { | ||
username: string; | ||
email: string; | ||
address: { | ||
street: string; | ||
city: string; | ||
postalCode: string; | ||
}; | ||
} | ||
const isStreetValid = new Guard<string>(async (street) => { | ||
return typeof street === 'string' && street.length > 0; | ||
}); | ||
const isCityValid = new Guard<string>(async (city) => { | ||
return typeof city === 'string' && city.length > 0; | ||
}); | ||
const isPostalCodeValid = new Guard<string>(async (postalCode) => { | ||
return typeof postalCode === 'string' && /^[0-9]{5}$/.test(postalCode); | ||
}); | ||
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); | ||
return streetValid && cityValid && postalCodeValid; | ||
}); | ||
const isUsernameValid = new Guard<string>(async (username) => { | ||
return typeof username === 'string' && username.length >= 3; | ||
}); | ||
const isEmailValid = new Guard<string>(async (email) => { | ||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
return typeof email === 'string' && emailRegex.test(email); | ||
}); | ||
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)) | ||
]); | ||
const userProfile = { | ||
username: 'exampleUser', | ||
email: 'user@example.com', | ||
address: { | ||
street: '123 Main St', | ||
city: 'Anytown', | ||
postalCode: '12345' | ||
} | ||
}; | ||
const userProfileValidationResults = await userProfileValidationSet.executeGuardsWithData(userProfile); | ||
console.log(userProfileValidationResults.every(result => result)); // true if user profile is valid | ||
``` | ||
In this example, we created a nested guard structure to validate a user profile object that includes address information. Each nested object is validated individually using its specific guards. | ||
### Summary | ||
`@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. | ||
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. | ||
## License and Legal Information | ||
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. | ||
**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. |
@@ -1,4 +0,4 @@ | ||
import * as smartpromise from '@pushrocks/smartpromise'; | ||
import * as smartpromise from '@push.rocks/smartpromise'; | ||
// pushrocks scope | ||
export { smartpromise }; |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
25804
6
23
214
0
282
0
+ Added@push.rocks/smartpromise@4.0.4(transitive)
+ Added@push.rocks/smartrequest@2.0.23(transitive)
+ Added@push.rocks/smarturl@3.1.0(transitive)
- Removed@pushrocks/smartpromise@^3.0.2
- Removed@pushrocks/smartrequest@^1.1.56
- Removed@pushrocks/smartpromise@3.1.10(transitive)
- Removed@pushrocks/smartrequest@1.1.56(transitive)
- Removed@pushrocks/smarturl@2.0.1(transitive)