Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

class-transformer-validator

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

class-transformer-validator - npm Package Compare versions

Comparing version 0.8.0 to 0.9.0

0

index.d.ts

@@ -0,0 +0,0 @@ import { ValidatorOptions } from "class-validator";

1

index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformAndValidateSync = exports.transformAndValidate = void 0;
const class_validator_1 = require("class-validator");

@@ -4,0 +5,0 @@ const class_transformer_1 = require("class-transformer");

22

package.json
{
"name": "class-transformer-validator",
"version": "0.8.0",
"version": "0.9.0",
"scripts": {

@@ -11,19 +11,19 @@ "pretest": "npm run build",

"class-transformer": "^0.2.3",
"class-validator": "^0.10.1"
"class-validator": ">=0.12.0"
},
"devDependencies": {
"@types/chai": "^4.2.3",
"@types/chai": "^4.2.11",
"@types/chai-as-promised": "^7.1.2",
"@types/mocha": "^5.2.7",
"@types/mocha": "^7.0.2",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"class-transformer": "^0.2.3",
"class-validator": "^0.10.1",
"husky": "^3.0.8",
"lint-staged": "^9.4.1",
"mocha": "^6.2.1",
"prettier": "^1.18.2",
"tslint": "^5.20.0",
"class-validator": "^0.12.2",
"husky": "^4.2.5",
"lint-staged": "^10.2.9",
"mocha": "^7.2.0",
"prettier": "^2.0.5",
"tslint": "^6.1.2",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.6.3"
"typescript": "^3.9.5"
},

@@ -30,0 +30,0 @@ "husky": {

@@ -32,3 +32,3 @@ # class-transformer-validator

```ts
import { IsEmail } from 'class-validator';
import { IsEmail } from "class-validator";
import { transformAndValidate } from "class-transformer-validator";

@@ -38,49 +38,53 @@

class User {
@IsEmail()
public email: string;
@IsEmail()
public email: string;
public hello(): string {
return "World!";
}
public hello(): string {
return "World!";
}
}
// then load the JSON string from any part of your app
const userJson: string = loadJsonFromSomething();
const userJson: string = loadJsonFromSomething();
// transform the JSON to class instance and validate it correctness
transformAndValidate(User, userJson)
.then((userObject: User) => {
// now you can access all your class prototype method
console.log(`Hello ${userObject.hello()}`); // prints "Hello World!" on console
})
.catch(err => {
// here you can handle error on transformation (invalid JSON)
// or validation error (e.g. invalid email property)
console.error(err);
});
.then((userObject: User) => {
// now you can access all your class prototype method
console.log(`Hello ${userObject.hello()}`); // prints "Hello World!" on console
})
.catch(err => {
// here you can handle error on transformation (invalid JSON)
// or validation error (e.g. invalid email property)
console.error(err);
});
```
You can also transform and validate plain JS object (e.g. from express req.body). Using ES7 async/await syntax:
```ts
async (req, res) => {
try {
// transform and validate request body
const userObject = await transformAndValidate(User, req.body);
// infered type of userObject is User, you can access all class prototype properties and methods
} catch (err) {
// your error handling
console.error(err);
}
}
try {
// transform and validate request body
const userObject = await transformAndValidate(User, req.body);
// infered type of userObject is User, you can access all class prototype properties and methods
} catch (err) {
// your error handling
console.error(err);
}
};
```
And since release `0.3.0` you can also pass array of objects - all of them will be validated using given class validation constraints:
```ts
async (req, res) => {
try {
// transform and validate request body - array of User objects
const userObjects = await transformAndValidate(User, req.body);
userObjects.forEach(user => console.log(`Hello ${user.hello()}`));
} catch (err) {
// your error handling
}
}
try {
// transform and validate request body - array of User objects
const userObjects = await transformAndValidate(User, req.body);
userObjects.forEach(user => console.log(`Hello ${user.hello()}`));
} catch (err) {
// your error handling
}
};
```

@@ -93,18 +97,36 @@

There is available the `transformAndValidate` function with three overloads:
```ts
function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValidationOptions): Promise<T|T[]>;
function transformAndValidate<T extends object>(
classType: ClassType<T>,
jsonString: string,
options?: TransformValidationOptions,
): Promise<T | T[]>;
```
```ts
function transformAndValidate<T extends object>(classType: ClassType<T>, object: object, options?: TransformValidationOptions): Promise<T>;
function transformAndValidate<T extends object>(
classType: ClassType<T>,
object: object,
options?: TransformValidationOptions,
): Promise<T>;
```
```ts
function transformAndValidate<T extends object>(classType: ClassType<T>, array: object[], options?: TransformValidationOptions): Promise<T[]>;
function transformAndValidate<T extends object>(
classType: ClassType<T>,
array: object[],
options?: TransformValidationOptions,
): 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:
```ts
const users = await transformAndValidate(User, JSON.stringify([{ email: "test@test.test" }])) as User[];
const users = (await transformAndValidate(
User,
JSON.stringify([{ email: "test@test.test" }]),
)) as User[];
```
Or you can just check the type in runtime using `Array.isArray` method.

@@ -119,7 +141,9 @@

- `classType` - an class symbol, a constructor function which can be called with `new`
```ts
type ClassType<T> = {
new (...args: any[]): T;
}
type ClassType<T> = {
new (...args: any[]): T;
};
```
- `jsonString` - a normal string containing JSON

@@ -132,8 +156,10 @@

- `options` - optional options object, it has two optional properties
```ts
interface TransformValidationOptions {
validator?: ValidatorOptions;
transformer?: ClassTransformOptions;
validator?: ValidatorOptions;
transformer?: ClassTransformOptions;
}
```
You can use it to pass options for `class-validator` ([more info](https://github.com/pleerock/class-validator/blob/master/src/validation/ValidatorOptions.ts)) and for `class-transformer` ([more info](https://github.com/pleerock/class-transformer/blob/master/src/ClassTransformOptions.ts)).

@@ -147,45 +173,55 @@

**0.9.0**
- bump `class-validator` peer dependency to version `>=0.12.0`
- updated TypeScript dependency to version `^3.9.5`
- updated all dev dependencies
**0.8.0**
* updated `class-transformer` dependency to version `^0.2.3`
* updated `class-validator` dependency to version `^0.10.1`
* updated TypeScript dependency to version `^3.6.3`
* built code is now emitted as ES2015 (dropped es5 support)
* updated all dev dependencies
- updated `class-transformer` dependency to version `^0.2.3`
- updated `class-validator` dependency to version `^0.10.1`
- updated TypeScript dependency to version `^3.6.3`
- built code is now emitted as ES2015 (dropped es5 support)
- updated all dev dependencies
**0.7.1**
* updated `class-transformer` dependency to version `^0.2.0`
- updated `class-transformer` dependency to version `^0.2.0`
**0.6.0**
* updated `class-validator` dependency to version `^0.9.1`
- updated `class-validator` dependency to version `^0.9.1`
**0.5.0**
* remove deprecated `TransformValdiationOptions` interface (typo)
* updated `class-validator` dependency to version `^0.8.1` and `class-transformer` to `^0.1.9`
- remove deprecated `TransformValdiationOptions` interface (typo)
- updated `class-validator` dependency to version `^0.8.1` and `class-transformer` to `^0.1.9`
**0.4.1**
* fix `TransformValdiationOptions` interface name typo (deprecate in favour of `TransformValidationOptions`)
- fix `TransformValdiationOptions` interface name typo (deprecate in favour of `TransformValidationOptions`)
**0.4.0**
* added `transformAndValidateSync` function for synchronous validation
* changed return type for JSON's transform and validation to `Promise` of `T` or `T[]`
* updated `class-validator` dependency to version `^0.7.2` and `class-transformer` to `^0.1.7`
- added `transformAndValidateSync` function for synchronous validation
- changed return type for JSON's transform and validation to `Promise` of `T` or `T[]`
- updated `class-validator` dependency to version `^0.7.2` and `class-transformer` to `^0.1.7`
**0.3.0**
* added support for transform and validate array of objects given class
* updated `class-validator` dependency to version `^0.7.1`
- added support for transform and validate array of objects given class
- updated `class-validator` dependency to version `^0.7.1`
**0.2.0**
* changed object parameter type declaration to `object` (introduced in TS 2.2)
* throwing error when passed array, undefined or null
- changed object parameter type declaration to `object` (introduced in TS 2.2)
- throwing error when passed array, undefined or null
**0.1.1**
* changed throwing error (rejecting promise) [from string to `Error` with message](https://github.com/19majkel94/class-transformer-validator/commit/e0ed33f9f8feb58d52bfdbc78f8150cdfd0ebe77#diff-f41e9d04a45c83f3b6f6e630f10117feR39)
- changed throwing error (rejecting promise) [from string to `Error` with message](https://github.com/19majkel94/class-transformer-validator/commit/e0ed33f9f8feb58d52bfdbc78f8150cdfd0ebe77#diff-f41e9d04a45c83f3b6f6e630f10117feR39)
**0.1.0**
* initial version with `transformAndValidate` function
- initial version with `transformAndValidate` function

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc