rokot-validate
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -10,11 +10,40 @@ "use strict"; | ||
}; | ||
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; | ||
return { next: verb(0), "throw": verb(1), "return": verb(2) }; | ||
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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, 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 }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const validateJsWithCustomValidators_1 = require("./validateJsWithCustomValidators"); | ||
class ClientConstraintBuilder { | ||
array(arrayConstraint, extend) { | ||
var validateJsWithCustomValidators_1 = require("./validateJsWithCustomValidators"); | ||
var ClientConstraintBuilder = (function () { | ||
function ClientConstraintBuilder() { | ||
} | ||
ClientConstraintBuilder.prototype.array = function (arrayConstraint, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
array: arrayConstraint || true | ||
}, extend); | ||
} | ||
arrayMandatory(arrayConstraint, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.arrayMandatory = function (arrayConstraint, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -24,4 +53,4 @@ array: arrayConstraint || true, | ||
}, extend); | ||
} | ||
arrayValidator(spec, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.arrayValidator = function (spec, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -31,4 +60,4 @@ array: true, | ||
}, extend); | ||
} | ||
arrayValidatorMandatory(spec, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.arrayValidatorMandatory = function (spec, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -39,9 +68,9 @@ array: true, | ||
}, extend); | ||
} | ||
itemValidator(spec, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.itemValidator = function (spec, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
spec: spec | ||
}, extend); | ||
} | ||
itemValidatorMandatory(spec, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.itemValidatorMandatory = function (spec, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -51,4 +80,4 @@ presence: true, | ||
}, extend); | ||
} | ||
integerStrict(numericality, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.integerStrict = function (numericality, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -60,4 +89,4 @@ numericality: validateJsWithCustomValidators_1.validate.extend({ | ||
}, extend); | ||
} | ||
integerStrictMandatory(numericality, extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.integerStrictMandatory = function (numericality, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -70,9 +99,9 @@ presence: true, | ||
}, extend); | ||
} | ||
string(extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.string = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
string: true | ||
}, extend); | ||
} | ||
stringMandatory(extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.stringMandatory = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -82,4 +111,4 @@ presence: true, | ||
}, extend); | ||
} | ||
booleanMandatory(extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.booleanMandatory = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -89,17 +118,20 @@ presence: true, | ||
}, extend); | ||
} | ||
boolean(extend) { | ||
}; | ||
ClientConstraintBuilder.prototype.boolean = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
boolean: true | ||
}, extend); | ||
}; | ||
return ClientConstraintBuilder; | ||
}()); | ||
exports.ClientConstraintBuilder = ClientConstraintBuilder; | ||
var ConstraintBuilder = (function () { | ||
function ConstraintBuilder() { | ||
} | ||
} | ||
exports.ClientConstraintBuilder = ClientConstraintBuilder; | ||
class ConstraintBuilder { | ||
array(arrayConstraint, extend) { | ||
ConstraintBuilder.prototype.array = function (arrayConstraint, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
array: arrayConstraint || true | ||
}, extend); | ||
} | ||
arrayMandatory(arrayConstraint, extend) { | ||
}; | ||
ConstraintBuilder.prototype.arrayMandatory = function (arrayConstraint, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -109,4 +141,4 @@ array: arrayConstraint || true, | ||
}, extend); | ||
} | ||
arrayValidator(itemValidator, extend) { | ||
}; | ||
ConstraintBuilder.prototype.arrayValidator = function (itemValidator, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -116,4 +148,4 @@ array: true, | ||
}, extend); | ||
} | ||
arrayValidatorMandatory(itemValidator, extend) { | ||
}; | ||
ConstraintBuilder.prototype.arrayValidatorMandatory = function (itemValidator, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -124,9 +156,9 @@ array: true, | ||
}, extend); | ||
} | ||
itemValidator(itemValidator, extend) { | ||
}; | ||
ConstraintBuilder.prototype.itemValidator = function (itemValidator, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
validator: itemValidator | ||
}, extend); | ||
} | ||
itemValidatorMandatory(itemValidator, extend) { | ||
}; | ||
ConstraintBuilder.prototype.itemValidatorMandatory = function (itemValidator, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -136,4 +168,4 @@ presence: true, | ||
}, extend); | ||
} | ||
integerStrict(numericality, extend) { | ||
}; | ||
ConstraintBuilder.prototype.integerStrict = function (numericality, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -145,4 +177,4 @@ numericality: validateJsWithCustomValidators_1.validate.extend({ | ||
}, extend); | ||
} | ||
integerStrictMandatory(numericality, extend) { | ||
}; | ||
ConstraintBuilder.prototype.integerStrictMandatory = function (numericality, extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -155,9 +187,9 @@ presence: true, | ||
}, extend); | ||
} | ||
string(extend) { | ||
}; | ||
ConstraintBuilder.prototype.string = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
string: true | ||
}, extend); | ||
} | ||
stringMandatory(extend) { | ||
}; | ||
ConstraintBuilder.prototype.stringMandatory = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -167,4 +199,4 @@ presence: true, | ||
}, extend); | ||
} | ||
booleanMandatory(extend) { | ||
}; | ||
ConstraintBuilder.prototype.booleanMandatory = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
@@ -174,26 +206,50 @@ presence: true, | ||
}, extend); | ||
} | ||
boolean(extend) { | ||
}; | ||
ConstraintBuilder.prototype.boolean = function (extend) { | ||
return validateJsWithCustomValidators_1.validate.extend({ | ||
boolean: true | ||
}, extend); | ||
} | ||
mustValidator(validator) { | ||
return (value) => __awaiter(this, void 0, void 0, function* () { | ||
if (validateJsWithCustomValidators_1.validate.isEmpty(value)) { | ||
return true; | ||
} | ||
if (!validateJsWithCustomValidators_1.validate.isArray(value)) { | ||
return validator(value); | ||
} | ||
for (let i = 0; i < value.length; i++) { | ||
let validated = validator(value[i]); | ||
let result = validateJsWithCustomValidators_1.isPromise(validated) ? yield validated : validated; | ||
if (result === false || validateJsWithCustomValidators_1.validate.isString(result) || validateJsWithCustomValidators_1.validate.isArray(result)) { | ||
return result; | ||
}; | ||
ConstraintBuilder.prototype.mustValidator = function (validator) { | ||
var _this = this; | ||
return function (value) { return __awaiter(_this, void 0, void 0, function () { | ||
var i, validated, result, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (validateJsWithCustomValidators_1.validate.isEmpty(value)) { | ||
return [2 /*return*/, true]; | ||
} | ||
if (!validateJsWithCustomValidators_1.validate.isArray(value)) { | ||
return [2 /*return*/, validator(value)]; | ||
} | ||
i = 0; | ||
_b.label = 1; | ||
case 1: | ||
if (!(i < value.length)) return [3 /*break*/, 6]; | ||
validated = validator(value[i]); | ||
if (!validateJsWithCustomValidators_1.isPromise(validated)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, validated]; | ||
case 2: | ||
_a = _b.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
_a = validated; | ||
_b.label = 4; | ||
case 4: | ||
result = _a; | ||
if (result === false || validateJsWithCustomValidators_1.validate.isString(result) || validateJsWithCustomValidators_1.validate.isArray(result)) { | ||
return [2 /*return*/, result]; | ||
} | ||
_b.label = 5; | ||
case 5: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
}); }; | ||
}; | ||
return ConstraintBuilder; | ||
}()); | ||
exports.ConstraintBuilder = ConstraintBuilder; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -10,25 +20,34 @@ var Errors; | ||
})(ErrorType = Errors.ErrorType || (Errors.ErrorType = {})); | ||
class CustomError extends Error { | ||
constructor(type, message, internalMessage) { | ||
super(); | ||
this.internalMessage = internalMessage; | ||
this.name = ErrorType[type]; | ||
this.message = message; | ||
var CustomError = (function (_super) { | ||
__extends(CustomError, _super); | ||
function CustomError(type, message, internalMessage) { | ||
var _this = _super.call(this) || this; | ||
_this.internalMessage = internalMessage; | ||
_this.name = ErrorType[type]; | ||
_this.message = message; | ||
return _this; | ||
} | ||
} | ||
return CustomError; | ||
}(Error)); | ||
Errors.CustomError = CustomError; | ||
class ValidationError extends CustomError { | ||
constructor(errors) { | ||
super(ErrorType.ValidationError, "Validation error"); | ||
this.errors = errors; | ||
Error.captureStackTrace(this, this.constructor); | ||
var ValidationError = (function (_super) { | ||
__extends(ValidationError, _super); | ||
function ValidationError(errors) { | ||
var _this = _super.call(this, ErrorType.ValidationError, "Validation error") || this; | ||
_this.errors = errors; | ||
Error.captureStackTrace(_this, _this.constructor); | ||
return _this; | ||
} | ||
} | ||
return ValidationError; | ||
}(CustomError)); | ||
Errors.ValidationError = ValidationError; | ||
class NoValidationInputError extends CustomError { | ||
constructor() { | ||
super(ErrorType.NoInputError, "No input provided"); | ||
Error.captureStackTrace(this, this.constructor); | ||
var NoValidationInputError = (function (_super) { | ||
__extends(NoValidationInputError, _super); | ||
function NoValidationInputError() { | ||
var _this = _super.call(this, ErrorType.NoInputError, "No input provided") || this; | ||
Error.captureStackTrace(_this, _this.constructor); | ||
return _this; | ||
} | ||
} | ||
return NoValidationInputError; | ||
}(CustomError)); | ||
Errors.NoValidationInputError = NoValidationInputError; | ||
@@ -35,0 +54,0 @@ })(Errors = exports.Errors || (exports.Errors = {})); |
@@ -10,10 +10,38 @@ "use strict"; | ||
}; | ||
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; | ||
return { next: verb(0), "throw": verb(1), "return": verb(2) }; | ||
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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, 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 _this = this; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const moment = require("moment"); | ||
const validate = require("validate.js"); | ||
var moment = require("moment"); | ||
var validate = require("validate.js"); | ||
exports.validate = validate; | ||
require("validate.js"); | ||
validate.Promise = Promise; | ||
validate.error = () => { }; | ||
validate.validators.string = (value, options, key, attributes) => { | ||
validate.error = function () { }; | ||
validate.validators.string = function (value, options, key, attributes) { | ||
if (validate.isDefined(value) && !validate.isString(value)) { | ||
@@ -23,3 +51,3 @@ return "is not a string"; | ||
}; | ||
validate.validators.boolean = (value, options, key, attributes) => { | ||
validate.validators.boolean = function (value, options, key, attributes) { | ||
if (validate.isDefined(value) && !(value === true || value === false)) { | ||
@@ -29,3 +57,3 @@ return "is not a boolean"; | ||
}; | ||
validate.validators.array = (value, options, key, attributes) => { | ||
validate.validators.array = function (value, options, key, attributes) { | ||
if (!validate.isDefined(value)) { | ||
@@ -38,5 +66,5 @@ return; | ||
if (options.numericality) { | ||
const errors = value.map(v => validate.validators.numericality(v, options.numericality)).filter(r => !!r); | ||
var errors = value.map(function (v) { return validate.validators.numericality(v, options.numericality); }).filter(function (r) { return !!r; }); | ||
if (errors.length) { | ||
return errors.map(e => `array member ${e}`); | ||
return errors.map(function (e) { return "array member " + e; }); | ||
} | ||
@@ -46,3 +74,3 @@ ; | ||
}; | ||
validate.validators.arrayNumericality = (value, options, key, attributes) => { | ||
validate.validators.arrayNumericality = function (value, options, key, attributes) { | ||
if (!validate.isDefined(value) || !validate.isArray(value)) { | ||
@@ -52,3 +80,3 @@ return; | ||
}; | ||
validate.validators.absence = (value, options, key, attributes) => { | ||
validate.validators.absence = function (value, options, key, attributes) { | ||
if (!validate.single(value, { presence: true })) { | ||
@@ -58,3 +86,3 @@ return "must be blank"; | ||
}; | ||
validate.validators.defined = (value, options, key, attributes) => { | ||
validate.validators.defined = function (value, options, key, attributes) { | ||
if (value === null) { | ||
@@ -67,3 +95,3 @@ return "is null"; | ||
}; | ||
validate.validators.validator = (value, spec, key, attributes) => { | ||
validate.validators.validator = function (value, spec, key, attributes) { | ||
if (!spec || !spec.constraints || !validate.isFunction(spec.constraints)) { | ||
@@ -73,28 +101,58 @@ throw new Error("'validator' validator should be set to a Validator instance"); | ||
}; | ||
validate.validators.must = (value, validator, key, attributes) => __awaiter(this, void 0, void 0, function* () { | ||
if (!validator) { | ||
throw new Error("validator should be provided for 'must' validator"); | ||
} | ||
if (validate.isArray(validator)) { | ||
for (let i = 0; i < validator.length; i++) { | ||
let result = yield tryValidate(validator[i], value, attributes); | ||
if (result) { | ||
return result; | ||
} | ||
validate.validators.must = function (value, validator, key, attributes) { return __awaiter(_this, void 0, void 0, function () { | ||
var i, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!validator) { | ||
throw new Error("validator should be provided for 'must' validator"); | ||
} | ||
if (!validate.isArray(validator)) return [3 /*break*/, 5]; | ||
i = 0; | ||
_a.label = 1; | ||
case 1: | ||
if (!(i < validator.length)) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, tryValidate(validator[i], value, attributes)]; | ||
case 2: | ||
result = _a.sent(); | ||
if (result) { | ||
return [2 /*return*/, result]; | ||
} | ||
_a.label = 3; | ||
case 3: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [3 /*break*/, 7]; | ||
case 5: return [4 /*yield*/, tryValidate(validator, value, attributes)]; | ||
case 6: return [2 /*return*/, _a.sent()]; | ||
case 7: return [2 /*return*/]; | ||
} | ||
} | ||
else { | ||
return yield tryValidate(validator, value, attributes); | ||
} | ||
}); | ||
}); | ||
}); }; | ||
function tryValidate(validator, value, attributes) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const aa = validator.func(value, attributes); | ||
const valid = isPromise(aa) ? yield aa : aa; | ||
if (validate.isString(valid) || validate.isArray(valid)) { | ||
return valid; | ||
} | ||
if (valid === false) { | ||
return validator.message ? validate.format(validator.message, attributes) : "condition not met"; | ||
} | ||
return __awaiter(this, void 0, void 0, function () { | ||
var aa, valid, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
aa = validator.func(value, attributes); | ||
if (!isPromise(aa)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, aa]; | ||
case 1: | ||
_a = _b.sent(); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
_a = aa; | ||
_b.label = 3; | ||
case 3: | ||
valid = _a; | ||
if (validate.isString(valid) || validate.isArray(valid)) { | ||
return [2 /*return*/, valid]; | ||
} | ||
if (valid === false) { | ||
return [2 /*return*/, validator.message ? validate.format(validator.message, attributes) : "condition not met"]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
@@ -107,4 +165,4 @@ } | ||
validate.extend(validate.validators.datetime, { | ||
parse: (value, options) => { | ||
const utcMoment = moment.utc(value, moment.ISO_8601, true); | ||
parse: function (value, options) { | ||
var utcMoment = moment.utc(value, moment.ISO_8601, true); | ||
if (!utcMoment.isValid()) { | ||
@@ -115,3 +173,3 @@ return NaN; | ||
}, | ||
format: (value, options) => { | ||
format: function (value, options) { | ||
var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss"; | ||
@@ -118,0 +176,0 @@ return moment.utc(value).format(format); |
@@ -10,7 +10,34 @@ "use strict"; | ||
}; | ||
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; | ||
return { next: verb(0), "throw": verb(1), "return": verb(2) }; | ||
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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, 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 }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const validateJsWithCustomValidators_1 = require("./validateJsWithCustomValidators"); | ||
const errors_1 = require("./errors"); | ||
class VisitData { | ||
constructor() { | ||
var validateJsWithCustomValidators_1 = require("./validateJsWithCustomValidators"); | ||
var errors_1 = require("./errors"); | ||
var VisitData = (function () { | ||
function VisitData() { | ||
this.id = 1; | ||
@@ -20,35 +47,39 @@ this.visited = []; | ||
} | ||
getNextId() { | ||
VisitData.prototype.getNextId = function () { | ||
return this.id++; | ||
} | ||
store(visit, store) { | ||
}; | ||
VisitData.prototype.store = function (visit, store) { | ||
this.visited.push(visit); | ||
this.stored.push(store); | ||
} | ||
findStored(visit) { | ||
const index = this.visited.indexOf(visit); | ||
}; | ||
VisitData.prototype.findStored = function (visit) { | ||
var index = this.visited.indexOf(visit); | ||
if (index > -1) { | ||
return this.stored[index]; | ||
} | ||
}; | ||
return VisitData; | ||
}()); | ||
var Cleaner = (function () { | ||
function Cleaner() { | ||
} | ||
} | ||
class Cleaner { | ||
static recursivelyClean(validationResult, visitData) { | ||
const instance = validationResult.context.instance; | ||
Cleaner.recursivelyClean = function (validationResult, visitData) { | ||
var _this = this; | ||
var instance = validationResult.context.instance; | ||
if (!validateJsWithCustomValidators_1.validate.isDefined(instance)) { | ||
return instance; | ||
} | ||
const stored = visitData.findStored(instance); | ||
var stored = visitData.findStored(instance); | ||
if (stored) { | ||
return stored; | ||
} | ||
const cleaned = validateJsWithCustomValidators_1.validate.cleanAttributes(instance, validationResult.context.constraints); | ||
var cleaned = validateJsWithCustomValidators_1.validate.cleanAttributes(instance, validationResult.context.constraints); | ||
visitData.store(instance, cleaned); | ||
if (validationResult.children) { | ||
validationResult.children.forEach(c => { | ||
const clean = this.recursivelyClean(c, visitData); | ||
const parts = c.context.key.split("."); | ||
let parent = cleaned; | ||
const lastIndex = parts.length - 1; | ||
for (let i = 0; i < lastIndex; i++) { | ||
validationResult.children.forEach(function (c) { | ||
var clean = _this.recursivelyClean(c, visitData); | ||
var parts = c.context.key.split("."); | ||
var parent = cleaned; | ||
var lastIndex = parts.length - 1; | ||
for (var i = 0; i < lastIndex; i++) { | ||
parent = parent[parts[i]]; | ||
@@ -60,16 +91,20 @@ } | ||
return cleaned; | ||
}; | ||
return Cleaner; | ||
}()); | ||
var ValidationResultHelper = (function () { | ||
function ValidationResultHelper() { | ||
} | ||
} | ||
class ValidationResultHelper { | ||
static makeRef(key, parentKey, ref) { | ||
return { errors: [], context: { key, parentKey }, ref }; | ||
} | ||
static resolveRefs(result) { | ||
const refs = []; | ||
const resultById = this.collectResultById(result, {}, refs); | ||
refs.forEach(r => { | ||
ValidationResultHelper.makeRef = function (key, parentKey, ref) { | ||
return { errors: [], context: { key: key, parentKey: parentKey }, ref: ref }; | ||
}; | ||
ValidationResultHelper.resolveRefs = function (result) { | ||
var refs = []; | ||
var resultById = this.collectResultById(result, {}, refs); | ||
refs.forEach(function (r) { | ||
r.context = validateJsWithCustomValidators_1.validate.extend({}, resultById[r.ref].context, r.context); | ||
}); | ||
} | ||
static collectResultById(result, results, refs) { | ||
}; | ||
ValidationResultHelper.collectResultById = function (result, results, refs) { | ||
var _this = this; | ||
if (result.ref) { | ||
@@ -83,26 +118,28 @@ refs.push(result); | ||
if (result.children) { | ||
result.children.forEach(c => { | ||
this.collectResultById(c, results, refs); | ||
result.children.forEach(function (c) { | ||
_this.collectResultById(c, results, refs); | ||
}); | ||
} | ||
return results; | ||
} | ||
static collectErrors(result, collect) { | ||
}; | ||
ValidationResultHelper.collectErrors = function (result, collect) { | ||
var _this = this; | ||
if (result.errors && result.errors.length) { | ||
collect.push(...result.errors); | ||
collect.push.apply(collect, result.errors); | ||
} | ||
if (result.children) { | ||
result.children.forEach(c => { | ||
result.children.forEach(function (c) { | ||
if (!c) { | ||
return; | ||
} | ||
this.collectErrors(c, collect); | ||
_this.collectErrors(c, collect); | ||
}); | ||
} | ||
return collect; | ||
} | ||
} | ||
}; | ||
return ValidationResultHelper; | ||
}()); | ||
function createClientValidatorSpec(constraintSpec) { | ||
return { | ||
constraints() { | ||
constraints: function () { | ||
return constraintSpec; | ||
@@ -112,44 +149,61 @@ } | ||
} | ||
class Validation { | ||
static executeClient(instance, spec) { | ||
var Validation = (function () { | ||
function Validation() { | ||
} | ||
Validation.executeClient = function (instance, spec) { | ||
return Validation.execute(instance, createClientValidatorSpec(spec)); | ||
} | ||
static execute(instance, validation) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!instance) { | ||
throw new errors_1.Errors.NoValidationInputError(); | ||
} | ||
const result = yield this.recursivelyValidate(new VisitData(), instance, validation); | ||
const errors = ValidationResultHelper.collectErrors(result, []); | ||
if (errors.length) { | ||
throw new errors_1.Errors.ValidationError(errors); | ||
} | ||
ValidationResultHelper.resolveRefs(result); | ||
return Cleaner.recursivelyClean(result, new VisitData()); | ||
}; | ||
Validation.execute = function (instance, validation) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, errors; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!instance) { | ||
throw new errors_1.Errors.NoValidationInputError(); | ||
} | ||
return [4 /*yield*/, this.recursivelyValidate(new VisitData(), instance, validation)]; | ||
case 1: | ||
result = _a.sent(); | ||
errors = ValidationResultHelper.collectErrors(result, []); | ||
if (errors.length) { | ||
throw new errors_1.Errors.ValidationError(errors); | ||
} | ||
ValidationResultHelper.resolveRefs(result); | ||
return [2 /*return*/, Cleaner.recursivelyClean(result, new VisitData())]; | ||
} | ||
}); | ||
}); | ||
} | ||
static getValidationContext(spec, instance, key, parentKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.hasContext(spec)) { | ||
const ctx = yield spec.getContext(instance, !key); | ||
return this.makeValidationContext(spec, instance, key, parentKey, ctx); | ||
} | ||
return this.makeValidationContext(spec, instance, key, parentKey); | ||
}; | ||
Validation.getValidationContext = function (spec, instance, key, parentKey) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var ctx; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!this.hasContext(spec)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, spec.getContext(instance, !key)]; | ||
case 1: | ||
ctx = _a.sent(); | ||
return [2 /*return*/, this.makeValidationContext(spec, instance, key, parentKey, ctx)]; | ||
case 2: return [2 /*return*/, this.makeValidationContext(spec, instance, key, parentKey)]; | ||
} | ||
}); | ||
}); | ||
} | ||
static makeValidationContext(spec, instance, key, parentKey, ctx) { | ||
const constraints = spec.constraints(ctx); | ||
}; | ||
Validation.makeValidationContext = function (spec, instance, key, parentKey, ctx) { | ||
var constraints = spec.constraints(ctx); | ||
return { | ||
key, | ||
parentKey, | ||
constraints, | ||
instance, | ||
key: key, | ||
parentKey: parentKey, | ||
constraints: constraints, | ||
instance: instance, | ||
validators: this.gatherValidators(constraints) | ||
}; | ||
} | ||
static gatherValidators(constraints) { | ||
const spec = {}; | ||
let found = false; | ||
Object.keys(constraints).forEach((k) => { | ||
const con = constraints[k]; | ||
}; | ||
Validation.gatherValidators = function (constraints) { | ||
var spec = {}; | ||
var found = false; | ||
Object.keys(constraints).forEach(function (k) { | ||
var con = constraints[k]; | ||
if (con.validator) { | ||
@@ -165,85 +219,133 @@ spec[k] = con.validator; | ||
return found ? spec : undefined; | ||
} | ||
static hasContext(spec) { | ||
}; | ||
Validation.hasContext = function (spec) { | ||
return !!spec.getContext; | ||
} | ||
static recursivelyValidate(visitData, instance, spec, key, parentKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let id = visitData.getNextId(); | ||
if (validateJsWithCustomValidators_1.validate.isDefined(instance)) { | ||
const stored = visitData.findStored(instance); | ||
if (stored) { | ||
return ValidationResultHelper.makeRef(key, parentKey, stored); | ||
}; | ||
Validation.recursivelyValidate = function (visitData, instance, spec, key, parentKey) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var id, stored, context; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
id = visitData.getNextId(); | ||
if (validateJsWithCustomValidators_1.validate.isDefined(instance)) { | ||
stored = visitData.findStored(instance); | ||
if (stored) { | ||
return [2 /*return*/, ValidationResultHelper.makeRef(key, parentKey, stored)]; | ||
} | ||
visitData.store(instance, id); | ||
} | ||
return [4 /*yield*/, this.getValidationContext(spec, instance, key, parentKey)]; | ||
case 1: | ||
context = _a.sent(); | ||
return [4 /*yield*/, this.validateContext(visitData, id, context)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
} | ||
visitData.store(instance, id); | ||
} | ||
const context = yield this.getValidationContext(spec, instance, key, parentKey); | ||
return yield this.validateContext(visitData, id, context); | ||
}); | ||
}); | ||
} | ||
static validateContext(visitData, id, validationContext) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const errors = yield this.getValidationErrors(validationContext); | ||
const validationResult = { id, errors: errors || [], context: validationContext }; | ||
if (!validationContext.validators || !validationContext.instance) { | ||
return validationResult; | ||
} | ||
const results = yield Validation.getChildValidationResults(visitData, validationContext); | ||
validationResult.children = results; | ||
return validationResult; | ||
}; | ||
Validation.validateContext = function (visitData, id, validationContext) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var errors, validationResult, results; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.getValidationErrors(validationContext)]; | ||
case 1: | ||
errors = _a.sent(); | ||
validationResult = { id: id, errors: errors || [], context: validationContext }; | ||
if (!validationContext.validators || !validationContext.instance) { | ||
return [2 /*return*/, validationResult]; | ||
} | ||
return [4 /*yield*/, Validation.getChildValidationResults(visitData, validationContext)]; | ||
case 2: | ||
results = _a.sent(); | ||
validationResult.children = results; | ||
return [2 /*return*/, validationResult]; | ||
} | ||
}); | ||
}); | ||
} | ||
static getChildValidationResults(visitData, validationContext) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const results = []; | ||
const keys = Object.keys(validationContext.validators); | ||
for (let i = 0; i < keys.length; i++) { | ||
let key = keys[i]; | ||
let value = validationContext.instance[key]; | ||
if (!validateJsWithCustomValidators_1.validate.isDefined(value)) { | ||
continue; | ||
} | ||
let spec = validationContext.validators[key]; | ||
if (validateJsWithCustomValidators_1.validate.isArray(value)) { | ||
for (let idx = 0; idx < value.length; idx++) { | ||
let arrayKey = `${key}.${idx}`; | ||
let ve = yield this.recursivelyValidate(visitData, value[idx], spec, arrayKey, this.makeKey(arrayKey, validationContext.parentKey)); | ||
}; | ||
Validation.getChildValidationResults = function (visitData, validationContext) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var results, keys, i, key, value, spec, idx, arrayKey, ve_1, ve; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
results = []; | ||
keys = Object.keys(validationContext.validators); | ||
i = 0; | ||
_a.label = 1; | ||
case 1: | ||
if (!(i < keys.length)) return [3 /*break*/, 9]; | ||
key = keys[i]; | ||
value = validationContext.instance[key]; | ||
if (!validateJsWithCustomValidators_1.validate.isDefined(value)) { | ||
return [3 /*break*/, 8]; | ||
} | ||
spec = validationContext.validators[key]; | ||
if (!validateJsWithCustomValidators_1.validate.isArray(value)) return [3 /*break*/, 6]; | ||
idx = 0; | ||
_a.label = 2; | ||
case 2: | ||
if (!(idx < value.length)) return [3 /*break*/, 5]; | ||
arrayKey = key + "." + idx; | ||
return [4 /*yield*/, this.recursivelyValidate(visitData, value[idx], spec, arrayKey, this.makeKey(arrayKey, validationContext.parentKey))]; | ||
case 3: | ||
ve_1 = _a.sent(); | ||
if (ve_1) { | ||
results.push(ve_1); | ||
} | ||
_a.label = 4; | ||
case 4: | ||
idx++; | ||
return [3 /*break*/, 2]; | ||
case 5: | ||
; | ||
return [3 /*break*/, 8]; | ||
case 6: return [4 /*yield*/, this.recursivelyValidate(visitData, value, spec, key, this.makeKey(key, validationContext.parentKey))]; | ||
case 7: | ||
ve = _a.sent(); | ||
if (ve) { | ||
results.push(ve); | ||
} | ||
} | ||
; | ||
continue; | ||
_a.label = 8; | ||
case 8: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 9: return [2 /*return*/, results]; | ||
} | ||
let ve = yield this.recursivelyValidate(visitData, value, spec, key, this.makeKey(key, validationContext.parentKey)); | ||
if (ve) { | ||
results.push(ve); | ||
} | ||
} | ||
return results; | ||
}); | ||
}); | ||
} | ||
static makeKey(key, parentKey) { | ||
return parentKey ? `${parentKey}.${key}` : key; | ||
} | ||
static getValidationErrors(validationContext) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
return yield validateJsWithCustomValidators_1.validate.async(validationContext.instance, validationContext.constraints, { format: "detailed" }); | ||
} | ||
catch (errors) { | ||
if (!validateJsWithCustomValidators_1.validate.isArray(errors)) { | ||
throw errors; | ||
}; | ||
Validation.makeKey = function (key, parentKey) { | ||
return parentKey ? parentKey + "." + key : key; | ||
}; | ||
Validation.getValidationErrors = function (validationContext) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var errors_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, validateJsWithCustomValidators_1.validate.async(validationContext.instance, validationContext.constraints, { format: "detailed" })]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
case 2: | ||
errors_2 = _a.sent(); | ||
if (!validateJsWithCustomValidators_1.validate.isArray(errors_2)) { | ||
throw errors_2; | ||
} | ||
return [2 /*return*/, errors_2.map(function (d) { | ||
return { | ||
message: d.error, | ||
value: d.value, | ||
attribute: validationContext.parentKey ? validationContext.parentKey + "." + d.attribute : d.attribute | ||
}; | ||
})]; | ||
case 3: return [2 /*return*/]; | ||
} | ||
return errors.map(d => { | ||
return { | ||
message: d.error, | ||
value: d.value, | ||
attribute: validationContext.parentKey ? `${validationContext.parentKey}.${d.attribute}` : d.attribute | ||
}; | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
return Validation; | ||
}()); | ||
exports.Validation = Validation; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const validation_1 = require("./validation"); | ||
const constraintBuilder_1 = require("./constraintBuilder"); | ||
class ValidatorBase extends constraintBuilder_1.ConstraintBuilder { | ||
validate(instance) { | ||
var validation_1 = require("./validation"); | ||
var constraintBuilder_1 = require("./constraintBuilder"); | ||
var ValidatorBase = (function (_super) { | ||
__extends(ValidatorBase, _super); | ||
function ValidatorBase() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
ValidatorBase.prototype.validate = function (instance) { | ||
return validation_1.Validation.execute(instance, this); | ||
}; | ||
return ValidatorBase; | ||
}(constraintBuilder_1.ConstraintBuilder)); | ||
exports.ValidatorBase = ValidatorBase; | ||
var Validator = (function (_super) { | ||
__extends(Validator, _super); | ||
function Validator() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
} | ||
exports.ValidatorBase = ValidatorBase; | ||
class Validator extends ValidatorBase { | ||
} | ||
return Validator; | ||
}(ValidatorBase)); | ||
exports.Validator = Validator; | ||
function createValidator(constraintSpecBuilder) { | ||
class V extends Validator { | ||
constraints() { | ||
var V = (function (_super) { | ||
__extends(V, _super); | ||
function V() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
V.prototype.constraints = function () { | ||
return constraintSpecBuilder(this); | ||
} | ||
} | ||
}; | ||
return V; | ||
}(Validator)); | ||
return new V(); | ||
} | ||
exports.createValidator = createValidator; | ||
const clientConstraintBuilder = new constraintBuilder_1.ClientConstraintBuilder(); | ||
var clientConstraintBuilder = new constraintBuilder_1.ClientConstraintBuilder(); | ||
function createClientConstraintSpec(constraintSpecBuilder) { | ||
@@ -28,4 +53,9 @@ return constraintSpecBuilder(clientConstraintBuilder); | ||
exports.createClientConstraintSpec = createClientConstraintSpec; | ||
class ValidatorWithContext extends ValidatorBase { | ||
} | ||
var ValidatorWithContext = (function (_super) { | ||
__extends(ValidatorWithContext, _super); | ||
function ValidatorWithContext() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return ValidatorWithContext; | ||
}(ValidatorBase)); | ||
exports.ValidatorWithContext = ValidatorWithContext; |
{ | ||
"name": "rokot-validate", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Rocketmakers Rokot Platform - Validation", | ||
@@ -26,3 +26,5 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@types/es6-promise": "^0.0.32", | ||
"@types/node": "^6.0.41", | ||
"es6-promise": "^4.0.5", | ||
"rokot-test": "^0.2.5", | ||
@@ -29,0 +31,0 @@ "typescript": "~2.2.0", |
Sorry, the diff of this file is not supported yet
76979
1156
6