Skip to content

Commit 2e02f0f

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 2e02f0f

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src/js/Alpaca.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,26 @@
935935
return schemaType;
936936
},
937937

938+
/**
939+
* Returns the first non-null type for optional fields where schema.type is an array.
940+
* (type: ["string", "null"] is a valid way of defining an optional field type.)
941+
* If the array only contains a single value, then returns that value.
942+
* Returns schemaType if the value is not an array.
943+
* @param schemaType
944+
* @returns {string} the field type
945+
*/
946+
schemaTypeFromArray: function(schemaType)
947+
{
948+
if (!Alpaca.isArray(schemaType)) { return schemaType }
949+
if (schemaType.length === 1) { return schemaType[0] }
950+
for (var i = 0; i < schemaType.length; i++) {
951+
if (schemaType[i] === 'null') continue;
952+
return schemaType[i];
953+
}
954+
return null;
955+
},
956+
957+
938958
/**
939959
* Makes a best guess at the options field type if none provided.
940960
*
@@ -943,31 +963,28 @@
943963
*/
944964
guessOptionsType: function(schema)
945965
{
946-
var type = null;
966+
// check if it has format defined
967+
if (schema.format && Alpaca.defaultFormatFieldMapping[schema.format])
968+
{
969+
return Alpaca.defaultFormatFieldMapping[schema.format];
970+
}
947971

948972
if (schema && typeof(schema["enum"]) !== "undefined")
949973
{
950974
if (schema["enum"].length > 3)
951975
{
952-
type = "select";
976+
return "select";
953977
}
954978
else
955979
{
956-
type = "radio";
980+
return "radio";
957981
}
958982
}
959-
else
960-
{
961-
type = Alpaca.defaultSchemaFieldMapping[schema.type];
962-
}
963-
964-
// check if it has format defined
965-
if (schema.format && Alpaca.defaultFormatFieldMapping[schema.format])
966-
{
967-
type = Alpaca.defaultFormatFieldMapping[schema.format];
968-
}
969983

970-
return type;
984+
// type: ["string", "null"] is a valid way of defining an optional
985+
// field that can be either a string, or null. Use the first non-null type.
986+
var schemaType = Alpaca.schemaTypeFromArray(schema.type)
987+
return Alpaca.defaultSchemaFieldMapping[schemaType];
971988
},
972989

973990
/**

src/js/fields/list/ListField.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@
265265

266266
var val = null;
267267

268-
if (!self.schema.type || self.schema.type === "string")
268+
var schemaType = Alpaca.schemaTypeFromArray(self.schema.type);
269+
270+
if (!schemaType || schemaType === "string")
269271
{
270272
var array = [];
271273
for (var i = 0; i < this.data.length; i++) {
@@ -282,18 +284,18 @@
282284
val = array.join(",");
283285
}
284286
}
285-
else if (self.schema.type === "number")
287+
else if (schemaType === "number")
286288
{
287289
if (this.data.length > 0)
288290
{
289291
val = this.data[0].value;
290292
}
291293
}
292-
else if (self.schema.type === "boolean")
294+
else if (schemaType === "boolean")
293295
{
294296
val = (this.data.length > 0);
295297
}
296-
else if (self.schema.type === "array")
298+
else if (schemaType === "array")
297299
{
298300
var values = [];
299301
for (var i = 0; i < this.data.length; i++)
@@ -310,7 +312,7 @@
310312

311313
val = values;
312314
}
313-
else if (self.schema.type === "object")
315+
else if (schemaType === "object")
314316
{
315317
if (this.data.length > 0)
316318
{

0 commit comments

Comments
 (0)