[v4] json field: the migration guide's `jsonSchema` transform example fails typecheck and runs the json validator against `fetch(undefined)`

Aperta
#19,401 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
54/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
api, backend

Direzione di ricerca

Read the JSONField type in packages/payload/src/fields/config/types.ts and the validator in packages/payload/src/fields/validateJSONSchema.ts, then trace the jsonSchema transform loop in configToJSONSchema. Reproduce with the linked JsonTransforms.ts example and seed save. Done when the migration example typechecks, saves without the unhandled rejection, and a JSON field can use both schema validation and type transforms.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area: core Bug created-by: Contributor status: needs-triage v3
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:

  1. It fails typecheck. JSONField is { 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
    
  2. Type generation does work. configToJSONSchema applies jsonSchema transforms whenever Array.isArray(field.jsonSchema), without looking at the field type. The generated type is tags?: string[].
  3. Every save runs the json validator against the array. The validator calls validateJSONSchema whenever jsonSchema is truthy, and treats it as { uri, schema }. With an array, uri and schema are undefined, so fetch(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 .schema property holding the Promise.

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:

Image

https://payloadcms.com/docs/beta/fields/json

Link to the code that reproduces this issue

https://github.com/VeiaG/payload-4-reproductions/tree/repro-json-field-jsonschema-transforms

  • apps/next/collections/JsonTransforms.ts: the guide's example verbatim. @ts-expect-error marks the TS2739.
  • apps/next/helpers/seed.ts: creates a document on boot.

Code:

Reproduction Steps
  1. pnpm install && pnpm dev
  2. On boot the seed saves { tags: ['a', 'b'] } and the log shows:
    unhandledRejection: Failed to parse URL from undefined
    [repro] json-transforms: saved 6aeccdf2b88da615a116ac52
    
  3. payload-types.ts has tags?: string[], so the transforms did run.
  4. Remove the @ts-expect-error in JsonTransforms.ts and run tsc to get TS2739.
Which area(s) are affected?

area: core

Environment Info
payload: 4.0.0-beta.7
Node: 24
OS: Windows 11
Lingua principale
TypeScript
Stelle
45.8k
Fork
4.4k
Merge medio
3g 4h
PR unite (30g)
210

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di payloadcms/payload

Tutte le issue di payloadcms/payload

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.