jsonnext / codemirror-json-schema

A JSONSchema enabled mode for codemirror 6, for json4 and json5, inspired by monaco-json
https://codemirror-json-schema.netlify.app
MIT License
65 stars 13 forks source link

Autocomplete for schema with conditions not working #129

Open andreyqin opened 5 months ago

andreyqin commented 5 months ago

Hi there! First of all, thanks for this library, it really helps me building a JSON editor with a custom json format and conveniently validate it. Unfortunately, I ran into an issue where JSON schema with conditions doesn't trigger autocomplete to show up. For instance, I have a json structure like below:

const schema = {
    type: 'object',
    properties: {
        type: {
            type: 'string',
        },
        props: {
            type: 'number',
        },
    },
    allOf: [
        {
            if: {
                properties: {
                    type: { const: 'Test' },
                },
            },
            then: {
                properties: {
                    props: { type: 'string', enum: ['a', 'b'] },
                },
            },
        },
        {
            if: {
                properties: {
                    type: { const: 'Test2' },
                },
            },
            then: {
                properties: {
                    props: { type: 'string', enum: ['c', 'e'] },
                },
            },
        },
    ],
};

And I expect that when I type in "Test" into "type" field I get "a" and "b" hints for "props" field, but instead I get nothing (it still shows that it's number type, although validation works correctly):

Screenshot 2024-06-10 at 17 12 16

Here is my React component:

<CodeMirror
            extensions={[
            json(),
            linter(jsonParseLinter()),
            linter(jsonSchemaLinter(), {
                needsRefresh: handleRefresh,
            }),
            jsonLanguage.data.of({
                autocomplete: jsonCompletion(),
            }),
            hoverTooltip(
                jsonSchemaHover({
                    getHoverTexts: getHoverText,
                    formatHover: createHoverTooltip,
                }),
                { hoverTime: 0 },
            ),
            stateExtensions(schema),
]}
            value={value}
            theme='dark'
            height='100%'
            className={cn(styles.root, className)}
            onChange={handleChange}
        />

I'm using: node v18.7.0, "codemirror-json-schema": "^0.7.8", "@codemirror/lang-json": "^6.0.1", "@uiw/react-codemirror": "^4.22.1".

imolorhe commented 4 months ago

Seems json-schema-library doesn't fully derive nested schema from a pointer based on the data