8000 Tweaks to #446 · js-data/js-data@7e98fcf · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e98fcf

Browse files
committed
Tweaks to #446
1 parent fb065db commit 7e98fcf

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
- `Schema#pick` now copies properties not defined in the "properties" keyword if the "additionalProperties" keyword is present and truthy.
77
- Mappers are no longer given an empty schema if no schema is provided
88

9+
###### Bug fixes
10+
- #446 - fix(Collection): Add noValidate option to Collection#add, by @ivanvoznyakovsky
11+
912
##### 3.0.0-rc.7 - 29 January 2017
1013

1114
###### Bug fixes

src/Collection.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import utils from './utils'
22
import Component from './Component'
33
import Query from './Query'
4+
import Record from './Record'
45
import Index from '../lib/mindex/index'
56

7+
const { noValidatePath } = Record
8+
69
const DOMAIN = 'Collection'
710

811
const COLLECTION_DEFAULTS = {
@@ -212,7 +215,7 @@ export default Component.extend({
212215
* @param {(Object|Object[]|Record|Record[])} data The record or records to insert.
213216
* @param {Object} [opts] Configuration options.
214217
* @param {boolean} [opts.commitOnMerge=true] See {@link Collection#commitOnMerge}.
215-
* @param {boolean} [opts.noValidate] See {@link Mapper#noValidate}.
218+
* @param {boolean} [opts.noValidate] See {@link Record#noValidate}.
216219
* @param {string} [opts.onConflict] See {@link Collection#onConflict}.
217220
* @returns {(Object|Object[]|Record|Record[])} The added record or records.
218221
*/
@@ -254,11 +257,13 @@ export default Component.extend({
254257
// Here, the currently visited record corresponds to a record already
255258
// in the collection, so we need to merge them
256259
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)
260+
if (onConflict !== 'merge' && onConflict !== 'replace') {
261+
throw utils.err(`${DOMAIN}#add`, 'opts.onConflict')(400, 'one of (merge, replace)', onConflict, true)
262+
}
263+
const existingNoValidate = existing._get(noValidatePath)
264+
if (opts.noValidate) {
265+
// Disable validation
266+
existing._set(noValidatePath, true)
262267
}
263268
if (onConflict === 'merge') {
264269
utils.deepMixIn(existing, record)
@@ -269,13 +274,11 @@ export default Component.extend({
269274
}
270275
})
271276
existing.set(record)
272-
} else {
273-
// restore prev `noValidate` before throwing if it was overriden for the record
274-
utils.isFunction(restoreNoValidate) && restoreNoValidate()
275-
throw utils.err(`${DOMAIN}#add`, 'opts.onConflict')(400, 'one of (merge, replace)', onConflict, true)
276277
}
277-
// restore prev `noValidate` value if it was overriden for the record
278-
utils.isFunction(restoreNoValidate) && restoreNoValidate()
278+
if (opts.noValidate) {
279+
// Restore previous `noValidate` value
280+
existing._set(noValidatePath, existingNoValidate)
281+
}
279282
record = existing
280283
if (opts.commitOnMerge && utils.isFunction(record.commit)) {
281284
record.commit()

src/Record.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ const previousPath = 'previous'
120120
* @param {Object} [opts] Configuration options.
121121
* @param {boolean} [opts.noValidate=false] Whether to skip validation on the
122122
* initial properties.
123+
* @param {boolean} [opts.validateOnSet=true] Whether to enable setter
124+
* validation on properties after the Record has been initialized.
123125
* @since 3.0.0
124126
*/
125127
function Record (props, opts) {
@@ -129,9 +131,7 @@ function Record (props, opts) {
129131
opts || (opts = {})
130132
const _set = this._set
131133
_set(creatingPath, true)
132-
if (opts.noValidate) {
133-
_set(noValidatePath, opts.noValidate === undefined ? true : opts.noValidate)
134-
}
134+
_set(noValidatePath, !!opts.noValidate)
135135
_set(keepChangeHistoryPath, opts.keepChangeHistory === undefined ? (mapper ? mapper.keepChangeHistory : true) : opts.keepChangeHistory)
136136

137137
// Set the idAttribute value first, if it exists.
@@ -143,8 +143,13 @@ function Record (props, opts) {
143143

144144
utils.fillIn(this, props)
145145
_set(creatingPath, false)
146-
const validateOnSet = opts.validateOnSet === undefined ? (mapper ? mapper.validateOnSet : true) : opts.validateOnSet
147-
_set(noValidatePath, !validateOnSet)
146+
if (opts.validateOnSet !== undefined) {
147+
_set(noValidatePath, !opts.validateOnSet)
148+
} else if (mapper && mapper.validateOnSet !== undefined) {
149+
_set(noValidatePath, !mapper.validateOnSet)
150+
} else {
151+
_set(noValidatePath, false)
152+
}
148153
_set(previousPath, mapper ? mapper.toJSON(props) : utils.plainCopy(props))
149154
}
150155

@@ -844,6 +849,11 @@ export default Component.extend({
844849
validate (opts) {
845850
return this._mapper().validate(this, opts)
846851
}
852+
}, {
853+
creatingPath,
854+
noValidatePath,
855+
keepChangeHistoryPath,
856+
previousPath
847857
})
848858

849859
/**

0 commit comments

Comments
 (0)
0