-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef8cc17
commit e269a80
Showing
10 changed files
with
382 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "@atls/nestjs-validation", | ||
"version": "0.0.0", | ||
"license": "BSD-3-Clause", | ||
"type": "module", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": "./src/index.ts" | ||
}, | ||
"main": "src/index.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn library build", | ||
"prepack": "yarn run build", | ||
"postpack": "rm -rf dist" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/common": "^10.0.5", | ||
"@nestjs/core": "^10.0.5", | ||
"class-transformer": "0.5.1", | ||
"class-validator": "^0.14.0", | ||
"reflect-metadata": "^0.1.13", | ||
"rxjs": "^7.8.1" | ||
}, | ||
"peerDependencies": { | ||
"@nestjs/common": "^10", | ||
"@nestjs/core": "^10", | ||
"class-transformer": "0.5", | ||
"class-validator": "^0.14", | ||
"reflect-metadata": "^0.1", | ||
"rxjs": "^7" | ||
}, | ||
"publishConfig": { | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"import": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validation.error.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { ValidationError as VError } from 'class-validator' | ||
|
||
export class ValidationError extends Error { | ||
constructor(public readonly errors: Array<VError>) { | ||
super() | ||
|
||
this.message = 'Validation failed' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './validator/index.js' | ||
export * from './module/index.js' | ||
export * from './errors/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validation.module.js' |
26 changes: 26 additions & 0 deletions
26
packages/nestjs-validation/src/module/validation.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { DynamicModule } from '@nestjs/common' | ||
|
||
import { Module } from '@nestjs/common' | ||
|
||
import { Validator } from '../validator/index.js' | ||
|
||
@Module({}) | ||
export class ValidationModule { | ||
static register(): DynamicModule { | ||
return { | ||
module: ValidationModule, | ||
providers: [ | ||
{ | ||
provide: Validator, | ||
useClass: Validator, | ||
}, | ||
], | ||
exports: [ | ||
{ | ||
provide: Validator, | ||
useClass: Validator, | ||
}, | ||
], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './validator.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ClassConstructor } from 'class-transformer' | ||
|
||
import { plainToInstance } from 'class-transformer' | ||
import { validate } from 'class-validator' | ||
|
||
import { ValidationError } from '../errors/index.js' | ||
|
||
export class Validator { | ||
async transform<T>(metatype: ClassConstructor<unknown>, value: object): Promise<T> { | ||
return plainToInstance(metatype, value) as T | ||
} | ||
|
||
async validate<T>(valueOrObject: object, metatype?: ClassConstructor<unknown>): Promise<T> { | ||
const transformed = metatype ? await this.transform<T>(metatype, valueOrObject) : valueOrObject | ||
|
||
const errors = await validate(transformed as object) | ||
|
||
if (errors.length > 0) { | ||
throw new ValidationError(errors) | ||
} | ||
|
||
return transformed as T | ||
} | ||
} |
Oops, something went wrong.