codecombat / treema

jQuery plugin that generates HTML interfaces to edit JSON data defined by json-schema.
http://codecombat.github.io/treema/
MIT License
152 stars 36 forks source link

Support "autoPopulate" in addition to "default" and "required" #22

Open nwinter opened 10 years ago

nwinter commented 10 years ago

For objects, it will be really handy to have an autoPopulate array so that one could do schemas like this:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "default": "My Project"
      },
      "description": {
        "type": "string",
        "default": "A project I worked on.",
      },
      "picture": {
        "type": "string",
        "format": "image-file",
      },
      "link": {
        "type": "string",
        "default": "http://example.com"
      }
    },
    "required": [
      "name",
      "description",
      "picture"
    ],
    "autoPopulate": ['link']
  }
}

instead of like this:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "default": "My Project"
      },
      "description": {
        "type": "string",
        "default": "A project I worked on.",
      },
      "picture": {
        "type": "string",
        "format": "image-file",
      },
      "link": {
        "type": "string",
        "default": "http://example.com"
      }
    },
    "required": [
      "name",
      "description",
      "picture"
    ],
    "default": {
      "name": "My Project",
      "description": "A project I worked on.",
      "link": "http://example.com",
      "picture": ""
    }
  }
}

The difference is that we don't need to mirror the default values in two places–the autoPopulate will specify that by default, we will include the link property, and the default value can be grabbed from the leaf just like when we auto-populate required properties.