From 7f7d4ceb09048b63a8f7124638006b7a6fa5c48c Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Tue, 22 Aug 2023 12:59:37 -0500 Subject: [PATCH] add validation stuff --- .../src/domain/validation/common-validators.ts | 2 +- .../src/domain/validation/options-validators.ts | 2 +- .../src/domain/validation/validation-error.ts | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/consent/consent-tools/src/domain/validation/common-validators.ts b/packages/consent/consent-tools/src/domain/validation/common-validators.ts index 4d24e64e7..f33837500 100644 --- a/packages/consent/consent-tools/src/domain/validation/common-validators.ts +++ b/packages/consent/consent-tools/src/domain/validation/common-validators.ts @@ -11,7 +11,7 @@ export function assertIsFunction( export function assertIsObject( val: unknown, - variableName = 'value' + variableName: string ): asserts val is object { if (val === null || typeof val !== 'object') { throw new ValidationError(`${variableName} is not an object`, val) diff --git a/packages/consent/consent-tools/src/domain/validation/options-validators.ts b/packages/consent/consent-tools/src/domain/validation/options-validators.ts index 2abaf28c3..a07af6ac0 100644 --- a/packages/consent/consent-tools/src/domain/validation/options-validators.ts +++ b/packages/consent/consent-tools/src/domain/validation/options-validators.ts @@ -62,7 +62,7 @@ export function validateSettings(options: { export function validateAnalyticsInstance( analytics: unknown ): asserts analytics is AnyAnalytics { - assertIsObject(analytics) + assertIsObject(analytics, 'analytics') if ( 'load' in analytics && 'on' in analytics && diff --git a/packages/consent/consent-tools/src/domain/validation/validation-error.ts b/packages/consent/consent-tools/src/domain/validation/validation-error.ts index 174a3b31c..ccab434c2 100644 --- a/packages/consent/consent-tools/src/domain/validation/validation-error.ts +++ b/packages/consent/consent-tools/src/domain/validation/validation-error.ts @@ -1,10 +1,11 @@ import { AnalyticsConsentError } from '../../types/errors' export class ValidationError extends AnalyticsConsentError { - constructor(message: string, received: any) { - super( - 'ValidationError', - `[Validation] ${message} (${`Received: ${JSON.stringify(received)})`}` - ) + constructor(message: string, received?: any) { + if (arguments.length === 2) { + // to ensure that explicitly passing undefined as second argument still works + message += `(Received: ${JSON.stringify(received)})` + } + super('ValidationError', `[Validation] ${message}`) } }