-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validation for doc mutations (#166)
- Loading branch information
Showing
8 changed files
with
227 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
var assign = require('lodash/assign') | ||
var validate = require('../validate') | ||
|
||
/** | ||
* change object either by passing changed properties | ||
* as an object, or by passing a change function that | ||
* manipulates the passed object directly | ||
**/ | ||
module.exports = function changeObject (change, object) { | ||
module.exports = function changeObject (state, change, object) { | ||
var updatedObject = assign({}, object) | ||
|
||
if (typeof change === 'object') { | ||
return assign(object, change) | ||
updatedObject = assign(object, change) | ||
} else { | ||
change(updatedObject) | ||
} | ||
|
||
change(object) | ||
return object | ||
return validate(state, updatedObject) | ||
|
||
.then(function () { | ||
return updatedObject | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
var assign = require('lodash/assign') | ||
var changeObject = require('./change-object') | ||
|
||
// Normalizes objectOrId, applies changes if any, and mark as deleted | ||
module.exports = function markAsDeleted (change, objectOrId) { | ||
// Normalizes objectOrId, validates changes if any, applies them, and mark as deleted | ||
module.exports = function markAsDeleted (state, change, objectOrId) { | ||
var object = typeof objectOrId === 'string' ? { _id: objectOrId } : objectOrId | ||
|
||
if (change) { | ||
changeObject(change, object) | ||
return changeObject(state, change, object) | ||
|
||
.then(function (doc) { | ||
return assign({_deleted: true}, doc) | ||
}) | ||
} | ||
|
||
return assign({_deleted: true}, object) | ||
return Promise.resolve(assign({_deleted: true}, object)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters