Files
ccc-obfuscated-code/node_modules/class-validator/esm5/metadata/MetadataStorage.js
Pipi Chen b8a3516cd6 update
2020-05-07 01:02:44 +08:00

117 lines
4.9 KiB
JavaScript

import { ValidationSchemaToMetadataTransformer } from "../validation-schema/ValidationSchemaToMetadataTransformer";
/**
* Gets metadata storage.
* Metadata storage follows the best practices and stores metadata in a global variable.
*/
export function getMetadataStorage() {
if (typeof window !== "undefined") {
window.global = window;
}
if (!global.classValidatorMetadataStorage)
global.classValidatorMetadataStorage = new MetadataStorage();
return global.classValidatorMetadataStorage;
}
/**
* Storage all metadatas.
*/
var MetadataStorage = /** @class */ (function () {
function MetadataStorage() {
// -------------------------------------------------------------------------
// Private properties
// -------------------------------------------------------------------------
this.validationMetadatas = [];
this.constraintMetadatas = [];
}
Object.defineProperty(MetadataStorage.prototype, "hasValidationMetaData", {
get: function () {
return !!this.validationMetadatas.length;
},
enumerable: true,
configurable: true
});
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Adds a new validation metadata.
*/
MetadataStorage.prototype.addValidationSchema = function (schema) {
var _this = this;
var validationMetadatas = new ValidationSchemaToMetadataTransformer().transform(schema);
validationMetadatas.forEach(function (validationMetadata) { return _this.addValidationMetadata(validationMetadata); });
};
/**
* Adds a new validation metadata.
*/
MetadataStorage.prototype.addValidationMetadata = function (metadata) {
this.validationMetadatas.push(metadata);
};
/**
* Adds a new constraint metadata.
*/
MetadataStorage.prototype.addConstraintMetadata = function (metadata) {
this.constraintMetadatas.push(metadata);
};
/**
* Groups metadata by their property names.
*/
MetadataStorage.prototype.groupByPropertyName = function (metadata) {
var grouped = {};
metadata.forEach(function (metadata) {
if (!grouped[metadata.propertyName])
grouped[metadata.propertyName] = [];
grouped[metadata.propertyName].push(metadata);
});
return grouped;
};
/**
* Gets all validation metadatas for the given object with the given groups.
*/
MetadataStorage.prototype.getTargetValidationMetadatas = function (targetConstructor, targetSchema, groups) {
// get directly related to a target metadatas
var originalMetadatas = this.validationMetadatas.filter(function (metadata) {
if (metadata.target !== targetConstructor && metadata.target !== targetSchema)
return false;
if (metadata.always)
return true;
if (groups && groups.length > 0)
return metadata.groups && !!metadata.groups.find(function (group) { return groups.indexOf(group) !== -1; });
return true;
});
// get metadatas for inherited classes
var inheritedMetadatas = this.validationMetadatas.filter(function (metadata) {
// if target is a string it's means we validate agains a schema, and there is no inheritance support for schemas
if (typeof metadata.target === "string")
return false;
if (metadata.target === targetConstructor)
return false;
if (metadata.target instanceof Function &&
!(targetConstructor.prototype instanceof metadata.target))
return false;
if (metadata.always)
return true;
if (groups && groups.length > 0)
return metadata.groups && !!metadata.groups.find(function (group) { return groups.indexOf(group) !== -1; });
return true;
});
// filter out duplicate metadatas, prefer original metadatas instead of inherited metadatas
var uniqueInheritedMetadatas = inheritedMetadatas.filter(function (inheritedMetadata) {
return !originalMetadatas.find(function (originalMetadata) {
return originalMetadata.propertyName === inheritedMetadata.propertyName &&
originalMetadata.type === inheritedMetadata.type;
});
});
return originalMetadatas.concat(uniqueInheritedMetadatas);
};
/**
* Gets all validator constraints for the given object.
*/
MetadataStorage.prototype.getTargetValidatorConstraints = function (target) {
return this.constraintMetadatas.filter(function (metadata) { return metadata.target === target; });
};
return MetadataStorage;
}());
export { MetadataStorage };
//# sourceMappingURL=MetadataStorage.js.map