class-transformer-validator
Advanced tools
Comparing version 0.7.1 to 0.8.0
import { ValidatorOptions } from "class-validator"; | ||
import { ClassTransformOptions } from "class-transformer"; | ||
export interface ClassType<T> { | ||
new (...args: any[]): T; | ||
} | ||
export declare type ClassType<T> = new (...args: any[]) => T; | ||
export interface TransformValidationOptions { | ||
@@ -7,0 +5,0 @@ validator?: ValidatorOptions; |
34
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var class_validator_1 = require("class-validator"); | ||
var class_transformer_1 = require("class-transformer"); | ||
const class_validator_1 = require("class-validator"); | ||
const class_transformer_1 = require("class-transformer"); | ||
function transformAndValidate(classType, somethingToTransform, options) { | ||
return new Promise(function (resolve, reject) { | ||
var object; | ||
return new Promise((resolve, reject) => { | ||
let object; | ||
if (typeof somethingToTransform === "string") { | ||
object = JSON.parse(somethingToTransform); | ||
} | ||
else if (somethingToTransform != null && typeof somethingToTransform === "object") { | ||
else if (somethingToTransform != null && | ||
typeof somethingToTransform === "object") { | ||
object = somethingToTransform; | ||
@@ -17,10 +18,11 @@ } | ||
} | ||
var classObject = class_transformer_1.plainToClass(classType, object, options ? options.transformer : void 0); | ||
const classObject = class_transformer_1.plainToClass(classType, object, options ? options.transformer : void 0); | ||
if (Array.isArray(classObject)) { | ||
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); }); | ||
Promise.all(classObject.map(objectElement => class_validator_1.validate(objectElement, options ? options.validator : void 0))).then(errors => errors.every(error => error.length === 0) | ||
? resolve(classObject) | ||
: reject(errors)); | ||
} | ||
else { | ||
class_validator_1.validateOrReject(classObject, options ? options.validator : void 0) | ||
.then(function () { return resolve(classObject); }) | ||
.then(() => resolve(classObject)) | ||
.catch(reject); | ||
@@ -32,7 +34,8 @@ } | ||
function transformAndValidateSync(classType, somethingToTransform, options) { | ||
var object; | ||
let object; | ||
if (typeof somethingToTransform === "string") { | ||
object = JSON.parse(somethingToTransform); | ||
} | ||
else if (somethingToTransform != null && typeof somethingToTransform === "object") { | ||
else if (somethingToTransform != null && | ||
typeof somethingToTransform === "object") { | ||
object = somethingToTransform; | ||
@@ -43,6 +46,6 @@ } | ||
} | ||
var classObject = class_transformer_1.plainToClass(classType, object, options ? options.transformer : void 0); | ||
const 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; })) { | ||
const errorsArray = classObject.map(objectElement => class_validator_1.validateSync(objectElement, options ? options.validator : void 0)); | ||
if (errorsArray.some(errors => errors.length !== 0)) { | ||
throw errorsArray; | ||
@@ -53,3 +56,3 @@ } | ||
else { | ||
var errors = class_validator_1.validateSync(classObject, options ? options.validator : void 0); | ||
const errors = class_validator_1.validateSync(classObject, options ? options.validator : void 0); | ||
if (errors.length) { | ||
@@ -62,2 +65,1 @@ throw errors; | ||
exports.transformAndValidateSync = transformAndValidateSync; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "class-transformer-validator", | ||
"version": "0.7.1", | ||
"description": "A simple wrapper around class-transformer and class-validator which provides nice and programmer-friendly API.", | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"version": "0.8.0", | ||
"scripts": { | ||
"pretest": "npm run build", | ||
"test": "mocha build/tests/index.js", | ||
"build": "tsc" | ||
}, | ||
"peerDependencies": { | ||
"class-transformer": "^0.2.3", | ||
"class-validator": "^0.10.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.3", | ||
"@types/chai-as-promised": "^7.1.2", | ||
"@types/mocha": "^5.2.7", | ||
"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", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.6.3" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"{src}/**/*.ts": [ | ||
"tslint --fix", | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"author": { | ||
"name": "Michał Lytek", | ||
"url": "https://github.com/19majkel94" | ||
"url": "https://github.com/MichalLytek" | ||
}, | ||
"description": "A simple wrapper around class-transformer and class-validator which provides nice and programmer-friendly API.", | ||
"readmeFilename": "README.md", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/19majkel94/class-transformer-validator.git" | ||
"url": "https://github.com/MichalLytek/class-transformer-validator.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/19majkel94/class-transformer-validator/issues" | ||
"url": "https://github.com/MichalLytek/class-transformer-validator/issues" | ||
}, | ||
@@ -44,25 +83,3 @@ "tags": [ | ||
], | ||
"devDependencies": { | ||
"@types/chai": "^4.1.4", | ||
"@types/chai-as-promised": "7.1.0", | ||
"@types/mocha": "^5.2.5", | ||
"chai": "^4.1.2", | ||
"chai-as-promised": "^7.1.1", | ||
"class-transformer": "^0.2.0", | ||
"class-validator": "^0.9.1", | ||
"mocha": "^5.2.0", | ||
"tslint": "^5.11.0", | ||
"typescript": "~2.3.4" | ||
}, | ||
"peerDependencies": { | ||
"class-transformer": "^0.2.0", | ||
"class-validator": "^0.9.1" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
"pretest": "npm run build", | ||
"test": "mocha build/tests/index.js", | ||
"build": "tsc" | ||
}, | ||
"private": false | ||
} |
@@ -54,6 +54,6 @@ # class-transformer-validator | ||
}) | ||
.catch(error => { | ||
.catch(err => { | ||
// here you can handle error on transformation (invalid JSON) | ||
// or validation error (e.g. invalid email property) | ||
console.err(error); | ||
console.error(err); | ||
}); | ||
@@ -68,5 +68,5 @@ ``` | ||
// infered type of userObject is User, you can access all class prototype properties and methods | ||
} catch (error) { | ||
} catch (err) { | ||
// your error handling | ||
console.err(error); | ||
console.error(err); | ||
} | ||
@@ -82,3 +82,3 @@ } | ||
userObjects.forEach(user => console.log(`Hello ${user.hello()}`)); | ||
} catch (error) { | ||
} catch (err) { | ||
// your error handling | ||
@@ -145,3 +145,11 @@ } | ||
**0.7.0** | ||
**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 | ||
**0.7.1** | ||
* updated `class-transformer` dependency to version `^0.2.0` | ||
@@ -148,0 +156,0 @@ |
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
127
186
17526
14
5