Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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