-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from Kashoo/issue-98-custom-validation
Issue #98: Pass correct values to final param of custom validation constraints
- Loading branch information
Showing
9 changed files
with
116 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const testHelper = require('../src/testing/test-helper.js'); | ||
|
||
describe('Custom validation constraint:', () => { | ||
beforeEach(() => { | ||
testHelper.initSyncFunction('build/sync-functions/test-custom-validation-sync-function.js'); | ||
}); | ||
|
||
it('allows a document if custom validation succeeds', () => { | ||
const doc = { | ||
_id: 'my-doc', | ||
type: 'customValidationDoc', | ||
baseProp: { | ||
failValidation: false, | ||
customValidationProp: 'foo' | ||
} | ||
}; | ||
|
||
testHelper.verifyDocumentCreated(doc); | ||
}); | ||
|
||
it('blocks a document if custom validation fails', () => { | ||
const oldDoc = { | ||
_id: 'my-doc', | ||
type: 'customValidationDoc', | ||
baseProp: { } | ||
}; | ||
|
||
const doc = { | ||
_id: 'my-doc', | ||
type: 'customValidationDoc', | ||
baseProp: { | ||
failValidation: true, | ||
customValidationProp: 'foo' | ||
} | ||
}; | ||
|
||
const expectedCurrentItemEntry = { | ||
itemValue: doc.baseProp.customValidationProp, | ||
oldItemValue: null, | ||
itemName: 'customValidationProp' | ||
}; | ||
const expectedValidationItemStack = [ | ||
{ // The document (root) | ||
itemValue: doc, | ||
oldItemValue: oldDoc, | ||
itemName: null | ||
}, | ||
{ // The parent of the property with the customValidation constraint | ||
itemValue: doc.baseProp, | ||
oldItemValue: oldDoc.baseProp, | ||
itemName: 'baseProp' | ||
} | ||
]; | ||
|
||
testHelper.verifyDocumentNotReplaced( | ||
doc, | ||
oldDoc, | ||
'customValidationDoc', | ||
[ | ||
'doc: ' + JSON.stringify(doc), | ||
'oldDoc: ' + JSON.stringify(oldDoc), | ||
'currentItemEntry: ' + JSON.stringify(expectedCurrentItemEntry), | ||
'validationItemStack: ' + JSON.stringify(expectedValidationItemStack) | ||
]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function() { | ||
return { | ||
customValidationDoc: { | ||
typeFilter: simpleTypeFilter, | ||
channels: { write: 'write' }, | ||
propertyValidators: { | ||
baseProp: { | ||
type: 'object', | ||
propertyValidators: { | ||
failValidation: { | ||
type: 'boolean' | ||
}, | ||
customValidationProp: { | ||
type: 'string', | ||
customValidation: function(doc, oldDoc, currentItemEntry, validationItemStack) { | ||
var parentItemValue = validationItemStack[validationItemStack.length - 1].itemValue; | ||
if (parentItemValue && parentItemValue.failValidation) { | ||
return [ | ||
'doc: ' + jsonStringify(doc), | ||
'oldDoc: ' + jsonStringify(oldDoc), | ||
'currentItemEntry: ' + jsonStringify(currentItemEntry), | ||
'validationItemStack: ' + jsonStringify(validationItemStack) | ||
]; | ||
} else { | ||
return [ ]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} |
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