|
|
|
@ -55,10 +55,10 @@ const stringValidationSet = new GuardSet<string>([isStringGuard, isNotEmptyGuard
|
|
|
|
|
|
|
|
|
|
### 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`.
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
const areValidStrings = await stringValidationSet.executeGuardsWithData('Hello World!');
|
|
|
|
@ -84,7 +84,7 @@ const isValidAddressGuard = new Guard<string>(async (address) => {
|
|
|
|
|
`@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 express from 'express';
|
|
|
|
|
import { Guard } from '@push.rocks/smartguard';
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
@ -94,7 +94,7 @@ const isAuthorizedUserGuard = new Guard<express.Request>(async (req) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use(async (req, res, next) => {
|
|
|
|
|
const isAuthorized = await isAuthorizedUserGuard.executeGuardWithData(req);
|
|
|
|
|
const isAuthorized = await isAuthorizedUserGuard.execGuardWithData(req);
|
|
|
|
|
if (!isAuthorized) {
|
|
|
|
|
res.status(403).send('Unauthorized');
|
|
|
|
|
return;
|
|
|
|
@ -145,7 +145,7 @@ const validApiResponseGuard = new Guard(async (url: string) => {
|
|
|
|
|
return response.status === 200;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
@ -172,9 +172,9 @@ const isPasswordStrong = new Guard<string>(async (password) => {
|
|
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
|
new Guard(async (data) => isUsernameValid.execGuardWithData(data.username)),
|
|
|
|
|
new Guard(async (data) => isEmailValid.execGuardWithData(data.email)),
|
|
|
|
|
new Guard(async (data) => isPasswordStrong.execGuardWithData(data.password))
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Form data to validate
|
|
|
|
@ -218,9 +218,9 @@ const isPostalCodeValid = new Guard<string>(async (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);
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -234,9 +234,9 @@ const isEmailValid = new Guard<string>(async (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))
|
|
|
|
|
new Guard(async (data) => isUsernameValid.execGuardWithData(data.username)),
|
|
|
|
|
new Guard(async (data) => isEmailValid.execGuardWithData(data.email)),
|
|
|
|
|
new Guard(async (data) => isAddressValid.execGuardWithData(data.address))
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const userProfile = {
|
|
|
|
@ -296,7 +296,7 @@ const isStringGuard = new Guard<string>(async (data) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isStringArrayGuard = new Guard<string[]>(async (data) => {
|
|
|
|
@ -309,14 +309,14 @@ const isEmailGuard = new Guard<string>(async (data) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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)))
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
@ -350,7 +350,7 @@ const isApiKeyValidGuard = new Guard<string>(async (apiKey) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
@ -399,18 +399,31 @@ class MinLengthGuard extends Guard<string> {
|
|
|
|
|
|
|
|
|
|
const minLengthGuard = new MinLengthGuard(10);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In this example, we create a `MinLengthGuard` class that extends `Guard` and validates a string based on its minimum length.
|
|
|
|
|
|
|
|
|
|
### Conclusion
|
|
|
|
|
|
|
|
|
|
`@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.
|
|
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
|