Skip to content

Commit

Permalink
Merge pull request #1702 from strongloop/fix/nested-property-resolution
Browse files Browse the repository at this point in the history
fix: primitive data type coercion
  • Loading branch information
b-admike committed Apr 16, 2019
2 parents fe83f3c + 48af738 commit 47addd9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/dao.js
Expand Up @@ -2301,6 +2301,7 @@ DataAccessObject.updateAll = function(where, data, options, cb) {
if (!(data instanceof Model)) {
try {
inst = new Model(data, {applyDefaultValues: false});
ctx.data = inst.toObject(true);
} catch (err) {
return cb(err);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/list.js
Expand Up @@ -67,8 +67,14 @@ function List(items, itemType, parent) {
if (isClass(this.itemType)) {
return new this.itemType(item);
} else {
if (Array.isArray(item)) return item;
else return this.itemType(item);
if (Array.isArray(item)) {
return item;
} else if (this.itemType === Date) {
if (item === null) return null;
return new Date(item);
} else {
return this.itemType(item);
}
}
};

Expand Down
34 changes: 34 additions & 0 deletions test/basic-querying.test.js
Expand Up @@ -1055,6 +1055,40 @@ describe('basic-querying', function() {
});
});

describe('updateAll', function() {
it('coerces primitive datatypes on update', async () => {
const numAndDateModel = db.define('numAndDateModel', {
dateProp: Date,
dateArray: [Date],
numProp: Number,
numArray: [Number],
});
const createDate = new Date('2019-02-21T12:00:00').toISOString();
const createData = {
dateProp: createDate,
dateArray: [createDate, createDate],
numProp: '1',
numArray: ['1', '2'],
};
const updateDate = new Date('2019-04-15T12:00:00').toISOString();
const updateData = {
dateProp: updateDate,
dateArray: [updateDate, updateDate],
numProp: '3',
numArray: ['3', '4'],
};
const created = await numAndDateModel.create(createData);
const updated = await numAndDateModel.updateAll({id: created.id}, updateData);
const found = await numAndDateModel.findById(created.id);
found.dateProp.should.deepEqual(new Date(updateDate));
found.dateArray[0].should.deepEqual(new Date(updateDate));
found.dateArray[1].should.deepEqual(new Date(updateDate));
found.numProp.should.equal(3);
found.numArray[0].should.equal(3);
found.numArray[1].should.equal(4);
});
});

context('regexp operator', function() {
const invalidDataTypes = [0, true, {}, [], Function, null];

Expand Down
48 changes: 48 additions & 0 deletions test/datatype.test.js
Expand Up @@ -52,6 +52,54 @@ describe('datatypes', function() {
});
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',
function(done) {
Expand Down
2 changes: 1 addition & 1 deletion test/manipulation.test.js
Expand Up @@ -2366,7 +2366,7 @@ describe('manipulation', function() {
it('should not coerce invalid values provided in where conditions', function(done) {
Person.update({name: 'Brett Boe'}, {dob: 'notadate'}, function(err) {
should.exist(err);
err.message.should.equal('Invalid date: notadate');
err.message.should.equal('Invalid date: Invalid Date');
done();
});
});
Expand Down

0 comments on commit 47addd9

Please sign in to comment.