payload icon indicating copy to clipboard operation
payload copied to clipboard

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

Open jhb-dev opened this issue 1 year ago • 4 comments

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: https://github.com/payloadcms/payload/blob/f6194c6c91d3759783579b96e0ffc18c2fc6d7ad/packages/payload/src/fields/validations.ts#L373

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

jhb-dev avatar Nov 06 '25 09:11 jhb-dev

To work around this issue, the @cfworker/json-schema package can be used in a custom validation function:

import { validations } from 'payload'

{
    name: 'design-tokens',
    type: 'json',
    required: true,
    // Workaround for issue: https://github.com/payloadcms/payload/issues/14504
    validate: (value, options) => {
      const result = validations.json(value, options)

      // return result if Payload's JSON validation failed
      if (typeof result === 'string') {
        return result
      } else {
        // validate the schema using @cfworker/json-schema

        const validator = new Validator(designTokensJsonSchema.schema as Schema)
        const result = validator.validate(value)

        if (!result.valid) {
          return result.errors.map((error) => error.error).join(', ')
        } else {
          return true
        }
      }
    },
    // Disabled because of issue: https://github.com/payloadcms/payload/issues/14504
    // jsonSchema: designTokensJsonSchema,
},

jhb-dev avatar Nov 06 '25 10:11 jhb-dev

Ajv has been removed from payload core and replaced with Zod!

nathanlentz avatar Sep 09 '26 23:09 nathanlentz

That's great news @nathanlentz!

Does this mean this issue is only closed for V4 or will the fix also be ported to V3?

jhb-dev avatar Sep 10 '26 07:09 jhb-dev

@jhb-dev I am in favor of this only being a v4 fix. Zod is not a dependency in v3 and I don't want to swap the validation behavior there.

nathanlentz avatar Sep 10 '26 16:09 nathanlentz