- Newest
- Most votes
- Most comments
You are correct that there are some limitations in function referencing within AWS AppSync JavaScript resolvers using the APPSYNC_JS runtime. The error you're encountering is due to restrictions in how functions can be used and passed around in this environment.
In the APPSYNC_JS runtime, there are several rules that limit function usage to prevent potential issues and ensure predictable behavior:
- Recursive function calls are not allowed.
- Passing functions as arguments to other functions is not permitted.
- Functions cannot be reassigned.
- Functions cannot be returned as values from other functions.
In your code, you're trying to store functions in an object (validation
) and then retrieve and call them dynamically. This pattern of passing functions around and calling them indirectly is not supported in the APPSYNC_JS runtime.
To work around this limitation, you might need to restructure your code. Instead of storing functions in an object and calling them dynamically, you could use a switch statement or if-else blocks to call the appropriate validation function directly based on the field name.
For example, you could modify your code like this:
function validateField(field, value) { switch(field) { case "field1": return isValidUUID(value); case "field2": return isValidEnumValue(value); // ... other cases ... default: return true; // or handle unknown fields as needed } } export function request(ctx) { for (const [field, value] of Object.entries(ctx.args.input)) { if (!validateField(field, value)) { // Handle validation failure } } return {}; }
This approach directly calls the validation functions without storing or passing them as variables, which should work within the constraints of the APPSYNC_JS runtime.
Sources
Configuring utilities for the APPSYNC_JS runtime - AWS AppSync
AWS AppSync JavaScript runtime features for resolvers and functions - AWS AppSync
Relevant content
- asked 2 years ago
- asked 6 months ago
- AWS OFFICIALUpdated 3 years ago
Understood, I'll try to rewrite the code, it should not be too big of a rewrite. Once I'm here, another question, part of the validation was relying on Regular expressions, which I know are not fully supported ( .match() is reported as Invalid function) is there any utils or workaround to validate strings against some sort of regex?