class-transformer-validator
Advanced tools
Comparing version 0.3.0 to 0.4.0
import { ValidatorOptions } from "class-validator"; | ||
import { ClassTransformOptions } from "class-transformer"; | ||
export declare type ClassType<T> = { | ||
export interface ClassType<T> { | ||
new (...args: any[]): T; | ||
}; | ||
} | ||
export interface TransformValdiationOptions { | ||
@@ -10,4 +10,61 @@ validator?: ValidatorOptions; | ||
} | ||
export declare function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T>; | ||
/** | ||
* Asynchronously converts JSON string to class (constructor) object. | ||
* Reject the promise if the object parsed from string doesn't pass validation. | ||
* | ||
* @param {ClassType<T>} classType The Class to parse and convert JSON to | ||
* @param {string} jsonString The string containing JSON | ||
* @param {TransformValdiationOptions} [options] Optional options object for class-validator and class-transformer | ||
* @returns {Promise<T|T[]>} Promise of object of given class T or array of objects given class T | ||
*/ | ||
export declare function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T | T[]>; | ||
/** | ||
* Asynchronously converts array of plain objects to array of class (constructor) objects. | ||
* Reject the promise if any of the objects in array doesn't pass validation. | ||
* | ||
* @param {ClassType<T>} classType The Class to convert object to | ||
* @param {object} array The array of objects to instantiate and validate | ||
* @param {TransformValdiationOptions} [options] Optional options object for class-validator and class-transformer | ||
* @returns {Promise<T>} Promise of object of given class T | ||
*/ | ||
export declare function transformAndValidate<T extends object>(classType: ClassType<T>, array: object[], options?: TransformValdiationOptions): Promise<T[]>; | ||
/** | ||
* Asynchronously converts plain object to class (constructor) object. | ||
* Reject the promise if the object doesn't pass validation. | ||
* | ||
* @param {ClassType<T>} classType The Class to convert object to | ||
* @param {object} object The object to instantiate and validate | ||
* @param {TransformValdiationOptions} [options] Optional options object for class-validator and class-transformer | ||
* @returns {Promise<T>} Promise of object of given class T | ||
*/ | ||
export declare function transformAndValidate<T extends object>(classType: ClassType<T>, object: object, options?: TransformValdiationOptions): Promise<T>; | ||
/** | ||
* Synchronously converts JSON string to class (constructor) object. | ||
* Throws error if the object parsed from string doesn't pass validation. | ||
* | ||
* @param {ClassType<T>} classType The Class to parse and convert JSON to | ||
* @param {string} jsonString The string containing JSON | ||
* @param {TransformValdiationOptions} [options] Optional options object for class-validator and class-transformer | ||
* @returns {T|T[]} Object of given class T or array of objects given class T | ||
*/ | ||
export declare function transformAndValidateSync<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): T | T[]; | ||
/** | ||
* Synchronously converts array of plain objects to array of class (constructor) objects. | ||
* Throws error if any of the objects in array doesn't pass validation. | ||
* | ||
* @param {ClassType<T>} classType The Class to convert object to | ||
* @param {object} array The array of objects to instantiate and validate | ||
* @param {TransformValdiationOptions} [options] Optional options object for class-validator and class-transformer | ||
* @returns {T[]} Array of objects of given class T | ||
*/ | ||
export declare function transformAndValidateSync<T extends object>(classType: ClassType<T>, array: object[], options?: TransformValdiationOptions): T[]; | ||
/** | ||
* Synchronously converts plain object to class (constructor) object. | ||
* Throws error if the object doesn't pass validation. | ||
* | ||
* @param {ClassType<T>} classType The Class to convert object to | ||
* @param {object} object The object to instantiate and validate | ||
* @param {TransformValdiationOptions} [options] Optional options object for class-validator and class-transformer | ||
* @returns {T} Object of given class T | ||
*/ | ||
export declare function transformAndValidateSync<T extends object>(classType: ClassType<T>, object: object, options?: TransformValdiationOptions): T; |
32
index.js
@@ -19,4 +19,4 @@ "use strict"; | ||
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); }); | ||
Promise.all(classObject.map(function (objectElement) { return class_validator_1.validate(objectElement, options ? options.validator : void 0); })) | ||
.then(function (errors) { return errors.every(function (error) { return error.length === 0; }) ? resolve(classObject) : reject(errors); }); | ||
} | ||
@@ -31,2 +31,30 @@ else { | ||
exports.transformAndValidate = transformAndValidate; | ||
function transformAndValidateSync(classType, somethingToTransform, options) { | ||
var object; | ||
if (typeof somethingToTransform === "string") { | ||
object = JSON.parse(somethingToTransform); | ||
} | ||
else if (somethingToTransform != null && typeof somethingToTransform === "object") { | ||
object = somethingToTransform; | ||
} | ||
else { | ||
throw 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); | ||
if (Array.isArray(classObject)) { | ||
var errorsArray = classObject.map(function (objectElement) { return class_validator_1.validateSync(objectElement, options ? options.validator : void 0); }); | ||
if (errorsArray.some(function (errors) { return errors.length !== 0; })) { | ||
throw errorsArray; | ||
} | ||
return classObject; | ||
} | ||
else { | ||
var errors = class_validator_1.validateSync(classObject, options ? options.validator : void 0); | ||
if (errors.length) { | ||
throw errors; | ||
} | ||
return classObject; | ||
} | ||
} | ||
exports.transformAndValidateSync = transformAndValidateSync; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "class-transformer-validator", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A simple wrapper around class-transformer and class-validator which provides nice and programmer-friendly API.", | ||
@@ -44,5 +44,17 @@ "license": "MIT", | ||
], | ||
"devDependencies": { | ||
"@types/chai": "^4.0.4", | ||
"@types/chai-as-promised": "7.1.0", | ||
"@types/mocha": "^2.2.43", | ||
"chai": "^4.1.2", | ||
"chai-as-promised": "^7.1.1", | ||
"class-transformer": "^0.1.7", | ||
"class-validator": "^0.7.2", | ||
"mocha": "^3.5.3", | ||
"tslint": "^5.7.0", | ||
"typescript": "^2.3.4" | ||
}, | ||
"peerDependencies": { | ||
"class-validator": "^0.7.1", | ||
"class-transformer": "^0.1.6" | ||
"class-validator": "^0.7.2", | ||
"class-transformer": "^0.1.7" | ||
}, | ||
@@ -49,0 +61,0 @@ "main": "index.js", |
@@ -90,5 +90,5 @@ # class-transformer-validator | ||
There is available one function with three overloads: | ||
There is available the `transformAndValidate` function with three overloads: | ||
```js | ||
function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T>; | ||
function transformAndValidate<T extends object>(classType: ClassType<T>, jsonString: string, options?: TransformValdiationOptions): Promise<T|T[]>; | ||
``` | ||
@@ -104,2 +104,10 @@ | ||
If you need sync validation, use `transformAndValidateSync` function instead (since v0.4.0). | ||
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: | ||
```js | ||
const users = transformAndValidate(User, JSON.stringify([{ email: "test@test.test" }])) as User[]; | ||
``` | ||
Or you can just check in runtime using `Array.isArray(obj)` method. | ||
#### Parameters and types | ||
@@ -134,6 +142,12 @@ | ||
**0.4.0** | ||
* added `transformAndValidateSync` function for synchronous validation | ||
* changed return type for transform and validation JSON to `Promise` of `T` or `T[]` | ||
* bumped `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 | ||
* bumped `class-validator` dependency to `^0.7.1` | ||
* bumped `class-validator` dependency to version `^0.7.1` | ||
@@ -140,0 +154,0 @@ **0.2.0** |
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
18834
6
126
163
10