Skip to content

Commit

Permalink
fix: zod 3.20.0 issues
Browse files Browse the repository at this point in the history
- recursive type definition
- exact flag for too_big and too_small:
colinhacks/zod#1620
- options property of ZodDiscriminatedUnion was renamed to optionsMap:
colinhacks/zod#1290 (comment)
  • Loading branch information
dcroote committed Dec 17, 2022
1 parent 894ffa2 commit f67853e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/airnode-validator/src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('parseConfigWithSecrets', () => {
minimum: 1,
type: 'string',
inclusive: true,
exact: false,
message: 'Secret cannot be empty',
path: ['AIRNODE_WALLET_MNEMONIC'],
},
Expand Down
3 changes: 3 additions & 0 deletions packages/airnode-validator/src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('gasPriceOracleSchema', () => {
minimum: 1,
type: 'array',
inclusive: true,
exact: false,
message: 'Array must contain at least 1 element(s)',
path: [],
},
Expand Down Expand Up @@ -379,6 +380,7 @@ describe('apiKey schemas', () => {
minimum: 30,
type: 'string',
inclusive: true,
exact: false,
message: 'String must contain at least 30 character(s)',
path: ['apiKey'],
},
Expand All @@ -393,6 +395,7 @@ describe('apiKey schemas', () => {
maximum: 120,
type: 'string',
inclusive: true,
exact: false,
message: 'String must contain at most 120 character(s)',
path: ['apiKey'],
},
Expand Down
45 changes: 20 additions & 25 deletions packages/airnode-validator/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,28 @@ export const gasPriceOracleStrategySchema = z.discriminatedUnion('gasPriceStrate
constantGasPriceStrategySchema,
]);

export const validateGasPriceOracleStrategies: SuperRefinement<GasPriceOracleConfig> = (gasPriceOracle, ctx) => {
const constantGasPriceStrategy = gasPriceOracle.find(
(gasPriceOracleStrategy) => gasPriceOracleStrategy.gasPriceStrategy === 'constantGasPrice'
);

// Require at least the constantGasPrice strategy to be defined
if (!constantGasPriceStrategy) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Missing required constantGasPrice strategy`,
path: ['gasPriceOracle'],
});
}

if (gasPriceOracle[gasPriceOracle.length - 1]?.gasPriceStrategy !== 'constantGasPrice') {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `ConstantGasPrice strategy must be set as the last strategy in the array.`,
path: ['gasPriceOracle'],
});
}
};

export const gasPriceOracleSchema = z
.array(gasPriceOracleStrategySchema)
.nonempty()
.superRefine(validateGasPriceOracleStrategies);
.superRefine((strategies, ctx) => {
const constantGasPriceStrategy = strategies.find((strategy) => strategy.gasPriceStrategy === 'constantGasPrice');

if (!constantGasPriceStrategy) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Missing required constantGasPrice strategy`,
path: ['gasPriceOracle'],
});
}

if (strategies[strategies.length - 1]?.gasPriceStrategy !== 'constantGasPrice') {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `ConstantGasPrice strategy must be set as the last strategy in the array.`,
path: ['gasPriceOracle'],
});
}
});

export const chainOptionsSchema = z
.object({
Expand Down Expand Up @@ -492,4 +487,4 @@ export type Amount = SchemaType<typeof amountSchema>;
export type EnabledGateway = SchemaType<typeof enabledGatewaySchema>;
export type MaxConcurrency = SchemaType<typeof maxConcurrencySchema>;

export const availableCloudProviders = Array.from(cloudProviderSchema.options.keys()) as CloudProvider['type'][];
export const availableCloudProviders = Array.from(cloudProviderSchema.optionsMap.keys()) as CloudProvider['type'][];

0 comments on commit f67853e

Please sign in to comment.