Skip to content

Commit e90e4f7

Browse files
authored
Merge pull request #1 from SecJS/feat/len-initial-commit
feat: Initial Commit v1.0.0
2 parents 31cf26b + d4650ed commit e90e4f7

18 files changed

Lines changed: 15281 additions & 0 deletions

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_size = 2
6+
indent_style = space
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.github/validator.png

32.3 KB
Loading

.github/workflows/cd.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CD Validator
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-18.04
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
with:
17+
fetch-depth: 0
18+
19+
- uses: actions/setup-node@v1
20+
with:
21+
node-version: '14.x'
22+
registry-url: 'https://registry.npmjs.org'
23+
24+
- name: Download dependencies
25+
run: npm install
26+
27+
- name: Transpile typescript to javascript
28+
run: npm run build
29+
30+
- name: Automatic GitHub Release
31+
uses: justincy/github-action-npm-release@2.0.1
32+
id: release
33+
34+
- name: Publish to NPM Registry
35+
run: yarn publish --access public
36+
if: steps.release.outputs.released == 'true'
37+
env:
38+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
39+
name: Deploy

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CI Validator
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-18.04
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
with:
17+
fetch-depth: 0
18+
19+
- uses: actions/setup-node@v1
20+
with:
21+
node-version: '14.x'
22+
registry-url: 'https://registry.npmjs.org'
23+
24+
- name: Download dependencies
25+
run: npm install
26+
27+
- name: Verify project lint and try to fix it
28+
run: npm run lint:fix
29+
30+
- name: Run the tests from project
31+
run: npm run test

.gitignore

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
node_modules/
3+
dist/
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Diagnostic reports (https://nodejs.org/api/report.html)
14+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
15+
16+
# Runtime data
17+
pids
18+
*.pid
19+
*.seed
20+
*.pid.lock
21+
22+
# Directory for instrumented libs generated by jscoverage/JSCover
23+
lib-cov
24+
25+
# Coverage directory used by tools like istanbul
26+
coverage
27+
*.lcov
28+
29+
# nyc test coverage
30+
.nyc_output
31+
32+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
33+
.grunt
34+
35+
# Bower dependency directory (https://bower.io/)
36+
bower_components
37+
38+
# node-waf configuration
39+
.lock-wscript
40+
41+
# Compiled binary addons (https://nodejs.org/api/addons.html)
42+
build/Release
43+
44+
# Dependency directories
45+
jspm_packages/
46+
47+
# Snowpack dependency directory (https://snowpack.dev/)
48+
web_modules/
49+
50+
# TypeScript cache
51+
*.tsbuildinfo
52+
53+
# Optional npm cache directory
54+
.npm
55+
56+
# Optional eslint cache
57+
.eslintcache
58+
59+
# Microbundle cache
60+
.rpt2_cache/
61+
.rts2_cache_cjs/
62+
.rts2_cache_es/
63+
.rts2_cache_umd/
64+
65+
# Optional REPL history
66+
.node_repl_history
67+
68+
# Output of 'npm pack'
69+
*.tgz
70+
71+
# Yarn Integrity file
72+
.yarn-integrity
73+
74+
# parcel-bundler cache (https://parceljs.org/)
75+
.cache
76+
.parcel-cache
77+
78+
# Next.js build output
79+
.next
80+
out
81+
82+
# Nuxt.js build / generate output
83+
.nuxt
84+
dist
85+
86+
# Gatsby files
87+
.cache/
88+
# Comment in the public line in if your project uses Gatsby and not Next.js
89+
# https://nextjs.org/blog/next-9-1#public-directory-support
90+
# public
91+
92+
# vuepress build output
93+
.vuepress/dist
94+
95+
# Serverless directories
96+
.serverless/
97+
98+
# FuseBox cache
99+
.fusebox/
100+
101+
# DynamoDB Local files
102+
.dynamodb/
103+
104+
# TernJS port file
105+
.tern-port
106+
107+
# Stores VSCode versions used for testing VSCode extensions
108+
.vscode-test
109+
110+
# yarn v2
111+
.yarn/cache
112+
.yarn/unplugged
113+
.yarn/build-state.yml
114+
.yarn/install-state.gz
115+
.pnp.*
116+
117+
# IDE
118+
.idea
119+
.vscode
120+
121+
# dotenv environment variables file
122+
.env
123+
.env.testing
124+
.env.production

README.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Validator ✅
2+
3+
> Validate payloads from any NodeJS project.
4+
5+
[![GitHub followers](https://img.shields.io/github/followers/jlenon7.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/jlenon7?tab=followers)
6+
[![GitHub stars](https://img.shields.io/github/stars/secjs/validator.svg?style=social&label=Star&maxAge=2592000)](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:

index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './src/Sanitizer'
2+
export * from './src/Validator'

0 commit comments

Comments
 (0)