-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Describe the Bug
When saving a document containing a json field which has a jsonSchema defined, the following error appears in the admin panel when deploying Payload to Cloudflare Workers:
Code generation from strings disallowed for this context
This happens because Cloudflare Workers do not allow eval or new Function, which Ajv uses during schema compilation, however Payload uses such when validating json fields with a jsonSchema defined:
| const ajv = new Ajv() |
Reference
From @cfworker/json-schema:
Cloudflare workers do not have APIs required by Ajv schema compilation (eval or new Function(code)). If possible use Ajv in a build step to precompile your schema. Otherwise this library could work for you.
Suggested fix:
Replace runtime Ajv validation in that block with a Workers-compatible validator such as @cfworker/json-schema, or precompile the schema during build.
Link to the code that reproduces this issue
https://github.com/payloadcms/payload/tree/main/templates/with-cloudflare-d1
Reproduction Steps
- Use the Cloudflare D1 template
- add a
jsonfield to a collection and specify a jsonSchema, e.g.:
{
"name":"customerJSON",
"type":"json",
"jsonSchema":{
"uri":"a://b/foo.json",
"fileMatch":[
"a://b/foo.json"
],
"schema":{
"type":"object",
"properties":{
"foo":{
"enum":[
"bar",
"foobar"
]
}
}
}
}
}- deploy to Cloudflare Workers
- try to save the document with the
jsonfield -> Error MessageCode generation from strings disallowed for this context
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
Binaries:
Node: 24.3.0
npm: 11.4.2
Yarn: N/A
pnpm: 10.12.4
Relevant Packages:
payload: 3.62.1
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6041
Available memory (MB): 24576
Available CPU cores: 14