Skip to content

Commit

Permalink
Merge pull request #1 from SecJS/feat/len-initial-commit
Browse files Browse the repository at this point in the history
feat: Initial Commit v1.0.0
  • Loading branch information
jlenon7 authored Oct 8, 2021
2 parents 31cf26b + d4650ed commit e90e4f7
Show file tree
Hide file tree
Showing 18 changed files with 15,281 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
Binary file added .github/validator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CD Validator

on:
push:
branches:
- main

jobs:
build:

runs-on: ubuntu-18.04

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/setup-node@v1
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'

- name: Download dependencies
run: npm install

- name: Transpile typescript to javascript
run: npm run build

- name: Automatic GitHub Release
uses: justincy/[email protected]
id: release

- name: Publish to NPM Registry
run: yarn publish --access public
if: steps.release.outputs.released == 'true'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
name: Deploy
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI Validator

on:
pull_request:
branches:
- main

jobs:
build:

runs-on: ubuntu-18.04

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/setup-node@v1
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'

- name: Download dependencies
run: npm install

- name: Verify project lint and try to fix it
run: npm run lint:fix

- name: Run the tests from project
run: npm run test
124 changes: 124 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

node_modules/
dist/
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IDE
.idea
.vscode

# dotenv environment variables file
.env
.env.testing
.env.production
149 changes: 149 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Validator ✅

> Validate payloads from any NodeJS project.
[![GitHub followers](https://img.shields.io/github/followers/jlenon7.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/jlenon7?tab=followers)
[![GitHub stars](https://img.shields.io/github/stars/secjs/validator.svg?style=social&label=Star&maxAge=2592000)](https://github.com/secjs/validator/stargazers/)

<p>
<img alt="GitHub language count" src="https://img.shields.io/github/languages/count/secjs/validator?style=for-the-badge&logo=appveyor">

<img alt="Repository size" src="https://img.shields.io/github/repo-size/secjs/validator?style=for-the-badge&logo=appveyor">

<img alt="License" src="https://img.shields.io/badge/license-MIT-brightgreen?style=for-the-badge&logo=appveyor">
</p>

The intention behind this repository is to maintain a payload validator package to use inside any NodeJS project.

<img src=".github/validator.png" width="200px" align="right" hspace="30px" vspace="100px">

## Installation

> To use the high potential from this package you need to install first this other packages from SecJS,
> it keeps as dev dependency because one day `@secjs/core` will install everything once.
```bash
npm install @secjs/exceptions
```

> Then you can install the package using:
```bash
npm install @secjs/validator
```

## Validator

> Use Validator class to extend in your validation classes
```js
import { Validator } from '@secjs/validator'

export class UserValidator extends Validator {
createSchema() {
return {
name: 'string|required',
email: 'email|required',
}
}

updateSchema() {
return {
name: 'string',
email: 'string',
}
}
}

const userValidator = new UserValidator()

userValidator.validate({ name: 'João', email: '[email protected]' }, 'createSchema') // Return on first error or undefined
userValidator.validateAll({ name: 'João', email: '[email protected]' }, 'updateSchema') // Return all errors or undefined
```

## Sanitizer

> Use Sanitizer class to extend in your validation classes
```ts
import { Sanitizer } from '@secjs/validator'

export class UserSanitizer extends Sanitizer {
createSchema() {
return {
email: 'trim',
}
}

updateSchema() {
return {
email: 'trim',
}
}
}

const userSanitizer = new UserSanitizer()

userSanitizer.sanitize({ email: '[email protected]' }, 'createSchema') // Return the object with sanitizations implemented
// { email: '[email protected]' }
```

## Extend Validator and Sanitizer

> Extend validation and sanitizer rules
```ts
import * as he from 'he'
import { Validator, Sanitizer } from '@secjs/validator'

export class ExtendValidator {
protected validator: Validator

constructor() {
this.validator = new Validator()

this.validator.extendAsync('unique', this.unique)
}

// Returning false will fail the validation
unique = async (data: any, field: string, args: string[]) => {
const repository = this.getRepository(args[0])

const model = await repository.getOne(null, {
where: { [field]: this.validator.getValue(data, field) },
})

return !model
}
}

export class ExtendSanitizer {
protected sanitizer: Sanitizer

constructor() {
this.sanitizer = new Sanitizer()

this.sanitizer.extend('escape', this.escape)
}

escape = async (data: any, field: string, args: string[], config: any) => {
let fieldValue = this.sanitizer.getValue(data, field)

if (typeof (fieldValue) !== 'string') {
return
}

this.sanitizer.patchValue(data, field, he.escape(fieldValue))
}
}
```

## More rules

> This project is using [indicative](https://github.com/poppinss/indicative) package to implement
> class Sanitizer and Validator if you want to check all the validation and sanitizer rules check
> [indicative documentation](https://indicative.adonisjs.com/).
---

Made with 🖤 by [jlenon7](https://github.com/jlenon7) :wave:
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './src/Sanitizer'
export * from './src/Validator'
Loading

0 comments on commit e90e4f7

Please sign in to comment.