-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(env-vars): Add warning validation decorator (#8555)
Introduced a custom decorator 'WarningIf' to log warnings for specific environment variable conditions. Implemented this for SESSION_STORE_SECRET to ensure users change it from the default value.
- Loading branch information
Showing
3 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...src/engine/core-modules/environment/decorators/__tests__/assert-or-warn.decorator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'reflect-metadata'; | ||
import { IsString, validateSync } from 'class-validator'; | ||
import { plainToClass } from 'class-transformer'; | ||
|
||
import { AssertOrWarn } from 'src/engine/core-modules/environment/decorators/assert-or-warn.decorator'; | ||
|
||
describe('AssertOrWarn Decorator', () => { | ||
it('should pass validation if the condition is met', () => { | ||
class EnvironmentVariables { | ||
@AssertOrWarn((object, value) => value > 10, { | ||
message: 'Value should be higher than 10', | ||
}) | ||
someProperty!: number; | ||
} | ||
|
||
const validatedConfig = plainToClass(EnvironmentVariables, { | ||
someProperty: 15, | ||
}); | ||
|
||
const warnings = validateSync(validatedConfig, { groups: ['warning'] }); | ||
|
||
expect(warnings.length).toBe(0); | ||
}); | ||
|
||
it('should provide a warning message if the condition is not met', () => { | ||
class EnvironmentVariables { | ||
@AssertOrWarn((object, value) => value > 10, { | ||
message: 'Value should be higher than 10', | ||
}) | ||
someProperty!: number; | ||
} | ||
|
||
const validatedConfig = plainToClass(EnvironmentVariables, { | ||
someProperty: 9, | ||
}); | ||
|
||
const warnings = validateSync(validatedConfig, { groups: ['warning'] }); | ||
|
||
expect(warnings.length).toBe(1); | ||
expect(warnings[0].constraints!.AssertOrWarn).toBe( | ||
'Value should be higher than 10', | ||
); | ||
}); | ||
|
||
it('should not impact errors if the condition is not met', () => { | ||
class EnvironmentVariables { | ||
@IsString() | ||
unit: string; | ||
|
||
@AssertOrWarn( | ||
(object, value) => object.unit == 's' && value.toString().length <= 10, | ||
{ | ||
message: 'The unit is in seconds but the duration in milliseconds', | ||
}, | ||
) | ||
duration!: number; | ||
} | ||
|
||
const validatedConfig = plainToClass(EnvironmentVariables, { | ||
duration: 1731944140876000, | ||
unit: 's', | ||
}); | ||
|
||
const warnings = validateSync(validatedConfig, { groups: ['warning'] }); | ||
const errors = validateSync(validatedConfig, { strictGroups: true }); | ||
|
||
expect(errors.length).toBe(0); | ||
expect(warnings.length).toBe(1); | ||
expect(warnings[0].constraints!.AssertOrWarn).toBe( | ||
'The unit is in seconds but the duration in milliseconds', | ||
); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
.../twenty-server/src/engine/core-modules/environment/decorators/assert-or-warn.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
ValidationOptions, | ||
registerDecorator, | ||
ValidationArguments, | ||
} from 'class-validator'; | ||
|
||
export const AssertOrWarn = ( | ||
condition: (object: any, value: any) => boolean, | ||
validationOptions?: ValidationOptions, | ||
) => { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
name: 'AssertOrWarn', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: { | ||
...validationOptions, | ||
groups: ['warning'], | ||
}, | ||
constraints: [condition], | ||
validator: { | ||
validate(value: any, args: ValidationArguments) { | ||
return condition(args.object, value); | ||
}, | ||
defaultMessage(args: ValidationArguments) { | ||
return `'${args.property}' failed the warning validation.`; | ||
}, | ||
}, | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters