123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- var __values = (this && this.__values) || function(o) {
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
- if (m) return m.call(o);
- if (o && typeof o.length === "number") return {
- next: function () {
- if (o && i >= o.length) o = void 0;
- return { value: o && o[i++], done: !o };
- }
- };
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
- };
- 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;
- };
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
- if (ar || !(i in from)) {
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
- ar[i] = from[i];
- }
- }
- return to.concat(ar || Array.prototype.slice.call(from));
- };
- import { ValidationSchemaToMetadataTransformer } from '../validation-schema/ValidationSchemaToMetadataTransformer';
- import { getGlobal } from '../utils';
- /**
- * Storage all metadatas.
- */
- var MetadataStorage = /** @class */ (function () {
- function MetadataStorage() {
- // -------------------------------------------------------------------------
- // Private properties
- // -------------------------------------------------------------------------
- this.validationMetadatas = new Map();
- this.constraintMetadatas = new Map();
- }
- Object.defineProperty(MetadataStorage.prototype, "hasValidationMetaData", {
- get: function () {
- return !!this.validationMetadatas.size;
- },
- 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) {
- var existingMetadata = this.validationMetadatas.get(metadata.target);
- if (existingMetadata) {
- existingMetadata.push(metadata);
- }
- else {
- this.validationMetadatas.set(metadata.target, [metadata]);
- }
- };
- /**
- * Adds a new constraint metadata.
- */
- MetadataStorage.prototype.addConstraintMetadata = function (metadata) {
- var existingMetadata = this.constraintMetadatas.get(metadata.target);
- if (existingMetadata) {
- existingMetadata.push(metadata);
- }
- else {
- this.constraintMetadatas.set(metadata.target, [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 e_1, _a;
- 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 filteredForOriginalMetadatasSearch = this.validationMetadatas.get(targetConstructor) || [];
- var originalMetadatas = filteredForOriginalMetadatasSearch.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 filteredForInheritedMetadatasSearch = [];
- try {
- for (var _b = __values(this.validationMetadatas.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
- var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
- if (targetConstructor.prototype instanceof key) {
- filteredForInheritedMetadatasSearch.push.apply(filteredForInheritedMetadatasSearch, __spreadArray([], __read(value), false));
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- var inheritedMetadatas = filteredForInheritedMetadatasSearch.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.get(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
|