From 93ca962e16d42636b0e4aeda8d6a0314f3c2e1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Mon, 4 Dec 2023 14:39:12 +0100 Subject: [PATCH] Validate signed API name triggers --- .../src/validation/schema.test.ts | 18 ++++++++++++++++ .../airnode-feed/src/validation/schema.ts | 21 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/airnode-feed/src/validation/schema.test.ts b/packages/airnode-feed/src/validation/schema.test.ts index b7831cdd..2e488076 100644 --- a/packages/airnode-feed/src/validation/schema.test.ts +++ b/packages/airnode-feed/src/validation/schema.test.ts @@ -93,3 +93,21 @@ test('validates trigger references', async () => { ]) ); }); + +test('trigger must point to a valid Signed API definition', async () => { + // As a note, having unused Signed API definitions is not an error. + const invalidConfig: Config = { + ...config, + signedApis: [{ ...config.signedApis[0]!, name: 'different-name' }], + }; + + await expect(async () => configSchema.parseAsync(invalidConfig)).rejects.toStrictEqual( + new ZodError([ + { + code: 'custom', + message: 'Unable to find signed API with name: localhost', + path: ['triggers', 'signedApiUpdates', 0, 'signedApiName'], + }, + ]) + ); +}); diff --git a/packages/airnode-feed/src/validation/schema.ts b/packages/airnode-feed/src/validation/schema.ts index c61a7ea2..ad88164f 100644 --- a/packages/airnode-feed/src/validation/schema.ts +++ b/packages/airnode-feed/src/validation/schema.ts @@ -225,6 +225,8 @@ export const signedApiSchema = z.strictObject({ url: z.string().url(), }); +export type SignedApi = z.infer; + export const signedApisSchema = z .array(signedApiSchema) .nonempty() @@ -241,6 +243,22 @@ export const signedApisSchema = z } }); +const validateSignedApiReferences: SuperRefinement<{ + triggers: Triggers; + signedApis: SignedApi[]; +}> = (config, ctx) => { + for (const [index, trigger] of config.triggers.signedApiUpdates.entries()) { + const api = config.signedApis.find((api) => api.name === trigger.signedApiName); + if (!api) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Unable to find signed API with name: ${trigger.signedApiName}`, + path: ['triggers', 'signedApiUpdates', index, 'signedApiName'], + }); + } + } +}; + export const oisesSchema = z.array(oisSchema); export const apisCredentialsSchema = z.array(config.apiCredentialsSchema); @@ -272,7 +290,8 @@ export const configSchema = z }) .superRefine(validateTemplatesReferences) .superRefine(validateOisReferences) - .superRefine(validateTriggerReferences); + .superRefine(validateTriggerReferences) + .superRefine(validateSignedApiReferences); export const encodedValueSchema = z.string().regex(/^0x[\dA-Fa-f]{64}$/);