Skip to content

Commit

Permalink
added more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaumaus committed Oct 13, 2023
1 parent 14521e3 commit a3d3cd6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
44 changes: 43 additions & 1 deletion apps/production/src/analytics/dto/events.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import * as _keys from 'lodash/keys'
import * as _values from 'lodash/values'
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty, IsOptional, IsObject } from 'class-validator'
import {
IsNotEmpty,
IsOptional,
IsObject,
ValidatorConstraint,
ValidatorConstraintInterface,
Validate,
} from 'class-validator'

const MAX_METADATA_KEYS = 20
const MAX_METADATA_VALUE_LENGTH = 1000

@ValidatorConstraint()
class MetadataSizeLimit implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
const values = _values(metadata)
let totalSize = 0

for (const value of values) {
totalSize += value.length
if (totalSize > MAX_METADATA_VALUE_LENGTH) {
return false
}
}

return true
}
}

@ValidatorConstraint()
class MetadataKeysQuantity implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
return _keys(metadata).length <= MAX_METADATA_KEYS
}
}

export class EventsDTO {
@ApiProperty({
Expand Down Expand Up @@ -76,5 +112,11 @@ export class EventsDTO {
})
@IsOptional()
@IsObject()
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})
meta?: Record<string, string>
}
44 changes: 43 additions & 1 deletion apps/selfhosted/src/analytics/dto/events.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import * as _keys from 'lodash/keys'
import * as _values from 'lodash/values'
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty, IsOptional, IsObject } from 'class-validator'
import {
IsNotEmpty,
IsOptional,
IsObject,
ValidatorConstraint,
ValidatorConstraintInterface,
Validate,
} from 'class-validator'

const MAX_METADATA_KEYS = 20
const MAX_METADATA_VALUE_LENGTH = 1000

@ValidatorConstraint()
class MetadataSizeLimit implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
const values = _values(metadata)
let totalSize = 0

for (const value of values) {
totalSize += value.length
if (totalSize > MAX_METADATA_VALUE_LENGTH) {
return false
}
}

return true
}
}

@ValidatorConstraint()
class MetadataKeysQuantity implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
return _keys(metadata).length <= MAX_METADATA_KEYS
}
}

export class EventsDTO {
@ApiProperty({
Expand Down Expand Up @@ -76,5 +112,11 @@ export class EventsDTO {
})
@IsOptional()
@IsObject()
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})
meta?: Record<string, string>
}

0 comments on commit a3d3cd6

Please sign in to comment.