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.2.0 to 0.3.0

1

index.d.ts

@@ -11,2 +11,3 @@ import { ValidatorOptions } from "class-validator";

export declare function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T>;
export declare function transformAndValidate<T extends object>(classType: ClassType<T>, array: object[], options?: TransformValdiationOptions): Promise<T[]>;
export declare function transformAndValidate<T extends object>(classType: ClassType<T>, object: object, options?: TransformValdiationOptions): Promise<T>;

16

index.js

@@ -11,12 +11,18 @@ "use strict";

}
else if (typeof somethingToTransform === "object" && somethingToTransform !== null && !Array.isArray(somethingToTransform)) {
else if (somethingToTransform != null && typeof somethingToTransform === "object") {
object = somethingToTransform;
}
else {
return reject(new Error("Incorrect object param type! Only strings and plain objects are valid."));
return reject(new Error("Incorrect object param type! Only string, plain object and array of plain objects are valid."));
}
var classObject = class_transformer_1.plainToClass(classType, object, options ? options.transformer : void 0);
class_validator_1.validateOrReject(classObject, options ? options.validator : void 0)
.then(function () { return resolve(classObject); })
.catch(reject);
if (Array.isArray(classObject)) {
Promise.all(classObject.map(function (object) { return class_validator_1.validate(object, options ? options.validator : void 0); }))
.then(function (errors) { return errors.every(function (error) { return error.length == 0; }) ? resolve(classObject) : reject(errors); });
}
else {
class_validator_1.validateOrReject(classObject, options ? options.validator : void 0)
.then(function () { return resolve(classObject); })
.catch(reject);
}
});

@@ -23,0 +29,0 @@ }

{
"name": "class-transformer-validator",
"version": "0.2.0",
"version": "0.3.0",
"description": "A simple wrapper around class-transformer and class-validator which provides nice and programmer-friendly API.",

@@ -45,3 +45,3 @@ "license": "MIT",

"peerDependencies": {
"class-validator": "^0.6.8",
"class-validator": "^0.7.1",
"class-transformer": "^0.1.6"

@@ -48,0 +48,0 @@ },

# class-transformer-validator
[![npm version](https://badge.fury.io/js/class-transformer-validator.svg)](https://badge.fury.io/js/class-transformer-validator)
[![Dependency Status](https://david-dm.org/19majkel94/class-transformer-validator.svg)](https://david-dm.org/19majkel94/class-transformer-validator)
[![devDependency Status](https://david-dm.org/19majkel94/class-transformer-validator/dev-status.svg)](https://david-dm.org/19majkel94/class-transformer-validator#info=devDependencies)
[![peerDependency Status](https://david-dm.org/19majkel94/class-transformer-validator/peer-status.svg)](https://david-dm.org/19majkel94/class-transformer-validator#info=devDependencies)
A simple plugin for [class-transformer](https://github.com/pleerock/class-transformer) and [class-validator](https://github.com/pleerock/class-validator) which combines them in a nice and programmer-friendly API.

@@ -11,2 +16,6 @@

(or the short way):
`npm i -S class-transformer-validator`
#### Peer dependencies

@@ -23,3 +32,3 @@

```typescript
```js
import { IsEmail } from 'class-validator';

@@ -54,3 +63,3 @@ import { transformAndValidate } from "class-transformer-validator";

You can also transform and validate plain JS object (e.g. from express req.body). Using ES7 async/await syntax:
```typescript
```js
async (req, res) => {

@@ -67,2 +76,14 @@ try {

```
And since release `0.3.0` you can also pass array of objects - all of them will be validated using given class validation constraints:
```js
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 (error) {
// your error handling
}
}
```

@@ -73,15 +94,19 @@ ## API reference

There is available one function with two overloads:
```typescript
There is available one function with three overloads:
```js
function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T>;
```
```typescript
```js
function transformAndValidate<T extends object>(classType: ClassType<T>, object: object, options?: TransformValdiationOptions): Promise<T>;
```
```js
function transformAndValidate<T extends object>(classType: ClassType<T>, array: object[], options?: TransformValdiationOptions): Promise<T[]>;
```
#### Parameters and types
- `classType` - an class symbol, a constructor function which can be called with `new`
```typescript
```js
type ClassType<T> = {

@@ -93,6 +118,8 @@ new (...args: any[]): T;

- `object` - plain JS object of type `object` (introduced in TypeScript 2.1), you will have compile-time error while trying to pass number, boolean, null or undefined but unfortunately run-time error when passing a function or an array
- `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
```typescript
```js
interface TransformValdiationOptions {

@@ -111,2 +138,7 @@ validator?: ValidatorOptions;

**0.3.0**
* added support for transform and validate array of objects given class
* bumped `class-validator` dependency to `^0.7.1`
**0.2.0**

@@ -113,0 +145,0 @@

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