Comparing version 0.6.1 to 0.6.2
@@ -82,10 +82,14 @@ 'use strict'; | ||
case 'array': | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (row[column]) { | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
} | ||
} else { | ||
row[column] = []; | ||
} | ||
@@ -96,82 +100,102 @@ | ||
case 'array.integer': | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (row[column]) { | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'number' || value % 1 !== 0) { | ||
throw new ValidationError("Not an array of integers"); | ||
} | ||
}); | ||
} else { | ||
row[column] = []; | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'number' || value % 1 !== 0) { | ||
throw new ValidationError("Not an array of integers"); | ||
} | ||
}); | ||
break; | ||
case 'array.string': | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (row[column]) { | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'string') { | ||
throw new ValidationError("Not an array of strings"); | ||
} | ||
}); | ||
} else { | ||
row[column] = []; | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'string') { | ||
throw new ValidationError("Not an array of strings"); | ||
} | ||
}); | ||
break; | ||
case 'array.boolean': | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (row[column]) { | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'boolean') { | ||
throw new ValidationError("Not an array of booleans"); | ||
} | ||
}); | ||
} else { | ||
row[column] = []; | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'boolean') { | ||
throw new ValidationError("Not an array of booleans"); | ||
} | ||
}); | ||
break; | ||
case 'array.float': | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (row[column]) { | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
if (!Array.isArray(row[column])) { | ||
throw new ValidationError("Data is not of type array: " + column); | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'number') { | ||
throw new ValidationError("Not an array of floats"); | ||
} | ||
}); | ||
} else { | ||
row[column] = []; | ||
} | ||
row[column].forEach(function(value) { | ||
if (typeof value !== 'number') { | ||
throw new ValidationError("Not an array of floats"); | ||
} | ||
}); | ||
break; | ||
case 'json': | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
if (row[column]) { | ||
try { | ||
row[column] = JSON.parse(row[column]); | ||
} catch(e) { | ||
throw new ValidationError("Unable to parse JSON: " + row[column]); | ||
} | ||
} else { | ||
row[column] = {}; | ||
} | ||
@@ -178,0 +202,0 @@ |
{ | ||
"name": "grille", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"description": "Simple CMS using Google Spreadsheets", | ||
@@ -5,0 +5,0 @@ "main": "lib/grille.js", |
@@ -358,2 +358,52 @@ 'use strict'; | ||
it("converts empty cells of type arrays to empty arrays", function() { | ||
var descriptors = { | ||
items: 'array', | ||
integers: 'array.integer', | ||
strings: 'array.string', | ||
flags: 'array.boolean', | ||
floats: 'array.float' | ||
}; | ||
var row = { | ||
items: '', | ||
integers: '', | ||
strings: '', | ||
flags: '', | ||
floats: '' | ||
}; | ||
var result = Worksheet.convertKeys(descriptors, row); | ||
assert(Array.isArray(result.items)); | ||
assert.strictEqual(result.items.length, 0); | ||
assert(Array.isArray(result.integers)); | ||
assert.strictEqual(result.integers.length, 0); | ||
assert(Array.isArray(result.strings)); | ||
assert.strictEqual(result.strings.length, 0); | ||
assert(Array.isArray(result.flags)); | ||
assert.strictEqual(result.flags.length, 0); | ||
assert(Array.isArray(result.floats)); | ||
assert.strictEqual(result.floats.length, 0); | ||
}); | ||
it("converts empty cells of type json to empty objects", function() { | ||
var descriptors = { | ||
blob: 'json' | ||
}; | ||
var row = { | ||
blob: '' | ||
}; | ||
var result = Worksheet.convertKeys(descriptors, row); | ||
assert.equal(typeof result.blob, 'object'); | ||
assert.deepEqual(result.blob, {}); | ||
}); | ||
describe("integration tests", function() { | ||
@@ -360,0 +410,0 @@ this.timeout(10 * 1000); |
59276
1460