Skip to content

Commit

Permalink
feat(validation): init package
Browse files Browse the repository at this point in the history
  • Loading branch information
TorinAsakura committed Sep 12, 2024
1 parent ef8cc17 commit e269a80
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 1 deletion.
161 changes: 161 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions packages/nestjs-validation/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/nestjs-validation/src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './validation.error.js'
9 changes: 9 additions & 0 deletions packages/nestjs-validation/src/errors/validation.error.ts
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'
}
}
3 changes: 3 additions & 0 deletions packages/nestjs-validation/src/index.ts
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'
1 change: 1 addition & 0 deletions packages/nestjs-validation/src/module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './validation.module.js'
26 changes: 26 additions & 0 deletions packages/nestjs-validation/src/module/validation.module.ts
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,
},
],
}
}
}
1 change: 1 addition & 0 deletions packages/nestjs-validation/src/validator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './validator.js'
24 changes: 24 additions & 0 deletions packages/nestjs-validation/src/validator/validator.ts
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
}
}
Loading

0 comments on commit e269a80

Please sign in to comment.