8000 fix(Collection): respect noValidate override passed in to find/findAl… · js-data/js-data@fb065db · GitHub
[go: up one dir, main page]

Skip to content

Commit fb065db

Browse files
ivanvoznyakovskyjmdobry
authored andcommitted
fix(Collection): respect noValidate override passed in to find/findAll when adding records (#446)
1 parent edeceeb commit fb065db

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Collection.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export default Component.extend({
212212
* @param {(Object|Object[]|Record|Record[])} data The record or records to insert.
213213
* @param {Object} [opts] Configuration options.
214214
* @param {boolean} [opts.commitOnMerge=true] See {@link Collection#commitOnMerge}.
215+
* @param {boolean} [opts.noValidate] See {@link Mapper#noValidate}.
215216
* @param {string} [opts.onConflict] See {@link Collection#onConflict}.
216217
* @returns {(Object|Object[]|Record|Record[])} The added record or records.
217218
*/
@@ -253,6 +254,12 @@ export default Component.extend({
253254
// Here, the currently visited record corresponds to a record already
254255
// in the collection, so we need to merge them
255256
const onConflict = opts.onConflict || this.onConflict
257+
let restoreNoValidate
258+
// `noValidate` might have been overriden in find/findAll. respect that
259+
if (opts.noValidate !== undefined) {
260+
restoreNoValidate = existing._set.bind(existing, 'noValidate', existing._get('noValidate'))
261+
existing._set('noValidate', opts.noValidate)
262+
}
256263
if (onConflict === 'merge') {
257264
utils.deepMixIn(existing, record)
258265
} else if (onConflict === 'replace') {
@@ -263,8 +270,12 @@ export default Component.extend({
263270
})
264271
existing.set(record)
265272
} else {
273+
// restore prev `noValidate` before throwing if it was overriden for the record
274+
utils.isFunction(restoreNoValidate) && restoreNoValidate()
266275
throw utils.err(`${DOMAIN}#add`, 'opts.onConflict')(400, 'one of (merge, replace)', onConflict, true)
267276
}
277+
// restore prev `noValidate` value if it was overriden for the record
278+
utils.isFunction(restoreNoValidate) && restoreNoValidate()
268279
record = existing
269280
if (opts.commitOnMerge && utils.isFunction(record.commit)) {
270281
record.commit()

test/unit/collection/add.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ describe('Collection#add', function () {
115115
collection.add({ id: 1 }, { onConflict: 'invalid_choice' })
116116
}, Error, `[Collection#add:opts.onConflict] expected: one of (merge, replace), found: invalid_choice\nhttp://www.js-data.io/v3.0/docs/errors#400`)
117117
})
118+
it('should respect opts.noValidate', function () {
119+
const mapper = new JSData.Mapper({
120+
name: 'user',
121+
noValidate: false,
122+
schema: {
123+
type: 'object',
124+
properties: {
125+
id: { type: 'string' },
126+
name: { type: 'string' }
127+
}
128+
}
129+
})
130+
const collection = new JSData.Collection({ mapper })
131+
const userData = { id: Math.random().toString(), name: Math.random().toString() }
132+
const user = collection.add(userData)
133+
assert.doesNotThrow(() => {
134+
collection.add(Object.assign({}, userData, { name: null }), { noValidate: true })
135+
})
136+
// original noValidate prop value should be restored
137+
assert.equal(user._get('noValidate'), false)
138+
})
118139
it('should required an argument', function () {
119140
const collection = new JSData.Collection()
120141
TYPES_EXCEPT_OBJECT_OR_ARRAY.forEach((value) => {

0 commit comments

Comments
 (0)
0