Skip to content

JSON field validation fails on Cloudflare Workers due to Ajv runtime code generation #14504

@jhb-dev

Description

@jhb-dev

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

Image

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:

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

  1. Use the Cloudflare D1 template
  2. add a json field 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"
               ]
            }
         }
      }
   }
}
  1. deploy to Cloudflare Workers
  2. try to save the document with the json field -> Error Message Code 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions