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

select correct oneOf based on specific property #709

Open
mbknor opened this issue Jan 3, 2017 · 3 comments
Open

select correct oneOf based on specific property #709

mbknor opened this issue Jan 3, 2017 · 3 comments

Comments

@mbknor
Copy link

mbknor commented Jan 3, 2017

We're using json-editor in combination with schemas generated by https://github.com/mbknor/mbknor-jackson-jsonSchema which uses oneOf to enable polymorphism.

When using polymorphism in json we use a special property to show which subclass the json represents.

Each oneOf-schema has this property embeded as a hidden single enum property with the "type info".
This ensures that the json extracted from the editor has correct type info corresponding with the oneOf which is selected.

When setting data into the editor using setValue, the multiple-editor tries to select the correct oneOf to use. This is done by selecting the one that has no validation errors.

We often need to show the editor with initial data that is not yet 100% valid.
Since the data is not fully valid, the multiple-editor does not know which oneOf to select always ending up with the first one.

Since we always have this special property with the "type info", I'm proposing a new feature to optionally select oneOf based on a custom property and value.

If the (part of) schema (that is "root" for the specific oneOf) has this option:

"options": {
  "multiple_editor_select_via_property": {
     "property": "myProperty",
     "value": "myValue"
  }
}

Then the multiple-editor will look for the property 'myProperty' in the value-object we're setting using setValue. If it is pressent, and has the value 'myValue', then we select this editor.

If not, we fallback to default implementation which selects it if it has no validation errors.

Im soon going to submit a pullRequest with this feature.

mbknor added a commit to mbknor/json-editor that referenced this issue Jan 3, 2017
mbknor added a commit to mbknor/json-editor that referenced this issue Jan 3, 2017
mbknor added a commit to mbknor/json-editor that referenced this issue Jan 3, 2017
@mbknor
Copy link
Author

mbknor commented Jan 3, 2017

PullRequest with fix: #710

@mbknor
Copy link
Author

mbknor commented Jan 3, 2017

This commits adds support for this feature in mbknor-jackson-jsonSchema:

mbknor/mbknor-jackson-jsonSchema@d729092

mbknor added a commit to mbknor/json-editor that referenced this issue Jan 3, 2017
@mobyjames
Copy link

This is fantastic and exactly what we needed for our project as well.

May I suggest adding support for checking nested properties by integrating a function like this into the check:

var nestedValue = function(obj, path) {
    if (obj === null) return null;
    path = path.split('.');
    var obj = obj[path.shift()];
    while(obj && path.length) obj = obj[path.shift()];
    return obj;
 };

if (typeof val !== "undefined" && val !== null && nestedValue(val, propName) == propValue) {
  ...
}

btsimonh pushed a commit to btsimonh/json-editor that referenced this issue May 26, 2021
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

No branches or pull requests

2 participants