Skip to content

Commit 4499860

Browse files
committed
Merge pull request #74 from TylerBrock/add-ignore-validation
Add ability to ignore validation on Parse.Object constructor/set()
2 parents d1fabdf + e7e42a5 commit 4499860

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/ParseObject.js

Lines changed: 15 additions & 9 deletions
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
@@ -1359,11 +1365,11 @@ export default class ParseObject {
13591365
} else if (classMap[adjustedClassName]) {
13601366
parentProto = classMap[adjustedClassName].prototype;
13611367
}
1362-
var ParseObjectSubclass = function(attributes) {
1368+
var ParseObjectSubclass = function(attributes, options) {
13631369
this.className = adjustedClassName;
13641370
this._objCount = objectCount++;
13651371
if (attributes && typeof attributes === 'object'){
1366-
if (!this.set(attributes || {})) {
1372+
if (!this.set(attributes || {}, options)) {
13671373
throw new Error('Can\'t create an invalid Parse Object');
13681374
}
13691375
}

src/__tests__/ParseObject-test.js

Lines changed: 24 additions & 0 deletions
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)