From 3b7c79a8bcb6bd88d84d8d1e15f958be534997d3 Mon Sep 17 00:00:00 2001 From: Attila Olah Date: Tue, 1 May 2018 19:00:42 +0200 Subject: [PATCH] feat: add warning when no metadata is found during validation closes #132 --- src/metadata/MetadataStorage.ts | 4 ++++ src/validation/ValidationExecutor.ts | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/metadata/MetadataStorage.ts b/src/metadata/MetadataStorage.ts index 565f2ed447..18e4761eca 100644 --- a/src/metadata/MetadataStorage.ts +++ b/src/metadata/MetadataStorage.ts @@ -15,6 +15,10 @@ export class MetadataStorage { private validationMetadatas: ValidationMetadata[] = []; private constraintMetadatas: ConstraintMetadata[] = []; + get hasValidationMetaData() { + return !!this.validationMetadatas.length; + } + // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- diff --git a/src/validation/ValidationExecutor.ts b/src/validation/ValidationExecutor.ts index 5b27ca2e91..a2eef4727d 100644 --- a/src/validation/ValidationExecutor.ts +++ b/src/validation/ValidationExecutor.ts @@ -40,6 +40,16 @@ export class ValidationExecutor { // ------------------------------------------------------------------------- execute(object: Object, targetSchema: string, validationErrors: ValidationError[]) { + /** + * 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) { + console.warn(`No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.`); + } + const groups = this.validatorOptions ? this.validatorOptions.groups : undefined; const targetMetadatas = this.metadataStorage.getTargetValidationMetadatas(object.constructor, targetSchema, groups); const groupedMetadatas = this.metadataStorage.groupByPropertyName(targetMetadatas);