Skip to content
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

Allow it to work on edge (e.g. cloudflare workers) #2318

Open
cosbgn opened this issue Aug 10, 2023 · 4 comments
Open

Allow it to work on edge (e.g. cloudflare workers) #2318

cosbgn opened this issue Aug 10, 2023 · 4 comments

Comments

@cosbgn
Copy link

cosbgn commented Aug 10, 2023

Currently AJV can't run on cloudflare workers, vercel-edge and other edge environments.
When trying to run it, the worker throws an error:

Code generation from strings disallowed for this context

Most likely this is because they restrict certain JavaScript functions like eval(), new Function(), setTimeout([string]), and setInterval([string]), which can execute code generated from strings.

I'm not sure why these functions are needed but it would be better to avoid them so that it could run everywhere without issues.

@s0j0hn
Copy link

s0j0hn commented Oct 6, 2023

Up !

@igorbrasileiro
Copy link

Up! Also a question, is it possible to use ajv in web worker?

@hieuhani
Copy link

hieuhani commented Apr 5, 2024

I use this library https://github.com/cfworker/cfworker/tree/main/packages/json-schema alternative

@jasoniangreen
Copy link
Collaborator

jasoniangreen commented Jun 22, 2024

Currently AJV can't run on cloudflare workers, vercel-edge and other edge environments. When trying to run it, the worker throws an error:

Code generation from strings disallowed for this context

Most likely this is because they restrict certain JavaScript functions like eval(), new Function(), setTimeout([string]), and setInterval([string]), which can execute code generated from strings.

I'm not sure why these functions are needed but it would be better to avoid them so that it could run everywhere without issues.

Thanks for the extra info @cosbgn. I have looked into it and I can only see that of the disallowed functions, AJV only uses new Function(), all within the compile functionality. This makes sense as in order to produce highly performant code, the compile logic does a lot of magic and basically creates optimised validation functions using this technique.

So I am curious, can you use AJV in cloudflare if you avoid the compile method. For example can you do this:

const Ajv = require("ajv");
const ajv = new Ajv();
const schema = {
  type: "object",
  properties: {
    foo: {type: "integer"},
  },
};
const data = {
  foo: 1,
};

const valid = ajv.validate(schema, data);

Otherwise I can only suggest prebuilding your validation functions in CI using ajv-cli like this:

ajv compile -s schema.json -o validate_schema.js

Then within your application code you can require the validation functions.

const validate = require('./validate_schema.js');

const data = { /* your data */ };
if (validate(data)) {
  console.log('Validation successful');
} else {
  console.error('Validation failed', validate.errors);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants