|
| 1 | +# Validator ✅ |
| 2 | + |
| 3 | +> Validate payloads from any NodeJS project. |
| 4 | +
|
| 5 | +[](https://github.com/jlenon7?tab=followers) |
| 6 | +[](https://github.com/secjs/validator/stargazers/) |
| 7 | + |
| 8 | +<p> |
| 9 | + <img alt="GitHub language count" src="https://img.shields.io/github/languages/count/secjs/validator?style=for-the-badge&logo=appveyor"> |
| 10 | + |
| 11 | + <img alt="Repository size" src="https://img.shields.io/github/repo-size/secjs/validator?style=for-the-badge&logo=appveyor"> |
| 12 | + |
| 13 | + <img alt="License" src="https://img.shields.io/badge/license-MIT-brightgreen?style=for-the-badge&logo=appveyor"> |
| 14 | +</p> |
| 15 | + |
| 16 | +The intention behind this repository is to maintain a payload validator package to use inside any NodeJS project. |
| 17 | + |
| 18 | +<img src=".github/validator.png" width="200px" align="right" hspace="30px" vspace="100px"> |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +> To use the high potential from this package you need to install first this other packages from SecJS, |
| 23 | +> it keeps as dev dependency because one day `@secjs/core` will install everything once. |
| 24 | +
|
| 25 | +```bash |
| 26 | +npm install @secjs/exceptions |
| 27 | +``` |
| 28 | + |
| 29 | +> Then you can install the package using: |
| 30 | +
|
| 31 | +```bash |
| 32 | +npm install @secjs/validator |
| 33 | +``` |
| 34 | + |
| 35 | +## Validator |
| 36 | + |
| 37 | +> Use Validator class to extend in your validation classes |
| 38 | +
|
| 39 | +```js |
| 40 | +import { Validator } from '@secjs/validator' |
| 41 | + |
| 42 | +export class UserValidator extends Validator { |
| 43 | + createSchema() { |
| 44 | + return { |
| 45 | + name: 'string|required', |
| 46 | + email: 'email|required', |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + updateSchema() { |
| 51 | + return { |
| 52 | + name: 'string', |
| 53 | + email: 'string', |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +const userValidator = new UserValidator() |
| 59 | + |
| 60 | +userValidator.validate({ name: 'João', email: 'lenonSec7@gmail.com' }, 'createSchema') // Return on first error or undefined |
| 61 | +userValidator.validateAll({ name: 'João', email: 'lenonSec7@gmail.com' }, 'updateSchema') // Return all errors or undefined |
| 62 | +``` |
| 63 | + |
| 64 | +## Sanitizer |
| 65 | + |
| 66 | +> Use Sanitizer class to extend in your validation classes |
| 67 | +
|
| 68 | +```ts |
| 69 | +import { Sanitizer } from '@secjs/validator' |
| 70 | + |
| 71 | +export class UserSanitizer extends Sanitizer { |
| 72 | + createSchema() { |
| 73 | + return { |
| 74 | + email: 'trim', |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + updateSchema() { |
| 79 | + return { |
| 80 | + email: 'trim', |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const userSanitizer = new UserSanitizer() |
| 86 | + |
| 87 | +userSanitizer.sanitize({ email: 'lenonSec7@gmail.com' }, 'createSchema') // Return the object with sanitizations implemented |
| 88 | +// { email: 'lenonsec7@gmail.com' } |
| 89 | +``` |
| 90 | + |
| 91 | +## Extend Validator and Sanitizer |
| 92 | + |
| 93 | +> Extend validation and sanitizer rules |
| 94 | +
|
| 95 | +```ts |
| 96 | +import * as he from 'he' |
| 97 | +import { Validator, Sanitizer } from '@secjs/validator' |
| 98 | + |
| 99 | +export class ExtendValidator { |
| 100 | + protected validator: Validator |
| 101 | + |
| 102 | + constructor() { |
| 103 | + this.validator = new Validator() |
| 104 | + |
| 105 | + this.validator.extendAsync('unique', this.unique) |
| 106 | + } |
| 107 | + |
| 108 | + // Returning false will fail the validation |
| 109 | + unique = async (data: any, field: string, args: string[]) => { |
| 110 | + const repository = this.getRepository(args[0]) |
| 111 | + |
| 112 | + const model = await repository.getOne(null, { |
| 113 | + where: { [field]: this.validator.getValue(data, field) }, |
| 114 | + }) |
| 115 | + |
| 116 | + return !model |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +export class ExtendSanitizer { |
| 121 | + protected sanitizer: Sanitizer |
| 122 | + |
| 123 | + constructor() { |
| 124 | + this.sanitizer = new Sanitizer() |
| 125 | + |
| 126 | + this.sanitizer.extend('escape', this.escape) |
| 127 | + } |
| 128 | + |
| 129 | + escape = async (data: any, field: string, args: string[], config: any) => { |
| 130 | + let fieldValue = this.sanitizer.getValue(data, field) |
| 131 | + |
| 132 | + if (typeof (fieldValue) !== 'string') { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + this.sanitizer.patchValue(data, field, he.escape(fieldValue)) |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## More rules |
| 142 | + |
| 143 | +> This project is using [indicative](https://github.com/poppinss/indicative) package to implement |
| 144 | +> class Sanitizer and Validator if you want to check all the validation and sanitizer rules check |
| 145 | +> [indicative documentation](https://indicative.adonisjs.com/). |
| 146 | +
|
| 147 | +--- |
| 148 | + |
| 149 | +Made with 🖤 by [jlenon7](https://github.com/jlenon7) :wave: |
0 commit comments