angular-schema-form icon indicating copy to clipboard operation
angular-schema-form copied to clipboard

Array of arbitrary types

Open sangel10 opened this issue 9 years ago • 18 comments

Is there any way to have an object with an ordered list of arbitrary types? I'm thinking of something like this:

blocks: {
  type: "object",
  items:[
    first:   { type: 'string' }
    second:  { type: 'number' }
    third:   { type: 'boolean' }
  ]
}

Where the each of the objects in items will get the appropriate form when you do

form: ['*']

If not how could I go about extending the schema to include this?

sangel10 avatar Nov 07 '14 13:11 sangel10

+1 doing like this https://stackoverflow.com/questions/10809459/how-to-write-a-json-schema-for-array-of-objects

but unsuccessful

isuvorov avatar Nov 10 '14 21:11 isuvorov

In JSON Schemas the type "array" have a property "items" that either is a sub-schema that validates what is a valid item in the array is regardless of what index it has, or the "items" property is an array.

If its an array the model only validates if each item in the list matches the schema on the exact same index.

Is that what you want? Currently schema form does not support it. What is the use case?

davidlgj avatar Nov 11 '14 07:11 davidlgj

Chiming in as I'm working together with @sangel10 on a project where we are using angular-schema-form.

Our use case is this: we're building a CMS-like app where editors can create stories that are stored in JSON format (CouchDB). The schema for a story defines fields like title, date, author. But the body of the story would be a collection of different pieces of content – we call them blocks – e.g.

  • a text block
  • an image block
  • another text block
  • an image gallery
  • another text block
  • a youtube video
  • … you get the point

For each of theses block types we have a separate schema definition. I believe we could create a schema for that by using anyOf from JSON schema draft v4 like this:

{
    "type": "object",
    "properties": {
        "title":  { "type": "string" },
        "author": { "type": "string" },
        "date":   { "type": "string", "format": "date" },
        "body":   {
            "type" : "array",
            "items" : { 
                "anyOf" : [ 
                    // the different schema definitions for blocks here
                    { "type" : "object", "properties" : { … }},
                    { "type" : "object", "properties" : { … }},
                    // ... or ideally use `$ref` to reference the schemas
                ]
            }
        }
    }
}

I think we would even be able to validate against this schema using tv4, but angular-schema-form obviously cannot build a form based on that because it currently lacks support for anyOf.

Do you have any suggestion for us what we could do? Right now we are dynamically creating multiple form instances (one for the root-level attributes, and one for each of the blocks) but that feels rather hacky.

philippbosch avatar Nov 11 '14 10:11 philippbosch

Sorry that it took so long for me to answer!

Proper support of anyOf is the correct solution. But you can probably hack an add-on together based on the array implementation. By adding custom buttons that add different form definitions (with their respective form.schema snippets attached) to the array.

How do you want the form to look? i.e. what is the UI you have/want?

davidlgj avatar Jan 07 '15 10:01 davidlgj

Sorry that it took so long for me to answer, too ;-)

The UI you described is exactly what we want. I'm going to give this a shot and try to implement what you suggested – an add-on based on the array implementation. Just wanted to make sure that there haven't been any developments in that area in the meantime that would suggest another approach?

Thanks for your help!

philippbosch avatar Mar 17 '15 11:03 philippbosch

@davidlgj also looking for something like this. I haven't find any jsonschema based form editors properly handling polymorphism (support for anyof / oneof as described at http://stackoverflow.com/questions/18375506/how-to-use-dependencies-in-json-schema-draft-04).

Imo, a good UI for this would be to have the user select the type (or alias) of the object to create from a dropdown. After the type has been selected the rest of the UI will be generated so the form can be edited.

YousefED avatar Apr 28 '15 10:04 YousefED

We did manage to build something along these lines, but it's a bit of a hack as it uses multiple schema form instances because of the lack of support for anyOf. But it looks pretty cool:

philippbosch avatar Apr 28 '15 10:04 philippbosch

Very cool! Would love to see support for this in the library / let me know if I can help with this

YousefED avatar Apr 28 '15 10:04 YousefED

That is super slick.

Dervisevic avatar Apr 28 '15 10:04 Dervisevic

@philippbosch thats super cool! Is it just as an add-on or did you have to modify asf?

davidlgj avatar Apr 28 '15 11:04 davidlgj

We didn't modify ASF. We just wrote a controller with some glue code. Every block (text, image, quote, …) is a separate <form> with a separate schema and form definition. We keep an array of these forms on the scope, and when the user submits, we aggregate all the forms into one object and store that in the DB.

It is a hack, but it works. Ideally we would be able to just use anyOf … ;-)

philippbosch avatar Apr 28 '15 11:04 philippbosch

Interesting! Yeah anyOf support is high on the list of todo's, at least implementing enough support so that it doesn't fail badly. That way we open up for making add-ons that use it. (as a side note I believe its hard to make a generic "default" type for anyOf that is actually useful out of the box, better to make a couple for different scenarios )

davidlgj avatar Apr 28 '15 11:04 davidlgj

Should we reopen this issue?

YousefED avatar Apr 28 '15 12:04 YousefED

Re-opening it as an enhancement request. Feel free to add comments about your needs. Especially UI wise and what the purpose of the form is.

davidlgj avatar Apr 28 '15 12:04 davidlgj

First time looking at this library but coming from using the http://jeremydorn.com/json-editor/ library in hopes of an angular library I can integrate with a little easier and still make use of my json-schema setup i am using for my administrative forms.

I think the UI generated by that plugin is generally adequate for most situations I have used it for in the past if you are looking for a reasonable default UI for anyOf/oneOf situations. I have a couple different use cases where I am using json schema to describe a list of differing objects depending on what my administrative users decide to configure into the database.

I read in a different issue that this might have been slated for mid august but it was dated a bit old. Is support for oneOf/anyOf still on track for sometime in August or has it been deferred?

Thanks in advance and keep up the good work this library looks really promising.

itozapata avatar Aug 19 '15 00:08 itozapata

I'm starting work on implementing oneOf/anyOf in a fork: https://github.com/DanielSchaffer/angular-schema-form

Feel free to give feedback on my progress. Thanks!

DanielSchaffer avatar Sep 17 '15 00:09 DanielSchaffer

Good luck!

nicklasb avatar Sep 17 '15 05:09 nicklasb

Is support still planned?

jscharett avatar May 23 '18 06:05 jscharett