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

Handle const like enum with only one option #1446

Closed
wants to merge 4 commits into from

Conversation

navytux
Copy link

@navytux navytux commented Dec 8, 2023

By definition const: xxx is equivalent to enum: [xxx] : https://json-schema.org/draft/2020-12/json-schema-validation#name-const , so e.g. for

"abc": {
  "type": "string",
  "const": "ABC"
},
"def": {
  "type": "string",
  "enum": ["DEF"]
}

abc and def fields should be handled in the same way via select with only one item.

Fixes #1428

Q A
Is bugfix? ✔️
New feature? ✔️
Is backward-compatible? ✔️
Tests pass? ✔️
Fixed issues #1428
Updated README/docs?
Added CHANGELOG entry?

By definition `const: xxx` is equivalent to `enum: [xxx]` :
https://json-schema.org/draft/2020-12/json-schema-validation#name-const ,
so e.g. for

    "abc": {
      "type": "string",
      "const": "ABC"
    },
    "def": {
      "type": "string",
      "enum": ["DEF"]
    }

`abc` and `def` fields should be handled in the same way via select with
only one item.

Fixes json-editor#1428
@schmunk42
Copy link
Collaborator

@navytux
Copy link
Author

navytux commented Dec 8, 2023

@schmunk42, thanks for feedback.

Yes, those oneOf-2 and anyOf-2 tests needed to be changed, because those tests work on schemas with const and now const is automatically handled well. So instead of generating an error for e.g. oneOf subitem with const, such subitems are now automatically instantiated with their const value preselected. Previously those two tests were expecting const cases to emit errors, and now there is no error due to const-related behaviour change.

@navytux
Copy link
Author

navytux commented Dec 8, 2023

( here is the schema used in oneof-2 test: https://github.com/json-editor/json-editor/blob/67f29f49/tests/pages/oneof-2.html#L23-L67 )

@navytux
Copy link
Author

navytux commented Mar 26, 2024

@schmunk42, @germanbisurgi, do you think you could review this patch, or is there something wrong with it?

Offhand I would say this patch is much simpler compared to e.g. $ref resolver fixes, and it should be relatively straightforward to review.

Thanks beforehand for feedback,
Kirill

@schmunk42
Copy link
Collaborator

@navytux Sorry for that. We'll try to find time next week.

@germanbisurgi Please have a look, especially the changed tests ... but we might have checked a bug :)

@navytux
Copy link
Author

navytux commented Mar 26, 2024

Thanks, @schmunk42.

@UzairNoman
Copy link

UzairNoman commented Mar 27, 2024

I am facing the exact same issue where I have a select input and there is only one option:

image

this was working fine until i update my json schema to draft-07.

@navytux
Copy link
Author

navytux commented Apr 12, 2024

Hello @schmunk42 and @germanbisurgi.

Would you please have a look at this patch?

Basically it adjusts the editor to do what JSON-schema specification literally requires conformant implementations to do:

treat `const` as `enum` with single value

( https://json-schema.org/draft/2020-12/json-schema-validation#name-const )

So from that point of view this patch and adjustments to the tests should be obviously correct.

This patch was posted here back in December, but I see that in January another test was added that expects that it is possible to arbitrarily edit fields with const via UI:

febb98a6 (part of #1459)

which, from the point of view that "const is equivalent to enum with single value" is also incorrect and should be reworked, since e.g. for a select on enum: [true], it is not possible to input via UI anything but true.

On my side I prefer to get at least some high-level feedback first, before delving into any code rework, if at all.

I also understand that our pre-history on #1385 might be an issue. When I started my journey into JSON-schema world I was complete newbie and did not realize that in my case the editor will get already resolved schema and so there won't be the need to handle $ref inside the editor for me. So I first tried to fix all the encountered issues, but if you ask me, $ref parsing really needs a clean rewrite and is hard to consistently fix incrementally. So I've put that on hold first, and then, after realizing that I don't need $ref support from the editor, made a clear statement that I'm no longer actively looking into that issue. I appologize if my silence on #1385 might have created bad feelings on your side and caused your silence in hereby PR in return. I'm sorry for that.

Regarding changes themselves I would say that hereby const handling is different story though - it fixes aspect of core part of JSON-schema specification and the lack of the fix is causing real issues.

So I would appreciate if const topic could get your attention, and, one way or another we could add support for const in the way that JSON-schema specification describes.

Thanks beforehand for feedback,
Kirill

@germanbisurgi
Copy link
Collaborator

Hi @navytux, i am currently reviewing this your PR. The implementation looks good to me. I have to take a deeper look at the tests changes. Just wanted to add feedback at the moment.

@navytux
Copy link
Author

navytux commented Apr 16, 2024

Thanks, @germanbisurgi.

Copy link
Collaborator

@germanbisurgi germanbisurgi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will bring a minor breaking change in that the default keyword will be ognored, for example:

"string": {
  "title": "Some string",
  "type": "string",
  "const": "test",
  "default": "not in const"
}

"not in const" will be ignored and never shown.

Otherwise it looks good to me. The master branch should be merged into this PR before we merge it.

@schmunk42 Do you think it would be a good idea to add this feature behind a option like, "const_to_enum"?

@germanbisurgi
Copy link
Collaborator

@navytux This is ready to merge this, but there is a small conflict. could you resolve it in your branch please?

@germanbisurgi
Copy link
Collaborator

So, i resolve the merge conflict and fixed some linting errors introduced by myself (no linting in GitHub editor) but it has still test not passing. @navytux could you check on this and update code?

@navytux
Copy link
Author

navytux commented Apr 24, 2024

@germanbisurgi thanks for feedback and updating the branch, and @schmunk42 thanks for approving this PR.

I'm currently travelling and will try to have a look to finalize this PR in several days.

Sorry for the delay, and thanks, once again,
Kirill

@germanbisurgi
Copy link
Collaborator

Hello @navytux, upon further exploration of the 'const' topic, we've decided to pursue an alternative approach to ensure better backward compatibility for this feature, while retaining the integrity of the existing tests. You can review the PR here: #1555. Your input on this would be greatly appreciated.

@navytux
Copy link
Author

navytux commented Apr 28, 2024

@germanbisurgi, thanks for doing the alternative patch. #1555 works for me.

@navytux navytux closed this Apr 28, 2024
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 this pull request may close these issues.

const is not handled properly
4 participants