Skip to content

Commit

Permalink
Added support for schemas with type: ["string", "null"]. Just uses th…
Browse files Browse the repository at this point in the history
…e first non-null type in the array.
  • Loading branch information
ndbroadbent committed Apr 13, 2020
1 parent f137cb2 commit 57cdc1e
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/js/Alpaca.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,31 +943,36 @@
*/
guessOptionsType: function(schema)
{
var type = null;
// check if it has format defined
if (schema.format && Alpaca.defaultFormatFieldMapping[schema.format])
{
return Alpaca.defaultFormatFieldMapping[schema.format];
}

if (schema && typeof(schema["enum"]) !== "undefined")
{
if (schema["enum"].length > 3)
{
type = "select";
return "select";
}
else
{
type = "radio";
return "radio";
}
}
else
{
type = Alpaca.defaultSchemaFieldMapping[schema.type];
}

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

return type;
return Alpaca.defaultSchemaFieldMapping[schemaType];
},

/**
Expand Down

0 comments on commit 57cdc1e

Please sign in to comment.