Skip to content

Commit

Permalink
Validate signed API name triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Dec 6, 2023
1 parent 43bf629 commit 93ca962
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/airnode-feed/src/validation/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
])
);
});
21 changes: 20 additions & 1 deletion packages/airnode-feed/src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export const signedApiSchema = z.strictObject({
url: z.string().url(),
});

export type SignedApi = z.infer<typeof signedApiSchema>;

export const signedApisSchema = z
.array(signedApiSchema)
.nonempty()
Expand All @@ -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);
Expand Down Expand Up @@ -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}$/);

Expand Down

0 comments on commit 93ca962

Please sign in to comment.