Skip to content

Commit

Permalink
add validation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Aug 22, 2023
1 parent f695c54 commit 7f7d4ce
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
Original file line number Diff line number Diff line change
@@ -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}`)
}
}

0 comments on commit 7f7d4ce

Please sign in to comment.