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

Incoming data quoted "false" treated as true #724

Open rhinmass opened 5 years ago

rhinmass commented 5 years ago

If the data has quotes around the "false" , it is parsed as true. My understanding is that quotes around booleans is valid in json.

When alpaca is reading the data, it should know it's handling a boolean field and should interpret "false" as false. It seems looking in the source , this is the intent, as seen here in the function ensureProperType.

                        else if (type === "boolean")
                        {
                            if (v === "" || v.toLowerCase() === "false")
                            {
                                v = false;
                            }
                            else
                            {
                                v = true;
                            }
                        }

But this code doesn't seem to be getting called.

Here is a snippet to illustrate this case

           $(document).ready(function() {
                $("#form").alpaca({
                    "data": {
                        "name": "Diego Maradona",
                        "feedback": "Very impressive.",
                        "ranking": "excellent",
                        "recommend" : "false",
                    },              
                    "schema": {
                        "title":"User Feedback",
                        "description":"What do you think about Alpaca?",
                        "type":"object",
                        "properties": {
                            "name": {
                                "type":"string",
                                "title":"Name"
                            },
                            "feedback": {
                                "type":"string",
                                "title":"Feedback"
                            },
                            "ranking": {
                                "type":"string",
                                "title":"Ranking",
                                "enum":['excellent','ok','so so']
                            },
                            "recommend" : {
                                "type" : "boolean",
                                "title" : "Would you recommend us to a friend?"
                            },
                        }
                    },
                });
            });