mirror of
https://github.com/Expand-sys/ccashfrontend
synced 2025-12-20 16:42:13 +11:00
139 lines
No EOL
6 KiB
JavaScript
139 lines
No EOL
6 KiB
JavaScript
import { ValidationSchemaToMetadataTransformer } from '../validation-schema/ValidationSchemaToMetadataTransformer';
|
|
import { getGlobal } from '../utils';
|
|
/**
|
|
* 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: false,
|
|
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, always, strictGroups, groups) {
|
|
var includeMetadataBecauseOfAlwaysOption = function (metadata) {
|
|
// `metadata.always` overrides global default.
|
|
if (typeof metadata.always !== 'undefined')
|
|
return metadata.always;
|
|
// `metadata.groups` overrides global default.
|
|
if (metadata.groups && metadata.groups.length)
|
|
return false;
|
|
// Use global default.
|
|
return always;
|
|
};
|
|
var excludeMetadataBecauseOfStrictGroupsOption = function (metadata) {
|
|
if (strictGroups) {
|
|
// Validation is not using groups.
|
|
if (!groups || !groups.length) {
|
|
// `metadata.groups` has at least one group.
|
|
if (metadata.groups && metadata.groups.length)
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
// get directly related to a target metadatas
|
|
var originalMetadatas = this.validationMetadatas.filter(function (metadata) {
|
|
if (metadata.target !== targetConstructor && metadata.target !== targetSchema)
|
|
return false;
|
|
if (includeMetadataBecauseOfAlwaysOption(metadata))
|
|
return true;
|
|
if (excludeMetadataBecauseOfStrictGroupsOption(metadata))
|
|
return false;
|
|
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 against 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 (includeMetadataBecauseOfAlwaysOption(metadata))
|
|
return true;
|
|
if (excludeMetadataBecauseOfStrictGroupsOption(metadata))
|
|
return false;
|
|
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 };
|
|
/**
|
|
* Gets metadata storage.
|
|
* Metadata storage follows the best practices and stores metadata in a global variable.
|
|
*/
|
|
export function getMetadataStorage() {
|
|
var global = getGlobal();
|
|
if (!global.classValidatorMetadataStorage) {
|
|
global.classValidatorMetadataStorage = new MetadataStorage();
|
|
}
|
|
return global.classValidatorMetadataStorage;
|
|
}
|
|
//# sourceMappingURL=MetadataStorage.js.map
|