Skip to content

Commit f26bd9a

Browse files
committed
Add ability to ignore validation on Parse.Object constructor/set()
1 parent 4510c13 commit f26bd9a

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/ParseObject.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ var singleInstance = (!CoreManager.get('IS_NODE'));
8585
* @constructor
8686
* @param {String} className The class name for the object
8787
* @param {Object} attributes The initial set of data to store in the object.
88+
* @param {Object} options The options for this object instance.
8889
*/
8990
export default class ParseObject {
9091
/**
@@ -97,7 +98,7 @@ export default class ParseObject {
9798
_objCount: number;
9899
className: string;
99100

100-
constructor(className: ?string | { className: string, [attr: string]: mixed }, attributes?: { [attr: string]: mixed }) {
101+
constructor(className: ?string | { className: string, [attr: string]: mixed }, attributes?: { [attr: string]: mixed }, options?: { ignoreValidation: boolean }) {
101102
var toSet = null;
102103
this._objCount = objectCount++;
103104
if (typeof className === 'string') {
@@ -113,8 +114,11 @@ export default class ParseObject {
113114
toSet[attr] = className[attr];
114115
}
115116
}
117+
if (attributes && typeof attributes === 'object') {
118+
options = attributes;
119+
}
116120
}
117-
if (toSet && !this.set(toSet)) {
121+
if (toSet && !this.set(toSet, options)) {
118122
throw new Error('Can\'t create an invalid Parse Object');
119123
}
120124
// Enable legacy initializers
@@ -626,12 +630,14 @@ export default class ParseObject {
626630
}
627631

628632
// Validate changes
629-
var validation = this.validate(newValues);
630-
if (validation) {
631-
if (typeof options.error === 'function') {
632-
options.error(this, validation);
633+
if (!options.ignoreValidation) {
634+
var validation = this.validate(newValues);
635+
if (validation) {
636+
if (typeof options.error === 'function') {
637+
options.error(this, validation);
638+
}
639+
return false;
633640
}
634-
return false;
635641
}
636642

637643
// Consolidate Ops

src/__tests__/ParseObject-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ describe('ParseObject', () => {
108108
expect(o.attributes).toEqual({ value: 12 });
109109
});
110110

111+
it('can ignore validation if ignoreValidation option is provided', () => {
112+
class ValidatedObject extends ParseObject {
113+
validate(attrs) {
114+
if (attrs.hasOwnProperty('badAttr')) {
115+
return 'you have the bad attr';
116+
}
117+
}
118+
}
119+
120+
var o = new ValidatedObject({
121+
className: 'Item',
122+
value: 12,
123+
badAttr: true
124+
}, { ignoreValidation: true });
125+
126+
expect(o.attributes.value).toBe(12);
127+
expect(o.attributes.badAttr).toBe(true);
128+
});
129+
111130
it('can be inflated from server JSON', () => {
112131
var json = {
113132
className: 'Item',
@@ -500,6 +519,11 @@ describe('ParseObject', () => {
500519
}});
501520
});
502521

522+
it('ignores validation if ignoreValidation option is passed to set()', () => {
523+
var o = new ParseObject('Listing');
524+
expect(o.set('$$$', 'o_O', { ignoreValidation: true })).toBe(o);
525+
});
526+
503527
it('can test object validity', () => {
504528
// Note: an object should never become invalid through normal use, but
505529
// it's possible that someone could manipulate it to become invalid

0 commit comments

Comments
 (0)