Skip to content

Commit

Permalink
fix(unmarshal): handle $type: Array with non-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 17, 2017
1 parent 4ca9487 commit f65c063
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/unmarshal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ function visitArray(arr, schema, projection, path) {
let error = new ValidateError();
let curPath = realPathToSchemaPath(path);
let newPath = join(curPath, '$');
if (!schema._paths[newPath] || !schema._paths[newPath].$type) {
debug('skipping', newPath);
return {
value: arr,
error: null
};
}

if (arr == null) {
return {
Expand All @@ -99,6 +92,14 @@ function visitArray(arr, schema, projection, path) {
arr = [arr];
}

if (!schema._paths[newPath] || !schema._paths[newPath].$type) {
debug('skipping', newPath);
return {
value: arr,
error: null
};
}

debug('newPath', newPath, schema._paths[newPath].$type);
arr.forEach(function(value, index) {
if (schema._paths[newPath].$type === Array ||
Expand Down
9 changes: 9 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ describe('unmarshal()', function() {
});
});

it('boolean to array', function() {
let Band = new Archetype({
test: { $type: Array }
}).compile();

const res = new Band({ test: true });
assert.deepEqual(res.test, [true]);
});

it('casts deeply nested arrays', function() {
const Graph = new Archetype({
points: [[{ $type: 'number' }]]
Expand Down

0 comments on commit f65c063

Please sign in to comment.