Skip to content

Commit 7f124cf

Browse files
fix(ui): json schema (#11123)
Fixes #10166 [fix: json schema #11123 - Watch Video](https://www.loom.com/share/0f5199234ad1486f910a39165de837e5) - Using the same `URI` with the same `schema` will throw an error. - Using the same `URI` with a different `schema` will throw a warning (but still work). If you want to use the same schema on a different field, you need to define a different URI.
1 parent 6901b26 commit 7f124cf

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

packages/ui/src/fields/JSON/index.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22
import type { JSONFieldClientComponent } from 'payload'
33

4+
import { type OnMount } from '@monaco-editor/react'
45
import React, { useCallback, useEffect, useMemo, useState } from 'react'
56

67
import { CodeEditor } from '../../elements/CodeEditor/index.js'
@@ -9,9 +10,9 @@ import { useField } from '../../forms/useField/index.js'
910
import { withCondition } from '../../forms/withCondition/index.js'
1011
import { FieldDescription } from '../FieldDescription/index.js'
1112
import { FieldError } from '../FieldError/index.js'
13+
import './index.scss'
1214
import { FieldLabel } from '../FieldLabel/index.js'
1315
import { mergeFieldStyles } from '../mergeFieldStyles.js'
14-
import './index.scss'
1516
import { fieldBaseClass } from '../shared/index.js'
1617

1718
const baseClass = 'json-field'
@@ -55,23 +56,29 @@ const JSONFieldComponent: JSONFieldClientComponent = (props) => {
5556
validate: memoizedValidate,
5657
})
5758

58-
const handleMount = useCallback(
59+
const handleMount = useCallback<OnMount>(
5960
(editor, monaco) => {
6061
if (!jsonSchema) {
6162
return
6263
}
6364

64-
const existingSchemas = monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas || []
65-
const modelUri = monaco.Uri.parse(jsonSchema.uri)
66-
67-
const model = monaco.editor.createModel(JSON.stringify(value, null, 2), 'json', modelUri)
6865
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
6966
enableSchemaRequest: true,
70-
schemas: [...existingSchemas, jsonSchema],
67+
schemas: [
68+
...(monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas || []),
69+
jsonSchema,
70+
],
7171
validate: true,
7272
})
7373

74-
editor.setModel(model)
74+
const uri = jsonSchema.uri
75+
const newUri = uri.includes('?')
76+
? `${uri}&${crypto.randomUUID()}`
77+
: `${uri}?${crypto.randomUUID()}`
78+
79+
editor.setModel(
80+
monaco.editor.createModel(JSON.stringify(value, null, 2), 'json', monaco.Uri.parse(newUri)),
81+
)
7582
},
7683
[jsonSchema, value],
7784
)

0 commit comments

Comments
 (0)