class-transformer-validator
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -1,2 +0,1 @@ | ||
/// <reference types="chai" /> | ||
import { ValidatorOptions } from "class-validator"; | ||
@@ -7,5 +6,2 @@ import { ClassTransformOptions } from "class-transformer"; | ||
}; | ||
export declare type PlainObject = { | ||
[property: string]: any; | ||
}; | ||
export interface TransformValdiationOptions { | ||
@@ -15,3 +11,3 @@ validator?: ValidatorOptions; | ||
} | ||
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>, object: PlainObject, options?: TransformValdiationOptions): Promise<T>; | ||
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>, object: object, options?: TransformValdiationOptions): Promise<T>; |
15
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var class_validator_1 = require("class-validator"); | ||
var class_transformer_1 = require("class-transformer"); | ||
function transformAndValidate(classType, objectOrString, options) { | ||
function transformAndValidate(classType, somethingToTransform, options) { | ||
return new Promise(function (resolve, reject) { | ||
var object; | ||
if (typeof objectOrString === "string") { | ||
object = JSON.parse(objectOrString); | ||
if (typeof somethingToTransform === "string") { | ||
object = JSON.parse(somethingToTransform); | ||
} | ||
else if (typeof objectOrString === "object") { | ||
object = objectOrString; | ||
else if (typeof somethingToTransform === "object" && somethingToTransform !== null && !Array.isArray(somethingToTransform)) { | ||
object = somethingToTransform; | ||
} | ||
@@ -16,4 +17,4 @@ else { | ||
} | ||
var classObject = class_transformer_1.plainToClass(classType, object, options ? options.transformer : undefined); | ||
class_validator_1.validateOrReject(classObject, options ? options.validator : undefined) | ||
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); }) | ||
@@ -20,0 +21,0 @@ .catch(reject); |
{ | ||
"name": "class-transformer-validator", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A simple wrapper around class-transformer and class-validator which provides nice and programmer-friendly API.", | ||
@@ -31,2 +31,15 @@ "license": "MIT", | ||
], | ||
"keywords": [ | ||
"class-validator", | ||
"validator", | ||
"validation", | ||
"typescript-validator", | ||
"class-transformer", | ||
"serialization", | ||
"deserialization", | ||
"serializer", | ||
"typescript", | ||
"object-to-class", | ||
"typescript-serializer" | ||
], | ||
"peerDependencies": { | ||
@@ -37,3 +50,8 @@ "class-validator": "^0.6.8", | ||
"main": "index.js", | ||
"scripts": { | ||
"pretest": "npm run build", | ||
"test": "mocha build/tests/index.js", | ||
"build": "tsc" | ||
}, | ||
"private": false | ||
} |
@@ -71,7 +71,7 @@ # class-transformer-validator | ||
```typescript | ||
function transformAndValidate<T extends PlainObject>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T>; | ||
function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T>; | ||
``` | ||
```typescript | ||
function transformAndValidate<T extends PlainObject>(classType: ClassType<T>, object: PlainObject, options?: TransformValdiationOptions): Promise<T>; | ||
function transformAndValidate<T extends object>(classType: ClassType<T>, object: object, options?: TransformValdiationOptions): Promise<T>; | ||
``` | ||
@@ -89,8 +89,3 @@ | ||
- `object` - plain JS object with some properties (not empty - `{}`). `PlainObject` is a defined as normal JS object type but with some properties defined, to provide compile-time error when e.g. number is passed as parameter: | ||
```typescript | ||
type PlainObject = { | ||
[property: string]: any; | ||
} | ||
``` | ||
- `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 | ||
@@ -109,1 +104,16 @@ - `options` - optional options object, it has two optional properties | ||
The [class-transformer](https://github.com/pleerock/class-transformer) and [class-validator](https://github.com/pleerock/class-validator) are more powerfull than it was showed in the simple usage sample, so go to their github page and check out they capabilities! | ||
## Release notes | ||
**0.2.0** | ||
* 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) | ||
**0.1.0** | ||
* initial version with `transformAndValidate` function |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9569
117
34