Gathering detailed insights and metrics for class-transformer-validator
Gathering detailed insights and metrics for class-transformer-validator
Gathering detailed insights and metrics for class-transformer-validator
Gathering detailed insights and metrics for class-transformer-validator
A simple plugin for class-transformer and class-validator which combines them in a nice and programmer-friendly API.
npm install class-transformer-validator
94.6
Supply Chain
97.9
Quality
75.9
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
200 Stars
37 Commits
20 Forks
5 Watching
8 Branches
2 Contributors
Updated on 03 Jul 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-9.2%
9,410
Compared to previous day
Last week
-4%
50,858
Compared to previous week
Last month
14.4%
201,175
Compared to previous month
Last year
0.9%
2,589,704
Compared to previous year
A simple plugin for class-transformer and class-validator which combines them in a nice and programmer-friendly API.
npm install class-transformer-validator --save
(or the short way):
npm i -S class-transformer-validator
This package is only a simple plugin/wrapper, so you have to install the required modules too because it can't work without them. See detailed installation instruction for the modules installation:
The usage of this module is very simple.
1import { IsEmail } from "class-validator"; 2import { transformAndValidate } from "class-transformer-validator"; 3 4// declare the class using class-validator decorators 5class User { 6 @IsEmail() 7 public email: string; 8 9 public hello(): string { 10 return "World!"; 11 } 12} 13 14// then load the JSON string from any part of your app 15const userJson: string = loadJsonFromSomething(); 16 17// transform the JSON to class instance and validate it correctness 18transformAndValidate(User, userJson) 19 .then((userObject: User) => { 20 // now you can access all your class prototype method 21 console.log(`Hello ${userObject.hello()}`); // prints "Hello World!" on console 22 }) 23 .catch(err => { 24 // here you can handle error on transformation (invalid JSON) 25 // or validation error (e.g. invalid email property) 26 console.error(err); 27 });
You can also transform and validate plain JS object (e.g. from express req.body). Using ES7 async/await syntax:
1async (req, res) => { 2 try { 3 // transform and validate request body 4 const userObject = await transformAndValidate(User, req.body); 5 // infered type of userObject is User, you can access all class prototype properties and methods 6 } catch (err) { 7 // your error handling 8 console.error(err); 9 } 10};
And since release 0.3.0
you can also pass array of objects - all of them will be validated using given class validation constraints:
1async (req, res) => {
2 try {
3 // transform and validate request body - array of User objects
4 const userObjects = await transformAndValidate(User, req.body);
5 userObjects.forEach(user => console.log(`Hello ${user.hello()}`));
6 } catch (err) {
7 // your error handling
8 }
9};
There is available the transformAndValidate
function with three overloads:
1function transformAndValidate<T extends object>( 2 classType: ClassType<T>, 3 jsonString: string, 4 options?: TransformValidationOptions, 5): Promise<T | T[]>;
1function transformAndValidate<T extends object>( 2 classType: ClassType<T>, 3 object: object, 4 options?: TransformValidationOptions, 5): Promise<T>;
1function transformAndValidate<T extends object>( 2 classType: ClassType<T>, 3 array: object[], 4 options?: TransformValidationOptions, 5): Promise<T[]>;
Be aware that if you validate json string, the return type is a Promise
of T
or T[]
so you need to assert the returned type if you know the shape of json:
1const users = (await transformAndValidate( 2 User, 3 JSON.stringify([{ email: "test@test.test" }]), 4)) as User[];
Or you can just check the type in runtime using Array.isArray
method.
If you need sync validation, use transformAndValidateSync
function instead (available since v0.4.0). It will synchronously return T
or T[]
, not a Promise.
classType
- an class symbol, a constructor function which can be called with new
1type ClassType<T> = { 2 new (...args: any[]): T; 3};
jsonString
- a normal string containing JSON
object
- plain JS object of type object
(introduced in TypeScript 2.2), you will have compile-time error while trying to pass number, boolean, null or undefined but unfortunately run-time error when passing a function
array
- array of plain JS objects like described above
options
- optional options object, it has two optional properties
1interface TransformValidationOptions { 2 validator?: ValidatorOptions; 3 transformer?: ClassTransformOptions; 4}
You can use it to pass options for class-validator
(more info) and for class-transformer
(more info).
The class-transformer and class-validator are more powerful than it was showed in the simple usage sample, so go to their github page and check out they capabilities!
0.9.1
class-transformer
peer dependency version range to >=0.2.3
0.9.0
class-validator
peer dependency to version >=0.12.0
^3.9.5
0.8.0
class-transformer
dependency to version ^0.2.3
class-validator
dependency to version ^0.10.1
^3.6.3
0.7.1
class-transformer
dependency to version ^0.2.0
0.6.0
class-validator
dependency to version ^0.9.1
0.5.0
TransformValdiationOptions
interface (typo)class-validator
dependency to version ^0.8.1
and class-transformer
to ^0.1.9
0.4.1
TransformValdiationOptions
interface name typo (deprecate in favour of TransformValidationOptions
)0.4.0
transformAndValidateSync
function for synchronous validationPromise
of T
or T[]
class-validator
dependency to version ^0.7.2
and class-transformer
to ^0.1.7
0.3.0
class-validator
dependency to version ^0.7.1
0.2.0
object
(introduced in TS 2.2)0.1.1
Error
with message0.1.0
transformAndValidate
functionNo vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/28 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
19 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More