Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The library ignores conditional schema structure #132

Open
andreyqin opened this issue Jun 18, 2024 · 1 comment · Fixed by #133
Open

The library ignores conditional schema structure #132

andreyqin opened this issue Jun 18, 2024 · 1 comment · Fixed by #133

Comments

@andreyqin
Copy link

Hi! Thanks for this library guys! I'm implementing a JSON editor and I want the user to get autocomplete in one field depending on the value of another field. For this purpose I've got the schema similar to below:

const schema = {
    type: 'object',
    properties: {
        type: {
            type: 'string',
            enum: ['Test_1', 'Test_2'],
        },
        props: {
            type: 'object',
        },
    },
    allOf: [
        {
            if: {
                properties: {
                    type: { const: 'Test_1' },
                },
            },
            then: {
                properties: {
                    props: {
                        properties: {
                            test1Props: { type: 'string' },
                        },
                        additionalProperties: false,
                    },
                },
            },
        },
        {
            if: {
                properties: {
                    type: { const: 'Test_2' },
                },
            },
            then: {
                properties: {
                    props: {
                        properties: {
                            test2Props: { type: 'number' },
                        },
                        additionalProperties: false,
                    },
                },
            },
        },
    ],
};

So I expect that when I'm typing in "Test_1" in type field, I get only test1Props as a suggestion in props field but instead I get both:

Screenshot 2024-06-18 at 10 58 21

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 imolorhe linked a pull request Jun 20, 2024 that will close this issue
acao pushed a commit that referenced this issue Jun 23, 2024
As mentioned in #132, the "calculated" sub schema for a path in the JSON
document can change based on the values of other fields in the document.
`json-schema-library` already has the feature to [get
schema](https://github.com/sagold/json-schema-library?tab=readme-ov-file#getschema)
with the data when the schema is dynamic.

To retrieve the data, I added `best-effort-json-parser` so we can get
_some_ data even if the JSON document isn't in a valid JSON state (which
is going to be the case while writing the document). Given a document in
the following state:

```json
{
  "type": "Test_2",
  "props": {
    te
  }
}
```

it is able to retrieve the data as:

```json
{
    "type": "Test_1",
    "props": {
        "te": null
    }
}
```

...which is sufficient context (at least for all the existing test
cases)

Other changes in this PR include:
- deleted unused (old) json-completion.ts file
- created the `DocumentParser` type and moved parsers into a separate
directory
- added `loglevel` for better log tracing (logs now point to the file
the logs come from as opposed to `debug.ts`)
@acao acao closed this as completed in #133 Jun 23, 2024
@acao acao reopened this Jul 4, 2024
@acao
Copy link
Collaborator

acao commented Jul 4, 2024

@imolorhe does that PR work with conditionals!? or did we close thia by accident

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants