[v4] json field: the migration guide's `jsonSchema` transform example fails typecheck and runs the json validator against `fetch(undefined)`
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
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
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:
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-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
- Lingua principale
- TypeScript
- Stelle
- 45.8k
- Fork
- 4.4k
- Merge medio
- 3g 4h
- PR unite (30g)
- 210
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di payloadcms/payload
-
area: ui Bug Payload OSS
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
payloadcms/payload#19839 ·
I maintainer di solito rispondono entro 1 giorno
-
Accessibility: list row and select-all checkboxes still unnamed in 3.x (#17376 fix not in 3.91)ApertaBug Payload OSS
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
payloadcms/payload#19837 ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Payload OSS
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
payloadcms/payload#19835 ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Payload OSS
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
payloadcms/payload#19833 ·
I maintainer di solito rispondono entro 1 giorno
-
Bug Payload OSS
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
payloadcms/payload#19832 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di payloadcms/payload
Issue simili
-
[BUG]: Shared links drop the configured basePath on GitHub PagesForse già presa @anshulchikhale30-p l’ha presa oggi. Apertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
StabilityNexus/IdentityTokens-EVM-Frontend#182 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Add: GTrekkerApertacheck:passed streams:add
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
Koenkk/zigbee-herdsman-converters#13900 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
jeanibarz/knowledge-base-mcp-server#1291 ·
I maintainer di solito rispondono entro 1 giorno