Skip to content

Commit

Permalink
export default function
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza bonab committed Mar 14, 2021
1 parent 1cfe4a2 commit 044790f
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 8 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
lib/
node_modules/
62 changes: 62 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var class_validator_1 = require("class-validator");
// Define new constraint that checks the existence of sibling properties
var IsNotSiblingOfConstraint = /** @class */ (function () {
function IsNotSiblingOfConstraint() {
}
IsNotSiblingOfConstraint.prototype.validate = function (value, args) {
if (class_validator_1.isDefined(value)) {
return this.getFailedConstraints(args).length === 0;
}
return true;
};
IsNotSiblingOfConstraint.prototype.defaultMessage = function (args) {
return args.property + " cannot exist alongside the following defined properties: " + this.getFailedConstraints(args).join(', ');
};
IsNotSiblingOfConstraint.prototype.getFailedConstraints = function (args) {
return args.constraints.filter(function (prop) {
return class_validator_1.isDefined(args.object[prop]);
});
};
IsNotSiblingOfConstraint = __decorate([
class_validator_1.ValidatorConstraint({ async: false })
], IsNotSiblingOfConstraint);
return IsNotSiblingOfConstraint;
}());
// Create Decorator for the constraint that was just created
function IsNotSiblingOf(props, validationOptions) {
// eslint-disable-next-line @typescript-eslint/ban-types
return function (target, propertyName) {
class_validator_1.registerDecorator({
target: target.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: props,
validator: IsNotSiblingOfConstraint,
});
};
}
// Helper function for determining if a prop should be validated
function incompatibleSiblingsNotPresent(incompatibleSiblings) {
return function (o, v) {
return Boolean(class_validator_1.isDefined(v) || // Validate if prop has value
// Validate if all incompatible siblings are not defined
incompatibleSiblings.every(function (prop) { return !class_validator_1.isDefined(o[prop]); }));
};
}
function IncompatibleWith(incompatibleSiblings) {
var notSibling = IsNotSiblingOf(incompatibleSiblings);
var validateIf = class_validator_1.ValidateIf(incompatibleSiblingsNotPresent(incompatibleSiblings));
return function (target, key) {
notSibling(target, key);
validateIf(target, key);
};
}
exports.default = IncompatibleWith;
131 changes: 131 additions & 0 deletions lib/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var class_transformer_validator_1 = require("class-transformer-validator");
var class_validator_1 = require("class-validator");
var index_1 = __importDefault(require("./index"));
var UpdateUserDTO = /** @class */ (function () {
function UpdateUserDTO() {
}
__decorate([
class_validator_1.IsString(),
index_1.default(['deleted'])
], UpdateUserDTO.prototype, "status", void 0);
__decorate([
class_validator_1.IsBoolean(),
index_1.default(['status'])
], UpdateUserDTO.prototype, "deleted", void 0);
__decorate([
class_validator_1.IsString()
], UpdateUserDTO.prototype, "name", void 0);
return UpdateUserDTO;
}());
describe('IncompatibleWith decorator', function () {
it('throws if sibling is defined in the object', function () { return __awaiter(void 0, void 0, void 0, function () {
var obj;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
obj = {
status: 'test',
deleted: true,
name: 'john',
};
return [4 /*yield*/, expect(class_transformer_validator_1.transformAndValidate(UpdateUserDTO, obj)).rejects.toEqual([
{
children: [],
constraints: {
IsNotSiblingOfConstraint: 'status cannot exist alongside the following defined properties: deleted',
},
property: 'status',
target: { deleted: true, name: 'john', status: 'test' },
value: 'test',
},
{
children: [],
constraints: {
IsNotSiblingOfConstraint: 'deleted cannot exist alongside the following defined properties: status',
},
property: 'deleted',
target: { deleted: true, name: 'john', status: 'test' },
value: true,
},
])];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
it('returns the class if one of keys exist on the object', function () { return __awaiter(void 0, void 0, void 0, function () {
var objWithoutStatus, result, objWithoutDeleted;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
objWithoutStatus = {
deleted: true,
name: 'john',
};
return [4 /*yield*/, class_transformer_validator_1.transformAndValidate(UpdateUserDTO, objWithoutStatus)];
case 1:
result = _a.sent();
expect(result).toEqual(objWithoutStatus);
expect(result).toBeInstanceOf(UpdateUserDTO);
objWithoutDeleted = {
status: 'test',
name: 'john',
};
return [4 /*yield*/, class_transformer_validator_1.transformAndValidate(UpdateUserDTO, objWithoutDeleted)];
case 2:
result = _a.sent();
expect(result).toEqual(objWithoutDeleted);
expect(result).toBeInstanceOf(UpdateUserDTO);
return [2 /*return*/];
}
});
}); });
});
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "incompatible-with",
"version": "1.0.5",
"name": "incompatiblewith",
"version": "1.0.6",
"description": "InCompatibleWith is a decorator for class-validator that checks if siblings do not exist on the object.",
"main": "index.js",
"main": "./lib/index.js",
"scripts": {
"test": "jest",
"build": "rimraf lib && tsc"
Expand All @@ -11,7 +11,11 @@
"type": "git",
"url": "git+https://github.com/alireza-bonab/class-validator-is-compatible-with.git"
},
"keywords": [],
"keywords": [
"typescript",
"class-validator",
"class-transformer"
],
"author": "",
"license": "ISC",
"bugs": {
Expand All @@ -31,4 +35,4 @@
"class-transformer-validator": "^0.9.1",
"class-validator": "^0.13.1"
}
}
}
2 changes: 1 addition & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { transformAndValidate } from 'class-transformer-validator';
import { IsString, IsBoolean } from 'class-validator';
import { IncompatibleWith } from './index';
import IncompatibleWith from './index';

class UpdateUserDTO {
@IsString()
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function incompatibleSiblingsNotPresent(incompatibleSiblings: string[]) {
};
}

export function IncompatibleWith(incompatibleSiblings: string[]) {
export default function IncompatibleWith(incompatibleSiblings: string[]) {
const notSibling = IsNotSiblingOf(incompatibleSiblings);
const validateIf = ValidateIf(
incompatibleSiblingsNotPresent(incompatibleSiblings),
Expand Down

0 comments on commit 044790f

Please sign in to comment.