-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: environment variables validation #216
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,77 @@ | ||
import { plainToClass, Transform } from 'class-transformer'; | ||
import { | ||
IsEnum, | ||
IsString, | ||
IsOptional, | ||
validateSync, | ||
Min, | ||
IsArray, | ||
ArrayMinSize, | ||
IsInt, | ||
IsArray, | ||
IsBoolean, | ||
ValidateIf, | ||
IsEnum, | ||
IsInt, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsPositive, | ||
IsString, | ||
IsUrl, | ||
Max, | ||
Min, | ||
ValidateIf, | ||
validateSync, | ||
} from 'class-validator'; | ||
import { Environment, LogLevel, LogFormat } from './interfaces'; | ||
import { Environment, LogLevel, LogFormat, Chain } from './interfaces'; | ||
import { NonEmptyArray } from '@lido-nestjs/execution/dist/interfaces/non-empty-array'; | ||
|
||
const toNumber = | ||
({ defaultValue }) => | ||
({ value }) => { | ||
if (value === '' || value == null || value == undefined) return defaultValue; | ||
if (value === '' || value == null) return defaultValue; | ||
return Number(value); | ||
}; | ||
|
||
export const toBoolean = (value: any): boolean => { | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
const toBoolean = ({ defaultValue }) => { | ||
return function({ value }) { | ||
if (value == null || value === '') { | ||
return defaultValue; | ||
} | ||
|
||
if (typeof value === 'number') { | ||
return !!value; | ||
} | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
|
||
const str = value.toString().toLowerCase().trim(); | ||
|
||
switch (str) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I did a similar solution for Ejector we came across the fact that in some environments boolean variables can be capitalized, it would be good to take this into account. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it has been taken into account. As you can see in the quoted code in line 38, I cast the input to lowercase before making comparisons. So, all strings in all registers should be accepted (uppercase, lowercase, capitalized). |
||
case 'true': | ||
case 'yes': | ||
case '1': | ||
return true; | ||
|
||
if (!(typeof value === 'string')) { | ||
return false; | ||
case 'false': | ||
case 'no': | ||
case '0': | ||
return false; | ||
|
||
default: | ||
return value; | ||
} | ||
} | ||
} | ||
|
||
switch (value.toLowerCase().trim()) { | ||
case 'true': | ||
case 'yes': | ||
case '1': | ||
return true; | ||
case 'false': | ||
case 'no': | ||
case '0': | ||
case null: | ||
return false; | ||
default: | ||
return false; | ||
const toArrayOfUrls = (url: string | null): string[] => { | ||
if (url == null || url === '') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it extra check ? Because all urls required, or depends on another config There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check protects the app from the situation when a user sets an empty string as a value of the Previously the Assume a user sets this in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good |
||
return []; | ||
} | ||
|
||
return url.split(',').map((str) => str.trim().replace(/\/$/, '')); | ||
}; | ||
|
||
export class EnvironmentVariables { | ||
@IsOptional() | ||
@IsEnum(Environment) | ||
@Transform(({ value }) => value || Environment.development) | ||
NODE_ENV: Environment = Environment.development; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Min(1) | ||
@Min(1025) | ||
@Max(65535) | ||
@Transform(toNumber({ defaultValue: 3000 })) | ||
PORT = 3000; | ||
|
||
|
@@ -68,19 +81,19 @@ export class EnvironmentVariables { | |
|
||
@IsOptional() | ||
@IsInt() | ||
@Min(1) | ||
@IsPositive() | ||
Amuhar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Transform(toNumber({ defaultValue: 5 })) | ||
GLOBAL_THROTTLE_TTL = 5; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Min(1) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 100 })) | ||
GLOBAL_THROTTLE_LIMIT = 100; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Min(1) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 1 })) | ||
GLOBAL_CACHE_TTL = 1; | ||
|
||
|
@@ -98,78 +111,102 @@ export class EnvironmentVariables { | |
@Transform(({ value }) => value || LogFormat.json) | ||
LOG_FORMAT: LogFormat = LogFormat.json; | ||
|
||
@IsNotEmpty() | ||
@IsArray() | ||
@ArrayMinSize(1) | ||
@Transform(({ value }) => value.split(',').map((url) => url.replace(/\/$/, ''))) | ||
@IsUrl({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this check run after Transform ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The short answer is yes. The It works this way.
|
||
require_protocol: true | ||
}, { | ||
each: true, | ||
}) | ||
@Transform(({ value }) => toArrayOfUrls(value)) | ||
PROVIDERS_URLS!: NonEmptyArray<string>; | ||
|
||
@IsInt() | ||
@IsNotEmpty() | ||
@IsEnum(Chain) | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
CHAIN_ID!: number; | ||
CHAIN_ID!: Chain; | ||
|
||
@IsNotEmpty() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you check how it behave now without @isnotempty() check ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't set
I don't think that we want to allow empty strings here. |
||
@IsString() | ||
DB_HOST!: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
DB_USER!: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
@IsNotEmpty() | ||
@IsString() | ||
DB_PASSWORD!: string; | ||
|
||
@ValidateIf((e) => !e.DB_PASSWORD) | ||
@IsString() | ||
@IsNotEmpty() | ||
DB_PASSWORD_FILE!: string; | ||
|
||
@IsNotEmpty() | ||
@IsString() | ||
DB_NAME!: string; | ||
|
||
@IsNotEmpty() | ||
@IsInt() | ||
@Min(1025) | ||
@Max(65535) | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
DB_PORT!: number; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 100 })) | ||
PROVIDER_JSON_RPC_MAX_BATCH_SIZE = 100; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 5 })) | ||
PROVIDER_CONCURRENT_REQUESTS = 5; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 10 })) | ||
PROVIDER_BATCH_AGGREGATION_WAIT_MS = 10; | ||
|
||
// Enable endpoints that use CL API for ejector | ||
@IsOptional() | ||
@IsBoolean() | ||
@Transform(({ value }) => toBoolean(value)) | ||
@Transform(toBoolean({ defaultValue: true })) | ||
VALIDATOR_REGISTRY_ENABLE = true; | ||
|
||
@ValidateIf((e) => e.VALIDATOR_REGISTRY_ENABLE === true) | ||
@ValidateIf((e) => e.VALIDATOR_REGISTRY_ENABLE) | ||
@IsNotEmpty() | ||
@IsArray() | ||
@ArrayMinSize(1) | ||
@Transform(({ value }) => value.split(',').map((url) => url.replace(/\/$/, ''))) | ||
@IsUrl({ | ||
require_protocol: true, | ||
}, { | ||
each: true, | ||
}) | ||
@Transform(({ value }) => toArrayOfUrls(value)) | ||
CL_API_URLS: string[] = []; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 5000 })) | ||
UPDATE_KEYS_INTERVAL_MS = 5000; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
@IsPositive() | ||
@Transform(toNumber({ defaultValue: 10000 })) | ||
UPDATE_VALIDATORS_INTERVAL_MS = 10000; | ||
|
||
@IsOptional() | ||
@IsInt() | ||
@IsPositive() | ||
@Transform(({ value }) => parseInt(value, 10)) | ||
@Transform(toNumber({ defaultValue: 1100 })) | ||
KEYS_FETCH_BATCH_SIZE = 1100; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if it is number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the previous code gave users too much unnecessary freedom in setting boolean values. I think it's better to be more strict here and allow users to set only "true" or "false" strings as valid values of boolean variables and throw errors otherwise.
But I understand that it is a breaking change. If we know or can reasonably assume that some KAPI users should be able to set "1" or "yes" as valid values of boolean variables for a good reason, or some users already doing it this way, we should revert back to the previous behavior.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have a lot of users, so i dont know
i think in guides was true/false value, so they used it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the results of today's internal discussion, it has been decided to revert back support of numerical values and special constants like "yes" and "no", but return an error if the
undefined
,null
or empty string was provided. I'll update the PR.@Amuhar please, let me know if you have any objections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, but i agree that just boolean values look better