gitana / alpaca

Alpaca provides the easiest way to generate interactive HTML5 forms for web and mobile applications. It uses JSON Schema and simple Handlebars templates to generate great looking, dynamic user interfaces on top of Twitter Bootstrap, jQuery UI, jQuery Mobile and HTML5.
http://www.alpacajs.org
Other
1.29k stars 371 forks source link

Add support for schemas with type: ["string", "null"] #747

Open ndbroadbent opened 4 years ago

ndbroadbent commented 4 years ago

Fixes #292

If a schema has type: ["string", "null"] or type: ["null", "integer"], etc., this change will just use the first non-null type from the array.

UPDATE: I just found some other cases where this needs to be handled: I wasn't able to update the value for any optional drop-down select lists (ListField). There might be a few other places with lines of code similar to this:

            if (!self.schema.type || self.schema.type === "string")

These will need to be updated to something like:

            if (!self.schema.type || self.schema.type === "string" || (
                Alpaca.isArray(self.schema.type) && 
                self.schema.type.length === 2 && 
                self.schema.type.indexOf("string") !== -1 && 
                self.schema.type.indexOf("null") !== -1))
            {

(But probably less verbose!)

Maybe there could be a generic "schema type checker" function that checks for any | null.

renannprado commented 4 years ago

Just came across alpaca for the first time and I face exactly this situation at the moment. It would be really nice if this fix is merged :D

evaisse commented 4 years ago

Hye, I've the same problems, could you merge this branch ?