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

Add partial form support #449

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

englercj
Copy link
Contributor

@englercj englercj commented Jul 1, 2015

This adds a new option root_schema that is a dot-notated string that describes a sub-section of a schema to use when generating the form.

This is useful when you have a schema with definitions and other shared items, but different parts of the schema need to be in different forms (for example in different tabs like #331). This makes that possible.

Usage example:

var editor = new JSONEditor(element, {
    schema: {
        "$schema": "http://json-schema.org/schema#",
        "definitions": { /* ... */ },
        "type": "object",
        "properties": {
            "containers": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {               
                        "id": { "type": "string" },
                        "base": { "$ref": "#/definitions/containerInfo" },
                        "variant": { "$ref": "#/definitions/containerInfo" }
                    }
                }
            }
        }
    },
    root_schema: "properties.containers.items.properties.base"
});

This will generate a form for just the "base" object, which in my case is necessary. I can then put the variant form on a separate tab, and keep all the other values ("id", etc) out of the form completely.

@englercj
Copy link
Contributor Author

englercj commented Jul 1, 2015

Might make more sense to call this subschema instead or something?

@jdorn
Copy link
Owner

jdorn commented Jul 3, 2015

It seems like this might be better as a pre-processing step before instantiating a JSON Editor instance. That way there's no need to mess with the core, which may have unintended consequences. For example:

// Full schema
var schema = {...};

// Get subschema from path (is there a better function name?)
var subschema = JSONEditor.getSubschema(schema, "properties.containers.items.properties.base");

// Use this subschema when creating the editor
var editor = new JSONEditor(element, {
  schema: subschema
});

@englercj
Copy link
Contributor Author

englercj commented Jul 3, 2015

But then you lose references to definitions in the schema, since the subschema doesn't contain them.

If you do getSubschema it has to return a schema that includes the definitions and changes only the root properties object. Is that something that you want?

@englercj
Copy link
Contributor Author

englercj commented Jul 6, 2015

I actually tried the subschema helper method, and made it preserve the definitions and such. Here is what it looks like:

JSONEditor.getSubschema = function (schema, path) {
    return {
        $schema: schema.$schema,
        definitions: schema.definitions ? $.extend(true, {}, schema.definitions) : undefined,
        type: schema.type,
        properties: $.extend(true, {}, $walk(path, schema.properties))
    };
};

Ignore the use of jQuery's extend there, just needed a quick deep clone. The problem is if you use it like this:

// Get subschema from path (is there a better function name?)
var subschema = JSONEditor.getSubschema(schema, "containers.items.properties.base");

You get out a schema that looks like:

    {
        "$schema": "http://json-schema.org/schema#",
        "definitions": { /* ... */ },
        "type": "object",
        "properties": {
            "$ref": "#/definitions/containerInfo"
        }
    }

And that doesn't work, the $ref isn't resolved.

@jdorn
Copy link
Owner

jdorn commented Jul 6, 2015

JSONEditor.getSubschema would have to first flatten the schema (resolve all definitions and references) before returning the subschema. There are already methods in the core to do most of this, so it shouldn't be too hard to implement.

@englercj
Copy link
Contributor Author

englercj commented Jul 6, 2015

That makes sense, but AFAIK the methods that would be what I used for this are instance methods. So you would have to create the instance first, flatten the schema using utility methods on the instance, then use that flattened schema to create another JSONEditor instance with the correct schema.

I feel like there is a better way...

englercj added a commit to englercj/json-editor that referenced this pull request Jul 14, 2015
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.

2 participants