Repository navigation
Description
Describe the Bug
Payload 4 renamed the field-level typescriptSchema to jsonSchema. The json field already had a jsonSchema key with a different meaning: the schema its value is validated against. The two now collide, and the migration guide's example for the rename happens to be a json field:
{
name: 'tags',
type: 'json',
jsonSchema: [() => ({ type: 'array', items: { type: 'string' } })],
}Using that example verbatim on 4.0.0-beta.7:
- It fails typecheck.
JSONFieldis{ jsonSchema?: { fileMatch; schema; uri } } & Omit<FieldBase, 'admin' | 'jsonSchema' | 'validate'>, so the transform array is rejected:error TS2739: Type '(() => { type: string; items: { type: string; }; })[]' is missing the following properties from type '{ fileMatch: string[]; schema: JSONSchema4; uri: string; }': fileMatch, schema, uri - Type generation does work.
configToJSONSchemaappliesjsonSchematransforms wheneverArray.isArray(field.jsonSchema), without looking at the field type. The generated type istags?: string[]. - Every save runs the json validator against the array. The validator calls
validateJSONSchemawheneverjsonSchemais truthy, and treats it as{ uri, schema }. With an array,uriandschemaareundefined, sofetch(undefined)is called and never awaited:- an unhandled rejection,
TypeError: Failed to parse URL from undefined, on every create/update; - the value is accepted (the validator returns
true); - the field config is mutated: the transform array gets a
.schemaproperty holding the Promise.
- an unhandled rejection,
So on v4 a json field can either validate against a schema or transform its generated type, and the documented way to do the latter breaks at runtime. On 3.x, JSONField was Omit<FieldBase, 'admin' | 'validate'>, so it kept typescriptSchema next to its own jsonSchema, and both worked.
Expected: the migration guide's example works on a json field, or the guide uses a field type where it does. A json field can set both a validation schema and type transforms, as on v3.
Also, related, current beta docs for json field has 2 jsonSchema fields in a table:
Link to the code that reproduces this issue
apps/next/collections/JsonTransforms.ts: the guide's example verbatim.@ts-expect-errormarks the TS2739.apps/next/helpers/seed.ts: creates a document on boot.
Code:
JSONFieldtype (Omit<FieldBase, 'admin' | 'jsonSchema' | 'validate'>)validateJSONSchema.ts#L45configToJSONSchema: theArray.isArray(field.jsonSchema)transform loop
Reproduction Steps
pnpm install && pnpm dev- On boot the seed saves
{ tags: ['a', 'b'] }and the log shows:unhandledRejection: Failed to parse URL from undefined [repro] json-transforms: saved 6aeccdf2b88da615a116ac52 payload-types.tshastags?: string[], so the transforms did run.- Remove the
@ts-expect-errorinJsonTransforms.tsand runtscto get TS2739.
Which area(s) are affected?
area: core
Environment Info
payload: 4.0.0-beta.7
Node: 24
OS: Windows 11
Activity
chore: add six more upstream fixes and regression coverage
fix: address 24 Payload read, schema, version and tooling reports (#9)