json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

How to make array type schemas work without wrapping them inside an object type #158

Closed mohsen1 closed 9 years ago

mohsen1 commented 9 years ago

I have this very simple schema:

{
    "type": "array",
    "title": "People",
    "items": {
        "type": "object",
        "title": "Person",
        "properties": {
            "name": { "type": "string" }
        }
    }
}

I want to render a very basic form it but everything I tried didn't work except when I wrapped this into another schema that has "type": "object" and used "key": "my-schema".

How can I avoid the wrapping, if possible?

Dervisevic commented 9 years ago

You really can't avoid the wrapping as that is what makes it a JSON Schema, without it it just isn't one and won't be handled. For more information and examples about schemas check this: http://json-schema.org/example1.html

mohsen1 commented 9 years ago

I'm trying to use this module for our project at Swagger Editor. I believe according to JSON Schema meta schema {"type": "array", "items" {"type": "string"}} is a valid JSON Schema. It would be nice if this module worked with basic schemas like {"type": "string"}.

davidlgj commented 9 years ago

It's currently not possible, this is by design to simplify things (same in JSON Form) I guess it could be done but it is not as straight forward as you would think though since it would mean a lot of special corner cases regarding form keys. Like what is the form key for the "root" if it's not an object?

fiddur commented 9 years ago

@Dervisevic It is a correct schema…

I think it should be possible. The key as a json-pointer would be "/", but I guess it will require a lot of refactoring to make is so.

Dervisevic commented 9 years ago

@fiddur One day i will learn what a schema actually is.

But it is not this day

mohsen1 commented 9 years ago

@davidlgj because the schema I create the from from is dynamic, it's getting tricky to make it work with any kind of schema. For now I'm doing some massage on the schema to get around it. So here what I would expect from basic schemas

schema result
{} no form
{"type": "the-type"} a form with a single input with type set according to the-type
{"type": "array", "items": {"type": "object", "properties": "..."}} what you render without need of wrapping it into an object schema

Thanks everyone for helping out!

davidlgj commented 9 years ago

@mohsen1 the problem is not handling the schema, its handling the form, it could probably be done but requires some thinking about. My recomendation is to just wrap the schema in an schema "objec"t before you put it on the scope.

mohsen1 commented 9 years ago

@davidlgj I do it in my project right now. You can see how I'm doing it here. Because the schema I am dealing with is dynamic, it's kind of hard to do this wrapping and I'm not sure if it's going to work for all schemas. Hopefully you guys make it easier for me to deal with array schemas. BTW thank you for this great module! As you can see it's a major dependency in our project. :smile:

davidlgj commented 9 years ago

@mohsen1 I'm sorry, but this will be a "wontfix". :( To do an actual fix inside angular schema form would be to implement the exact same wrapping code you now do outside, and since its a fringe case I'm reluctant add it.