backend v4 half
This commit is contained in:
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=ValidationArguments.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ValidationArguments.js","sourceRoot":"","sources":["../../../src/validation/ValidationArguments.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Arguments being sent to message builders - user can create message either by simply returning a string,\n * either by returning a function that accepts MessageArguments and returns a message string built based on these arguments.\n */\nexport interface ValidationArguments {\n /**\n * Validating value.\n */\n value: any;\n\n /**\n * Constraints set by this validation type.\n */\n constraints: any[];\n\n /**\n * Name of the target that is being validated.\n */\n targetName: string;\n\n /**\n * Object that is being validated.\n */\n object: object;\n\n /**\n * Name of the object's property being validated.\n */\n property: string;\n}\n"]}
|
||||
Generated
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Validation error description.
|
||||
*/
|
||||
var ValidationError = /** @class */ (function () {
|
||||
function ValidationError() {
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param shouldDecorate decorate the message with ANSI formatter escape codes for better readability
|
||||
* @param hasParent true when the error is a child of an another one
|
||||
* @param parentPath path as string to the parent of this property
|
||||
* @param showConstraintMessages show constraint messages instead of constraint names
|
||||
*/
|
||||
ValidationError.prototype.toString = function (shouldDecorate, hasParent, parentPath, showConstraintMessages) {
|
||||
var _this = this;
|
||||
if (shouldDecorate === void 0) { shouldDecorate = false; }
|
||||
if (hasParent === void 0) { hasParent = false; }
|
||||
if (parentPath === void 0) { parentPath = ""; }
|
||||
if (showConstraintMessages === void 0) { showConstraintMessages = false; }
|
||||
var boldStart = shouldDecorate ? "\u001B[1m" : "";
|
||||
var boldEnd = shouldDecorate ? "\u001B[22m" : "";
|
||||
var constraintsToString = function () { var _a; return (showConstraintMessages ? Object.values : Object.keys)((_a = _this.constraints) !== null && _a !== void 0 ? _a : {}).join(", "); };
|
||||
var propConstraintFailed = function (propertyName) {
|
||||
return " - property ".concat(boldStart).concat(parentPath).concat(propertyName).concat(boldEnd, " has failed the following constraints: ").concat(boldStart).concat(constraintsToString()).concat(boldEnd, " \n");
|
||||
};
|
||||
if (!hasParent) {
|
||||
return ("An instance of ".concat(boldStart).concat(this.target ? this.target.constructor.name : 'an object').concat(boldEnd, " has failed the validation:\n") +
|
||||
(this.constraints ? propConstraintFailed(this.property) : "") +
|
||||
(this.children
|
||||
? this.children
|
||||
.map(function (childError) { return childError.toString(shouldDecorate, true, _this.property, showConstraintMessages); })
|
||||
.join("")
|
||||
: ""));
|
||||
}
|
||||
else {
|
||||
// we format numbers as array indexes for better readability.
|
||||
var formattedProperty_1 = Number.isInteger(+this.property)
|
||||
? "[".concat(this.property, "]")
|
||||
: "".concat(parentPath ? "." : "").concat(this.property);
|
||||
if (this.constraints) {
|
||||
return propConstraintFailed(formattedProperty_1);
|
||||
}
|
||||
else {
|
||||
return this.children
|
||||
? this.children
|
||||
.map(function (childError) {
|
||||
return childError.toString(shouldDecorate, true, "".concat(parentPath).concat(formattedProperty_1), showConstraintMessages);
|
||||
})
|
||||
.join("")
|
||||
: "";
|
||||
}
|
||||
}
|
||||
};
|
||||
return ValidationError;
|
||||
}());
|
||||
export { ValidationError };
|
||||
//# sourceMappingURL=ValidationError.js.map
|
||||
Generated
Vendored
+1
File diff suppressed because one or more lines are too long
Generated
Vendored
+351
@@ -0,0 +1,351 @@
|
||||
var __read = (this && this.__read) || function (o, n) {
|
||||
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
||||
if (!m) return o;
|
||||
var i = m.call(o), r, ar = [], e;
|
||||
try {
|
||||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
||||
}
|
||||
catch (error) { e = { error: error }; }
|
||||
finally {
|
||||
try {
|
||||
if (r && !r.done && (m = i["return"])) m.call(i);
|
||||
}
|
||||
finally { if (e) throw e.error; }
|
||||
}
|
||||
return ar;
|
||||
};
|
||||
import { ValidationError } from './ValidationError';
|
||||
import { ValidationTypes } from './ValidationTypes';
|
||||
import { ValidationUtils } from './ValidationUtils';
|
||||
import { isPromise, convertToArray } from '../utils';
|
||||
import { getMetadataStorage } from '../metadata/MetadataStorage';
|
||||
/**
|
||||
* Executes validation over given object.
|
||||
*/
|
||||
var ValidationExecutor = /** @class */ (function () {
|
||||
// -------------------------------------------------------------------------
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
function ValidationExecutor(validator, validatorOptions) {
|
||||
this.validator = validator;
|
||||
this.validatorOptions = validatorOptions;
|
||||
// -------------------------------------------------------------------------
|
||||
// Properties
|
||||
// -------------------------------------------------------------------------
|
||||
this.awaitingPromises = [];
|
||||
this.ignoreAsyncValidations = false;
|
||||
// -------------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// -------------------------------------------------------------------------
|
||||
this.metadataStorage = getMetadataStorage();
|
||||
}
|
||||
// -------------------------------------------------------------------------
|
||||
// Public Methods
|
||||
// -------------------------------------------------------------------------
|
||||
ValidationExecutor.prototype.execute = function (object, targetSchema, validationErrors) {
|
||||
var _this = this;
|
||||
var _a, _b;
|
||||
/**
|
||||
* If there is no metadata registered it means possibly the dependencies are not flatterned and
|
||||
* more than one instance is used.
|
||||
*
|
||||
* TODO: This needs proper handling, forcing to use the same container or some other proper solution.
|
||||
*/
|
||||
if (!this.metadataStorage.hasValidationMetaData && ((_a = this.validatorOptions) === null || _a === void 0 ? void 0 : _a.enableDebugMessages) === true) {
|
||||
console.warn("No validation metadata found. No validation will be performed. There are multiple possible reasons:\n" +
|
||||
" - There may be multiple class-validator versions installed. You will need to flatten your dependencies to fix the issue.\n" +
|
||||
" - This validation runs before any file with validation decorator was parsed by NodeJS.");
|
||||
}
|
||||
var groups = this.validatorOptions ? this.validatorOptions.groups : undefined;
|
||||
var strictGroups = (this.validatorOptions && this.validatorOptions.strictGroups) || false;
|
||||
var always = (this.validatorOptions && this.validatorOptions.always) || false;
|
||||
/** Forbid unknown values are turned on by default and any other value than false will enable it. */
|
||||
var forbidUnknownValues = ((_b = this.validatorOptions) === null || _b === void 0 ? void 0 : _b.forbidUnknownValues) === undefined || this.validatorOptions.forbidUnknownValues !== false;
|
||||
var targetMetadatas = this.metadataStorage.getTargetValidationMetadatas(object.constructor, targetSchema, always, strictGroups, groups);
|
||||
var groupedMetadatas = this.metadataStorage.groupByPropertyName(targetMetadatas);
|
||||
if (forbidUnknownValues && !targetMetadatas.length) {
|
||||
var validationError = new ValidationError();
|
||||
if (!this.validatorOptions ||
|
||||
!this.validatorOptions.validationError ||
|
||||
this.validatorOptions.validationError.target === undefined ||
|
||||
this.validatorOptions.validationError.target === true)
|
||||
validationError.target = object;
|
||||
validationError.value = undefined;
|
||||
validationError.property = undefined;
|
||||
validationError.children = [];
|
||||
validationError.constraints = { unknownValue: 'an unknown value was passed to the validate function' };
|
||||
validationErrors.push(validationError);
|
||||
return;
|
||||
}
|
||||
if (this.validatorOptions && this.validatorOptions.whitelist)
|
||||
this.whitelist(object, groupedMetadatas, validationErrors);
|
||||
// General validation
|
||||
Object.keys(groupedMetadatas).forEach(function (propertyName) {
|
||||
var value = object[propertyName];
|
||||
var definedMetadatas = groupedMetadatas[propertyName].filter(function (metadata) { return metadata.type === ValidationTypes.IS_DEFINED; });
|
||||
var metadatas = groupedMetadatas[propertyName].filter(function (metadata) { return metadata.type !== ValidationTypes.IS_DEFINED && metadata.type !== ValidationTypes.WHITELIST; });
|
||||
if (value instanceof Promise &&
|
||||
metadatas.find(function (metadata) { return metadata.type === ValidationTypes.PROMISE_VALIDATION; })) {
|
||||
_this.awaitingPromises.push(value.then(function (resolvedValue) {
|
||||
_this.performValidations(object, resolvedValue, propertyName, definedMetadatas, metadatas, validationErrors);
|
||||
}));
|
||||
}
|
||||
else {
|
||||
_this.performValidations(object, value, propertyName, definedMetadatas, metadatas, validationErrors);
|
||||
}
|
||||
});
|
||||
};
|
||||
ValidationExecutor.prototype.whitelist = function (object, groupedMetadatas, validationErrors) {
|
||||
var _this = this;
|
||||
var notAllowedProperties = [];
|
||||
Object.keys(object).forEach(function (propertyName) {
|
||||
// does this property have no metadata?
|
||||
if (!groupedMetadatas[propertyName] || groupedMetadatas[propertyName].length === 0)
|
||||
notAllowedProperties.push(propertyName);
|
||||
});
|
||||
if (notAllowedProperties.length > 0) {
|
||||
if (this.validatorOptions && this.validatorOptions.forbidNonWhitelisted) {
|
||||
// throw errors
|
||||
notAllowedProperties.forEach(function (property) {
|
||||
var _a;
|
||||
var validationError = _this.generateValidationError(object, object[property], property);
|
||||
validationError.constraints = (_a = {}, _a[ValidationTypes.WHITELIST] = "property ".concat(property, " should not exist"), _a);
|
||||
validationError.children = undefined;
|
||||
validationErrors.push(validationError);
|
||||
});
|
||||
}
|
||||
else {
|
||||
// strip non allowed properties
|
||||
notAllowedProperties.forEach(function (property) { return delete object[property]; });
|
||||
}
|
||||
}
|
||||
};
|
||||
ValidationExecutor.prototype.stripEmptyErrors = function (errors) {
|
||||
var _this = this;
|
||||
return errors.filter(function (error) {
|
||||
if (error.children) {
|
||||
error.children = _this.stripEmptyErrors(error.children);
|
||||
}
|
||||
if (Object.keys(error.constraints).length === 0) {
|
||||
if (error.children.length === 0) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
delete error.constraints;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
};
|
||||
// -------------------------------------------------------------------------
|
||||
// Private Methods
|
||||
// -------------------------------------------------------------------------
|
||||
ValidationExecutor.prototype.performValidations = function (object, value, propertyName, definedMetadatas, metadatas, validationErrors) {
|
||||
var customValidationMetadatas = metadatas.filter(function (metadata) { return metadata.type === ValidationTypes.CUSTOM_VALIDATION; });
|
||||
var nestedValidationMetadatas = metadatas.filter(function (metadata) { return metadata.type === ValidationTypes.NESTED_VALIDATION; });
|
||||
var conditionalValidationMetadatas = metadatas.filter(function (metadata) { return metadata.type === ValidationTypes.CONDITIONAL_VALIDATION; });
|
||||
var validationError = this.generateValidationError(object, value, propertyName);
|
||||
validationErrors.push(validationError);
|
||||
var canValidate = this.conditionalValidations(object, value, conditionalValidationMetadatas);
|
||||
if (!canValidate) {
|
||||
return;
|
||||
}
|
||||
// handle IS_DEFINED validation type the special way - it should work no matter skipUndefinedProperties/skipMissingProperties is set or not
|
||||
this.customValidations(object, value, definedMetadatas, validationError);
|
||||
this.mapContexts(object, value, definedMetadatas, validationError);
|
||||
if (value === undefined && this.validatorOptions && this.validatorOptions.skipUndefinedProperties === true) {
|
||||
return;
|
||||
}
|
||||
if (value === null && this.validatorOptions && this.validatorOptions.skipNullProperties === true) {
|
||||
return;
|
||||
}
|
||||
if ((value === null || value === undefined) &&
|
||||
this.validatorOptions &&
|
||||
this.validatorOptions.skipMissingProperties === true) {
|
||||
return;
|
||||
}
|
||||
this.customValidations(object, value, customValidationMetadatas, validationError);
|
||||
this.nestedValidations(value, nestedValidationMetadatas, validationError);
|
||||
this.mapContexts(object, value, metadatas, validationError);
|
||||
this.mapContexts(object, value, customValidationMetadatas, validationError);
|
||||
};
|
||||
ValidationExecutor.prototype.generateValidationError = function (object, value, propertyName) {
|
||||
var validationError = new ValidationError();
|
||||
if (!this.validatorOptions ||
|
||||
!this.validatorOptions.validationError ||
|
||||
this.validatorOptions.validationError.target === undefined ||
|
||||
this.validatorOptions.validationError.target === true)
|
||||
validationError.target = object;
|
||||
if (!this.validatorOptions ||
|
||||
!this.validatorOptions.validationError ||
|
||||
this.validatorOptions.validationError.value === undefined ||
|
||||
this.validatorOptions.validationError.value === true)
|
||||
validationError.value = value;
|
||||
validationError.property = propertyName;
|
||||
validationError.children = [];
|
||||
validationError.constraints = {};
|
||||
return validationError;
|
||||
};
|
||||
ValidationExecutor.prototype.conditionalValidations = function (object, value, metadatas) {
|
||||
return metadatas
|
||||
.map(function (metadata) { return metadata.constraints[0](object, value); })
|
||||
.reduce(function (resultA, resultB) { return resultA && resultB; }, true);
|
||||
};
|
||||
ValidationExecutor.prototype.customValidations = function (object, value, metadatas, error) {
|
||||
var _this = this;
|
||||
metadatas.forEach(function (metadata) {
|
||||
_this.metadataStorage.getTargetValidatorConstraints(metadata.constraintCls).forEach(function (customConstraintMetadata) {
|
||||
if (customConstraintMetadata.async && _this.ignoreAsyncValidations)
|
||||
return;
|
||||
if (_this.validatorOptions &&
|
||||
_this.validatorOptions.stopAtFirstError &&
|
||||
Object.keys(error.constraints || {}).length > 0)
|
||||
return;
|
||||
var validationArguments = {
|
||||
targetName: object.constructor ? object.constructor.name : undefined,
|
||||
property: metadata.propertyName,
|
||||
object: object,
|
||||
value: value,
|
||||
constraints: metadata.constraints,
|
||||
};
|
||||
if (!metadata.each || !(Array.isArray(value) || value instanceof Set || value instanceof Map)) {
|
||||
var validatedValue = customConstraintMetadata.instance.validate(value, validationArguments);
|
||||
if (isPromise(validatedValue)) {
|
||||
var promise = validatedValue.then(function (isValid) {
|
||||
if (!isValid) {
|
||||
var _a = __read(_this.createValidationError(object, value, metadata, customConstraintMetadata), 2), type = _a[0], message = _a[1];
|
||||
error.constraints[type] = message;
|
||||
if (metadata.context) {
|
||||
if (!error.contexts) {
|
||||
error.contexts = {};
|
||||
}
|
||||
error.contexts[type] = Object.assign(error.contexts[type] || {}, metadata.context);
|
||||
}
|
||||
}
|
||||
});
|
||||
_this.awaitingPromises.push(promise);
|
||||
}
|
||||
else {
|
||||
if (!validatedValue) {
|
||||
var _a = __read(_this.createValidationError(object, value, metadata, customConstraintMetadata), 2), type = _a[0], message = _a[1];
|
||||
error.constraints[type] = message;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// convert set and map into array
|
||||
var arrayValue = convertToArray(value);
|
||||
// Validation needs to be applied to each array item
|
||||
var validatedSubValues = arrayValue.map(function (subValue) {
|
||||
return customConstraintMetadata.instance.validate(subValue, validationArguments);
|
||||
});
|
||||
var validationIsAsync = validatedSubValues.some(function (validatedSubValue) {
|
||||
return isPromise(validatedSubValue);
|
||||
});
|
||||
if (validationIsAsync) {
|
||||
// Wrap plain values (if any) in promises, so that all are async
|
||||
var asyncValidatedSubValues = validatedSubValues.map(function (validatedSubValue) {
|
||||
return isPromise(validatedSubValue) ? validatedSubValue : Promise.resolve(validatedSubValue);
|
||||
});
|
||||
var asyncValidationIsFinishedPromise = Promise.all(asyncValidatedSubValues).then(function (flatValidatedValues) {
|
||||
var validationResult = flatValidatedValues.every(function (isValid) { return isValid; });
|
||||
if (!validationResult) {
|
||||
var _a = __read(_this.createValidationError(object, value, metadata, customConstraintMetadata), 2), type = _a[0], message = _a[1];
|
||||
error.constraints[type] = message;
|
||||
if (metadata.context) {
|
||||
if (!error.contexts) {
|
||||
error.contexts = {};
|
||||
}
|
||||
error.contexts[type] = Object.assign(error.contexts[type] || {}, metadata.context);
|
||||
}
|
||||
}
|
||||
});
|
||||
_this.awaitingPromises.push(asyncValidationIsFinishedPromise);
|
||||
return;
|
||||
}
|
||||
var validationResult = validatedSubValues.every(function (isValid) { return isValid; });
|
||||
if (!validationResult) {
|
||||
var _b = __read(_this.createValidationError(object, value, metadata, customConstraintMetadata), 2), type = _b[0], message = _b[1];
|
||||
error.constraints[type] = message;
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
ValidationExecutor.prototype.nestedValidations = function (value, metadatas, error) {
|
||||
var _this = this;
|
||||
if (value === void 0) {
|
||||
return;
|
||||
}
|
||||
metadatas.forEach(function (metadata) {
|
||||
if (metadata.type !== ValidationTypes.NESTED_VALIDATION && metadata.type !== ValidationTypes.PROMISE_VALIDATION) {
|
||||
return;
|
||||
}
|
||||
else if (_this.validatorOptions &&
|
||||
_this.validatorOptions.stopAtFirstError &&
|
||||
Object.keys(error.constraints || {}).length > 0) {
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(value) || value instanceof Set || value instanceof Map) {
|
||||
// Treats Set as an array - as index of Set value is value itself and it is common case to have Object as value
|
||||
var arrayLikeValue = value instanceof Set ? Array.from(value) : value;
|
||||
arrayLikeValue.forEach(function (subValue, index) {
|
||||
_this.performValidations(value, subValue, index.toString(), [], metadatas, error.children);
|
||||
});
|
||||
}
|
||||
else if (value instanceof Object) {
|
||||
var targetSchema = typeof metadata.target === 'string' ? metadata.target : metadata.target.name;
|
||||
_this.execute(value, targetSchema, error.children);
|
||||
}
|
||||
else {
|
||||
var _a = __read(_this.createValidationError(metadata.target, value, metadata), 2), type = _a[0], message = _a[1];
|
||||
error.constraints[type] = message;
|
||||
}
|
||||
});
|
||||
};
|
||||
ValidationExecutor.prototype.mapContexts = function (object, value, metadatas, error) {
|
||||
var _this = this;
|
||||
return metadatas.forEach(function (metadata) {
|
||||
if (metadata.context) {
|
||||
var customConstraint = void 0;
|
||||
if (metadata.type === ValidationTypes.CUSTOM_VALIDATION) {
|
||||
var customConstraints = _this.metadataStorage.getTargetValidatorConstraints(metadata.constraintCls);
|
||||
customConstraint = customConstraints[0];
|
||||
}
|
||||
var type = _this.getConstraintType(metadata, customConstraint);
|
||||
if (error.constraints[type]) {
|
||||
if (!error.contexts) {
|
||||
error.contexts = {};
|
||||
}
|
||||
error.contexts[type] = Object.assign(error.contexts[type] || {}, metadata.context);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
ValidationExecutor.prototype.createValidationError = function (object, value, metadata, customValidatorMetadata) {
|
||||
var targetName = object.constructor ? object.constructor.name : undefined;
|
||||
var type = this.getConstraintType(metadata, customValidatorMetadata);
|
||||
var validationArguments = {
|
||||
targetName: targetName,
|
||||
property: metadata.propertyName,
|
||||
object: object,
|
||||
value: value,
|
||||
constraints: metadata.constraints,
|
||||
};
|
||||
var message = metadata.message || '';
|
||||
if (!metadata.message &&
|
||||
(!this.validatorOptions || (this.validatorOptions && !this.validatorOptions.dismissDefaultMessages))) {
|
||||
if (customValidatorMetadata && customValidatorMetadata.instance.defaultMessage instanceof Function) {
|
||||
message = customValidatorMetadata.instance.defaultMessage(validationArguments);
|
||||
}
|
||||
}
|
||||
var messageString = ValidationUtils.replaceMessageSpecialTokens(message, validationArguments);
|
||||
return [type, messageString];
|
||||
};
|
||||
ValidationExecutor.prototype.getConstraintType = function (metadata, customValidatorMetadata) {
|
||||
var type = customValidatorMetadata && customValidatorMetadata.name ? customValidatorMetadata.name : metadata.type;
|
||||
return type;
|
||||
};
|
||||
return ValidationExecutor;
|
||||
}());
|
||||
export { ValidationExecutor };
|
||||
//# sourceMappingURL=ValidationExecutor.js.map
|
||||
Generated
Vendored
+1
File diff suppressed because one or more lines are too long
Generated
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Validation types.
|
||||
*/
|
||||
var ValidationTypes = /** @class */ (function () {
|
||||
function ValidationTypes() {
|
||||
}
|
||||
/**
|
||||
* Checks if validation type is valid.
|
||||
*/
|
||||
ValidationTypes.isValid = function (type) {
|
||||
var _this = this;
|
||||
return (type !== 'isValid' &&
|
||||
type !== 'getMessage' &&
|
||||
Object.keys(this)
|
||||
.map(function (key) { return _this[key]; })
|
||||
.indexOf(type) !== -1);
|
||||
};
|
||||
/* system */
|
||||
ValidationTypes.CUSTOM_VALIDATION = 'customValidation'; // done
|
||||
ValidationTypes.NESTED_VALIDATION = 'nestedValidation'; // done
|
||||
ValidationTypes.PROMISE_VALIDATION = 'promiseValidation'; // done
|
||||
ValidationTypes.CONDITIONAL_VALIDATION = 'conditionalValidation'; // done
|
||||
ValidationTypes.WHITELIST = 'whitelistValidation'; // done
|
||||
ValidationTypes.IS_DEFINED = 'isDefined'; // done
|
||||
return ValidationTypes;
|
||||
}());
|
||||
export { ValidationTypes };
|
||||
//# sourceMappingURL=ValidationTypes.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ValidationTypes.js","sourceRoot":"","sources":["../../../src/validation/ValidationTypes.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH;IAAA;IAqBA,CAAC;IAZC;;OAEG;IACI,uBAAO,GAAd,UAAe,IAAY;QAA3B,iBAQC;QAPC,OAAO,CACL,IAAI,KAAK,SAAS;YAClB,IAAI,KAAK,YAAY;YACrB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;iBACd,GAAG,CAAC,UAAA,GAAG,IAAI,OAAC,KAAY,CAAC,GAAG,CAAC,EAAlB,CAAkB,CAAC;iBAC9B,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CACxB,CAAC;IACJ,CAAC;IAnBD,YAAY;IACL,iCAAiB,GAAG,kBAAkB,CAAC,CAAC,OAAO;IAC/C,iCAAiB,GAAG,kBAAkB,CAAC,CAAC,OAAO;IAC/C,kCAAkB,GAAG,mBAAmB,CAAC,CAAC,OAAO;IACjD,sCAAsB,GAAG,uBAAuB,CAAC,CAAC,OAAO;IACzD,yBAAS,GAAG,qBAAqB,CAAC,CAAC,OAAO;IAC1C,0BAAU,GAAG,WAAW,CAAC,CAAC,OAAO;IAc1C,sBAAC;CAAA,AArBD,IAqBC;SArBY,eAAe","sourcesContent":["/**\n * Validation types.\n */\nexport class ValidationTypes {\n /* system */\n static CUSTOM_VALIDATION = 'customValidation'; // done\n static NESTED_VALIDATION = 'nestedValidation'; // done\n static PROMISE_VALIDATION = 'promiseValidation'; // done\n static CONDITIONAL_VALIDATION = 'conditionalValidation'; // done\n static WHITELIST = 'whitelistValidation'; // done\n static IS_DEFINED = 'isDefined'; // done\n\n /**\n * Checks if validation type is valid.\n */\n static isValid(type: string): boolean {\n return (\n type !== 'isValid' &&\n type !== 'getMessage' &&\n Object.keys(this)\n .map(key => (this as any)[key])\n .indexOf(type) !== -1\n );\n }\n}\n"]}
|
||||
Generated
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Convert the constraint to a string to be shown in an error
|
||||
*/
|
||||
export function constraintToString(constraint) {
|
||||
if (Array.isArray(constraint)) {
|
||||
return constraint.join(', ');
|
||||
}
|
||||
if (typeof constraint === 'symbol') {
|
||||
constraint = constraint.description;
|
||||
}
|
||||
return "".concat(constraint);
|
||||
}
|
||||
var ValidationUtils = /** @class */ (function () {
|
||||
function ValidationUtils() {
|
||||
}
|
||||
ValidationUtils.replaceMessageSpecialTokens = function (message, validationArguments) {
|
||||
var messageString;
|
||||
if (message instanceof Function) {
|
||||
messageString = message(validationArguments);
|
||||
}
|
||||
else if (typeof message === 'string') {
|
||||
messageString = message;
|
||||
}
|
||||
if (messageString && Array.isArray(validationArguments.constraints)) {
|
||||
validationArguments.constraints.forEach(function (constraint, index) {
|
||||
messageString = messageString.replace(new RegExp("\\$constraint".concat(index + 1), 'g'), constraintToString(constraint));
|
||||
});
|
||||
}
|
||||
if (messageString &&
|
||||
validationArguments.value !== undefined &&
|
||||
validationArguments.value !== null &&
|
||||
['string', 'boolean', 'number'].includes(typeof validationArguments.value))
|
||||
messageString = messageString.replace(/\$value/g, validationArguments.value);
|
||||
if (messageString)
|
||||
messageString = messageString.replace(/\$property/g, validationArguments.property);
|
||||
if (messageString)
|
||||
messageString = messageString.replace(/\$target/g, validationArguments.targetName);
|
||||
return messageString;
|
||||
};
|
||||
return ValidationUtils;
|
||||
}());
|
||||
export { ValidationUtils };
|
||||
//# sourceMappingURL=ValidationUtils.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ValidationUtils.js","sourceRoot":"","sources":["../../../src/validation/ValidationUtils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAAmB;IACpD,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QAC9B,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACnC,UAAU,GAAG,UAAU,CAAC,WAAW,CAAC;IACtC,CAAC;IAED,OAAO,UAAG,UAAU,CAAE,CAAC;AACzB,CAAC;AAED;IAAA;IAiCA,CAAC;IAhCQ,2CAA2B,GAAlC,UACE,OAAyD,EACzD,mBAAwC;QAExC,IAAI,aAAqB,CAAC;QAC1B,IAAI,OAAO,YAAY,QAAQ,EAAE,CAAC;YAChC,aAAa,GAAI,OAAiD,CAAC,mBAAmB,CAAC,CAAC;QAC1F,CAAC;aAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YACvC,aAAa,GAAG,OAAO,CAAC;QAC1B,CAAC;QAED,IAAI,aAAa,IAAI,KAAK,CAAC,OAAO,CAAC,mBAAmB,CAAC,WAAW,CAAC,EAAE,CAAC;YACpE,mBAAmB,CAAC,WAAW,CAAC,OAAO,CAAC,UAAC,UAAU,EAAE,KAAK;gBACxD,aAAa,GAAG,aAAa,CAAC,OAAO,CACnC,IAAI,MAAM,CAAC,uBAAgB,KAAK,GAAG,CAAC,CAAE,EAAE,GAAG,CAAC,EAC5C,kBAAkB,CAAC,UAAU,CAAC,CAC/B,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC;QAED,IACE,aAAa;YACb,mBAAmB,CAAC,KAAK,KAAK,SAAS;YACvC,mBAAmB,CAAC,KAAK,KAAK,IAAI;YAClC,CAAC,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,mBAAmB,CAAC,KAAK,CAAC;YAE1E,aAAa,GAAG,aAAa,CAAC,OAAO,CAAC,UAAU,EAAE,mBAAmB,CAAC,KAAK,CAAC,CAAC;QAC/E,IAAI,aAAa;YAAE,aAAa,GAAG,aAAa,CAAC,OAAO,CAAC,aAAa,EAAE,mBAAmB,CAAC,QAAQ,CAAC,CAAC;QACtG,IAAI,aAAa;YAAE,aAAa,GAAG,aAAa,CAAC,OAAO,CAAC,WAAW,EAAE,mBAAmB,CAAC,UAAU,CAAC,CAAC;QAEtG,OAAO,aAAa,CAAC;IACvB,CAAC;IACH,sBAAC;AAAD,CAAC,AAjCD,IAiCC","sourcesContent":["import { ValidationArguments } from './ValidationArguments';\n\n/**\n * Convert the constraint to a string to be shown in an error\n */\nexport function constraintToString(constraint: unknown): string {\n if (Array.isArray(constraint)) {\n return constraint.join(', ');\n }\n\n if (typeof constraint === 'symbol') {\n constraint = constraint.description;\n }\n\n return `${constraint}`;\n}\n\nexport class ValidationUtils {\n static replaceMessageSpecialTokens(\n message: string | ((args: ValidationArguments) => string),\n validationArguments: ValidationArguments\n ): string {\n let messageString: string;\n if (message instanceof Function) {\n messageString = (message as (args: ValidationArguments) => string)(validationArguments);\n } else if (typeof message === 'string') {\n messageString = message;\n }\n\n if (messageString && Array.isArray(validationArguments.constraints)) {\n validationArguments.constraints.forEach((constraint, index) => {\n messageString = messageString.replace(\n new RegExp(`\\\\$constraint${index + 1}`, 'g'),\n constraintToString(constraint)\n );\n });\n }\n\n if (\n messageString &&\n validationArguments.value !== undefined &&\n validationArguments.value !== null &&\n ['string', 'boolean', 'number'].includes(typeof validationArguments.value)\n )\n messageString = messageString.replace(/\\$value/g, validationArguments.value);\n if (messageString) messageString = messageString.replace(/\\$property/g, validationArguments.property);\n if (messageString) messageString = messageString.replace(/\\$target/g, validationArguments.targetName);\n\n return messageString;\n }\n}\n"]}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
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 (g && (g = 0, op[0] && (_ = 0)), _) 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 };
|
||||
}
|
||||
};
|
||||
import { ValidationExecutor } from './ValidationExecutor';
|
||||
/**
|
||||
* Validator performs validation of the given object based on its metadata.
|
||||
*/
|
||||
var Validator = /** @class */ (function () {
|
||||
function Validator() {
|
||||
}
|
||||
/**
|
||||
* Performs validation of the given object based on decorators or validation schema.
|
||||
*/
|
||||
Validator.prototype.validate = function (objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
|
||||
return this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions);
|
||||
};
|
||||
/**
|
||||
* Performs validation of the given object based on decorators or validation schema and reject on error.
|
||||
*/
|
||||
Validator.prototype.validateOrReject = function (objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
|
||||
return __awaiter(this, void 0, void 0, function () {
|
||||
var errors;
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions)];
|
||||
case 1:
|
||||
errors = _a.sent();
|
||||
if (errors.length)
|
||||
return [2 /*return*/, Promise.reject(errors)];
|
||||
return [2 /*return*/];
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
/**
|
||||
* Performs validation of the given object based on decorators or validation schema.
|
||||
*/
|
||||
Validator.prototype.validateSync = function (objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
|
||||
var object = typeof objectOrSchemaName === 'string' ? objectOrValidationOptions : objectOrSchemaName;
|
||||
var options = typeof objectOrSchemaName === 'string' ? maybeValidatorOptions : objectOrValidationOptions;
|
||||
var schema = typeof objectOrSchemaName === 'string' ? objectOrSchemaName : undefined;
|
||||
var executor = new ValidationExecutor(this, options);
|
||||
executor.ignoreAsyncValidations = true;
|
||||
var validationErrors = [];
|
||||
executor.execute(object, schema, validationErrors);
|
||||
return executor.stripEmptyErrors(validationErrors);
|
||||
};
|
||||
// -------------------------------------------------------------------------
|
||||
// Private Properties
|
||||
// -------------------------------------------------------------------------
|
||||
/**
|
||||
* Performs validation of the given object based on decorators or validation schema.
|
||||
* Common method for `validateOrReject` and `validate` methods.
|
||||
*/
|
||||
Validator.prototype.coreValidate = function (objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
|
||||
var object = typeof objectOrSchemaName === 'string' ? objectOrValidationOptions : objectOrSchemaName;
|
||||
var options = typeof objectOrSchemaName === 'string' ? maybeValidatorOptions : objectOrValidationOptions;
|
||||
var schema = typeof objectOrSchemaName === 'string' ? objectOrSchemaName : undefined;
|
||||
var executor = new ValidationExecutor(this, options);
|
||||
var validationErrors = [];
|
||||
executor.execute(object, schema, validationErrors);
|
||||
return Promise.all(executor.awaitingPromises).then(function () {
|
||||
return executor.stripEmptyErrors(validationErrors);
|
||||
});
|
||||
};
|
||||
return Validator;
|
||||
}());
|
||||
export { Validator };
|
||||
//# sourceMappingURL=Validator.js.map
|
||||
Generated
Vendored
+1
File diff suppressed because one or more lines are too long
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=ValidatorConstraintInterface.js.map
|
||||
SerpentRace_Backend/node_modules/class-validator/esm5/validation/ValidatorConstraintInterface.js.map
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ValidatorConstraintInterface.js","sourceRoot":"","sources":["../../../src/validation/ValidatorConstraintInterface.ts"],"names":[],"mappings":"","sourcesContent":["import { ValidationArguments } from './ValidationArguments';\n/**\n * Custom validators must implement this interface to provide custom validation logic.\n */\nexport interface ValidatorConstraintInterface {\n /**\n * Method to be called to perform custom validation over given value.\n */\n validate(value: any, validationArguments?: ValidationArguments): Promise<boolean> | boolean;\n\n /**\n * Gets default message when validation for this constraint fail.\n */\n defaultMessage?(validationArguments?: ValidationArguments): string;\n}\n"]}
|
||||
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=ValidatorOptions.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ValidatorOptions.js","sourceRoot":"","sources":["../../../src/validation/ValidatorOptions.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Options passed to validator during validation.\n */\nexport interface ValidatorOptions {\n /**\n * If set to true then class-validator will print extra warning messages to the console when something is not right.\n */\n enableDebugMessages?: boolean;\n\n /**\n * If set to true then validator will skip validation of all properties that are undefined in the validating object.\n */\n skipUndefinedProperties?: boolean;\n\n /**\n * If set to true then validator will skip validation of all properties that are null in the validating object.\n */\n skipNullProperties?: boolean;\n\n /**\n * If set to true then validator will skip validation of all properties that are null or undefined in the validating object.\n */\n skipMissingProperties?: boolean;\n\n /**\n * If set to true validator will strip validated object of any properties that do not have any decorators.\n *\n * Tip: if no other decorator is suitable for your property use @Allow decorator.\n */\n whitelist?: boolean;\n\n /**\n * If set to true, instead of stripping non-whitelisted properties validator will throw an error\n */\n forbidNonWhitelisted?: boolean;\n\n /**\n * Groups to be used during validation of the object.\n */\n groups?: string[];\n\n /**\n * Set default for `always` option of decorators. Default can be overridden in decorator options.\n */\n always?: boolean;\n\n /**\n * If [groups]{@link ValidatorOptions#groups} is not given or is empty,\n * ignore decorators with at least one group.\n */\n strictGroups?: boolean;\n\n /**\n * If set to true, the validation will not use default messages.\n * Error message always will be undefined if its not explicitly set.\n */\n dismissDefaultMessages?: boolean;\n\n /**\n * ValidationError special options.\n */\n validationError?: {\n /**\n * Indicates if target should be exposed in ValidationError.\n */\n target?: boolean;\n\n /**\n * Indicates if validated value should be exposed in ValidationError.\n */\n value?: boolean;\n };\n\n /**\n * Fails validation for objects unknown to class-validator. Defaults to true.\n *\n * For instance, since a plain empty object has no annotations used for validation:\n * - `validate({})` // fails.\n * - `validate({}, { forbidUnknownValues: true })` // fails.\n * - `validate({}, { forbidUnknownValues: false })` // passes.\n * - `validate(new SomeAnnotatedEmptyClass(), { forbidUnknownValues: true })` // passes.\n */\n forbidUnknownValues?: boolean;\n\n /**\n * When set to true, validation of the given property will stop after encountering the first error. Defaults to false.\n */\n stopAtFirstError?: boolean;\n}\n"]}
|
||||
Reference in New Issue
Block a user