Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added validation for metadata values types

Blue Mouse0fb88398be26
diff --git a/apps/production/src/analytics/dto/events.dto.ts b/apps/production/src/analytics/dto/events.dto.ts
index b2655e857..2b1134b2d 100644
--- a/apps/production/src/analytics/dto/events.dto.ts
+++ b/apps/production/src/analytics/dto/events.dto.ts
@@ -1,5 +1,6 @@
import * as _keys from 'lodash/keys'
import * as _values from 'lodash/values'
+import * as _some from 'lodash/some'
import { ApiProperty } from '@nestjs/swagger'
import {
IsNotEmpty,
@@ -37,6 +38,15 @@ export class MetadataKeysQuantity implements ValidatorConstraintInterface {
}
}
+@ValidatorConstraint()
+export class MetadataValueType implements ValidatorConstraintInterface {
+ validate(metadata: Record<string, string>) {
+ const values = _values(metadata)
+
+ return !_some(values, value => typeof value !== 'string')
+ }
+}
+
export class EventsDTO {
@ApiProperty({
example: 'aUn1quEid-3',
@@ -115,6 +125,9 @@ export class EventsDTO {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Validate(MetadataValueType, {
+ message: 'All of metadata object values must be strings',
+ })
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})
diff --git a/apps/production/src/analytics/dto/pageviews.dto.ts b/apps/production/src/analytics/dto/pageviews.dto.ts
index deb809921..e398a6b80 100644
--- a/apps/production/src/analytics/dto/pageviews.dto.ts
+++ b/apps/production/src/analytics/dto/pageviews.dto.ts
@@ -5,6 +5,7 @@ import {
MAX_METADATA_KEYS,
MAX_METADATA_VALUE_LENGTH,
MetadataKeysQuantity,
+ MetadataValueType,
MetadataSizeLimit,
} from './events.dto'
@@ -108,6 +109,9 @@ export class PageviewsDTO {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Validate(MetadataValueType, {
+ message: 'All of metadata object values must be strings',
+ })
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})
diff --git a/apps/selfhosted/src/analytics/dto/events.dto.ts b/apps/selfhosted/src/analytics/dto/events.dto.ts
index 16ad3c809..2b1134b2d 100644
--- a/apps/selfhosted/src/analytics/dto/events.dto.ts
+++ b/apps/selfhosted/src/analytics/dto/events.dto.ts
@@ -1,5 +1,6 @@
import * as _keys from 'lodash/keys'
import * as _values from 'lodash/values'
+import * as _some from 'lodash/some'
import { ApiProperty } from '@nestjs/swagger'
import {
IsNotEmpty,
@@ -37,6 +38,15 @@ export class MetadataKeysQuantity implements ValidatorConstraintInterface {
}
}
+@ValidatorConstraint()
+export class MetadataValueType implements ValidatorConstraintInterface {
+ validate(metadata: Record<string, string>) {
+ const values = _values(metadata)
+
+ return !_some(values, value => typeof value !== 'string')
+ }
+}
+
export class EventsDTO {
@ApiProperty({
example: 'aUn1quEid-3',
@@ -58,7 +68,7 @@ export class EventsDTO {
description:
'If true, only 1 event with the same ID will be saved per user session',
})
- unique: object
+ unique: boolean
// Tracking metrics
@ApiProperty({
@@ -115,6 +125,9 @@ export class EventsDTO {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Validate(MetadataValueType, {
+ message: 'All of metadata object values must be strings',
+ })
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})
diff --git a/apps/selfhosted/src/analytics/dto/pageviews.dto.ts b/apps/selfhosted/src/analytics/dto/pageviews.dto.ts
index deb809921..e398a6b80 100644
--- a/apps/selfhosted/src/analytics/dto/pageviews.dto.ts
+++ b/apps/selfhosted/src/analytics/dto/pageviews.dto.ts
@@ -5,6 +5,7 @@ import {
MAX_METADATA_KEYS,
MAX_METADATA_VALUE_LENGTH,
MetadataKeysQuantity,
+ MetadataValueType,
MetadataSizeLimit,
} from './events.dto'
@@ -108,6 +109,9 @@ export class PageviewsDTO {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Validate(MetadataValueType, {
+ message: 'All of metadata object values must be strings',
+ })
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})