Skip to content

Commit

Permalink
Return meaningful error for undefined record key
Browse files Browse the repository at this point in the history
refs #113
  • Loading branch information
Steven-Chan authored and rickmak committed Aug 15, 2017
1 parent 1c2892e commit c24af27
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/skygear-core/lib/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ export default class Record {
_access: this._access && this._access.toJSON()
};
_.each(this.attributeKeys, (key) => {
payload[key] = toJSON(this[key]);
const value = this[key];
if (value === undefined) {
throw new Error(`Unsupported undefined value of record key: ${key}`);
}
payload[key] = toJSON(value);
});

return payload;
Expand Down
4 changes: 4 additions & 0 deletions packages/skygear-core/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import Geolocation from './geolocation';
import {UnknownValue} from './type';

export function toJSON(v) {
if (v === undefined) {
throw new Error('toJSON does not support undefined value');
}

if (v === null) {
return null;
} else if (_.isArray(v)) {
Expand Down
10 changes: 10 additions & 0 deletions packages/skygear-core/test/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ describe('Extended Record', function () {
});
});

it('serialize with undefined value', function () {
const note = new Note({
_id: 'note/uid',
content: undefined
});

expect(() => note.toJSON())
.to.throw('Unsupported undefined value of record key: content');
});

it('deserialize from payload with date', function () {
const note = new Note({
_id: 'note/uid',
Expand Down
5 changes: 5 additions & 0 deletions packages/skygear-core/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,10 @@ describe('util', function () {
]);
});

it('toJSON undefined', function () {
expect(() => toJSON(undefined))
.to.throw('toJSON does not support undefined value');
});

});
/*eslint-enable no-unused-vars, quote-props */

0 comments on commit c24af27

Please sign in to comment.