Skip to content

Commit 57cdc1e

Browse files
committed
Added support for schemas with type: ["string", "null"]. Just uses the first non-null type in the array.
1 parent f137cb2 commit 57cdc1e

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/js/Alpaca.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -943,31 +943,36 @@
943943
*/
944944
guessOptionsType: function(schema)
945945
{
946-
var type = null;
946+
// check if it has format defined
947+
if (schema.format && Alpaca.defaultFormatFieldMapping[schema.format])
948+
{
949+
return Alpaca.defaultFormatFieldMapping[schema.format];
950+
}
947951

948952
if (schema && typeof(schema["enum"]) !== "undefined")
949953
{
950954
if (schema["enum"].length > 3)
951955
{
952-
type = "select";
956+
return "select";
953957
}
954958
else
955959
{
956-
type = "radio";
960+
return "radio";
957961
}
958962
}
959-
else
960-
{
961-
type = Alpaca.defaultSchemaFieldMapping[schema.type];
962-
}
963963

964-
// check if it has format defined
965-
if (schema.format && Alpaca.defaultFormatFieldMapping[schema.format])
966-
{
967-
type = Alpaca.defaultFormatFieldMapping[schema.format];
964+
// type: ["string", "null"] is a valid way of defining an optional
965+
// field that can be either a string, or null. Use the first non-null type.
966+
var schemaType = schema.type
967+
if (Alpaca.isArray(schemaType)) {
968+
for (var i = 0; i < schemaType.length; i++) {
969+
if (schemaType[i] === 'null') continue;
970+
schemaType = schemaType[i];
971+
break;
972+
}
968973
}
969974

970-
return type;
975+
return Alpaca.defaultSchemaFieldMapping[schemaType];
971976
},
972977

973978
/**

0 commit comments

Comments
 (0)