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 30, 2020
1 parent f137cb2 commit 2e02f0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
45 changes: 31 additions & 14 deletions src/js/Alpaca.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,26 @@
return schemaType;
},

/**
* Returns the first non-null type for optional fields where schema.type is an array.
* (type: ["string", "null"] is a valid way of defining an optional field type.)
* If the array only contains a single value, then returns that value.
* Returns schemaType if the value is not an array.
* @param schemaType
* @returns {string} the field type
*/
schemaTypeFromArray: function(schemaType)
{
if (!Alpaca.isArray(schemaType)) { return schemaType }
if (schemaType.length === 1) { return schemaType[0] }
for (var i = 0; i < schemaType.length; i++) {
if (schemaType[i] === 'null') continue;
return schemaType[i];
}
return null;
},


/**
* Makes a best guess at the options field type if none provided.
*
Expand All @@ -943,31 +963,28 @@
*/
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];
}

return type;
// 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 = Alpaca.schemaTypeFromArray(schema.type)
return Alpaca.defaultSchemaFieldMapping[schemaType];
},

/**
Expand Down
12 changes: 7 additions & 5 deletions src/js/fields/list/ListField.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@

var val = null;

if (!self.schema.type || self.schema.type === "string")
var schemaType = Alpaca.schemaTypeFromArray(self.schema.type);

if (!schemaType || schemaType === "string")
{
var array = [];
for (var i = 0; i < this.data.length; i++) {
Expand All @@ -282,18 +284,18 @@
val = array.join(",");
}
}
else if (self.schema.type === "number")
else if (schemaType === "number")
{
if (this.data.length > 0)
{
val = this.data[0].value;
}
}
else if (self.schema.type === "boolean")
else if (schemaType === "boolean")
{
val = (this.data.length > 0);
}
else if (self.schema.type === "array")
else if (schemaType === "array")
{
var values = [];
for (var i = 0; i < this.data.length; i++)
Expand All @@ -310,7 +312,7 @@

val = values;
}
else if (self.schema.type === "object")
else if (schemaType === "object")
{
if (this.data.length > 0)
{
Expand Down

0 comments on commit 2e02f0f

Please sign in to comment.