JSON field validation fails on Cloudflare Workers due to Ajv runtime code generation
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: 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
- 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
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,
},
Ajv has been removed from payload core and replaced with Zod!
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 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.