use class-transformer to validate interfaces

This commit is contained in:
admin 2025-08-21 23:13:57 +02:00
parent 7bc047b4da
commit 264da24033
3 changed files with 17 additions and 5 deletions

11
package-lock.json generated
View File

@ -1,18 +1,19 @@
{ {
"name": "@apihub24/token-authentication", "name": "@apihub24/token-authentication",
"version": "1.0.4", "version": "1.0.5",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@apihub24/token-authentication", "name": "@apihub24/token-authentication",
"version": "1.0.4", "version": "1.0.5",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@apihub24/repository": "^1.0.3", "@apihub24/repository": "^1.0.3",
"@nestjs/common": "^11.1.6", "@nestjs/common": "^11.1.6",
"@nestjs/config": "^4.0.2", "@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.1.6", "@nestjs/core": "^11.1.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.2" "class-validator": "^0.14.2"
}, },
"devDependencies": { "devDependencies": {
@ -2022,6 +2023,12 @@
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/class-transformer": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==",
"license": "MIT"
},
"node_modules/class-validator": { "node_modules/class-validator": {
"version": "0.14.2", "version": "0.14.2",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.2.tgz", "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.2.tgz",

View File

@ -1,6 +1,6 @@
{ {
"name": "@apihub24/token-authentication", "name": "@apihub24/token-authentication",
"version": "1.0.4", "version": "1.0.5",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
@ -16,7 +16,8 @@
"@nestjs/config": "^4.0.2", "@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.1.6", "@nestjs/core": "^11.1.6",
"@apihub24/repository": "^1.0.3", "@apihub24/repository": "^1.0.3",
"class-validator": "^0.14.2" "class-validator": "^0.14.2",
"class-transformer": "^0.5.1"
}, },
"devDependencies": { "devDependencies": {
"rimraf": "^6.0.1", "rimraf": "^6.0.1",

View File

@ -5,6 +5,8 @@ import * as passwordService from "../contracts/services/password.service";
import { IRegistration } from "../contracts/models/registration"; import { IRegistration } from "../contracts/models/registration";
import { IAccount } from "../contracts/models/account"; import { IAccount } from "../contracts/models/account";
import { Account } from "../models/account"; import { Account } from "../models/account";
import { plainToClass } from "class-transformer";
import { Registration } from "../models/registration";
@Injectable() @Injectable()
export class AccountFactoryService { export class AccountFactoryService {
@ -16,7 +18,9 @@ export class AccountFactoryService {
) {} ) {}
async createFromRegistration(registration: IRegistration): Promise<IAccount> { async createFromRegistration(registration: IRegistration): Promise<IAccount> {
let validationErrors = await validate(registration); let validationErrors = await validate(
plainToClass(Registration, registration)
);
if (validationErrors?.length) { if (validationErrors?.length) {
throw new Error(validationErrors[0].toString()); throw new Error(validationErrors[0].toString());
} }