fix: coerce date array properties

This commit is contained in:
biniam 2019-04-16 10:50:54 -04:00
parent fe83f3cfd1
commit 1534392c62
2 changed files with 56 additions and 2 deletions

View File

@ -67,8 +67,14 @@ function List(items, itemType, parent) {
if (isClass(this.itemType)) { if (isClass(this.itemType)) {
return new this.itemType(item); return new this.itemType(item);
} else { } else {
if (Array.isArray(item)) return item; if (Array.isArray(item)) {
else return this.itemType(item); return item;
} else if (this.itemType === Date) {
if (item === null) return null;
return new Date(item);
} else {
return this.itemType(item);
}
} }
}; };

View File

@ -52,6 +52,54 @@ describe('datatypes', function() {
}); });
Account.definition.properties.item.type.should.not.equal(String); Account.definition.properties.item.type.should.not.equal(String);
}); });
it('should resolve array prop with connector specific metadata', function() {
const model = db.define('test', {
randomReview: {
type: [String],
mongodb: {
dataType: 'Decimal128',
},
},
});
model.definition.properties.randomReview.type.should.deepEqual(Array(String));
model.definition.properties.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'});
});
it('should coerce array of dates from string', async () => {
const dateArrayModel = db.define('dateArrayModel', {
bunchOfDates: [Date],
bunchOfOtherDates: {
type: [Date],
},
});
const dateVal = new Date('2019-02-21T12:00:00').toISOString();
const created = await dateArrayModel.create({
bunchOfDates: [dateVal,
dateVal,
dateVal],
bunchOfOtherDates: [dateVal,
dateVal,
dateVal],
});
created.bunchOfDates[0].should.be.an.instanceOf(Date);
created.bunchOfDates[0].should.deepEqual(new Date(dateVal));
created.bunchOfOtherDates[0].should.be.an.instanceOf(Date);
created.bunchOfOtherDates[0].should.deepEqual(new Date(dateVal));
});
it('should coerce array of numbers from string', async () => {
const numArrayModel = db.define('numArrayModel', {
bunchOfNums: [Number],
});
const dateVal = new Date('2019-02-21T12:00:00').toISOString();
const created = await numArrayModel.create({
bunchOfNums: ['1',
'2',
'3'],
});
created.bunchOfNums[0].should.be.an.instanceOf(Number);
created.bunchOfNums[0].should.equal(1);
});
it('should return 400 when property of type array is set to string value', it('should return 400 when property of type array is set to string value',
function(done) { function(done) {