-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Fix async validations #107
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ const dummyValidations: Record<string, any> = { | |
} | ||
|
||
if (errors.length < 1) { | ||
return; | ||
return true; | ||
} | ||
return errors.length > 1 ? errors : errors[0]; | ||
}, | ||
|
@@ -43,7 +43,7 @@ const dummyValidations: Record<string, any> = { | |
); | ||
}, | ||
async(_k: string, value: unknown) { | ||
return Promise.resolve(value); | ||
return Promise.resolve(value || ''); | ||
}, | ||
options(_k: string, value: unknown) { | ||
return !!value; | ||
|
@@ -1702,16 +1702,21 @@ describe('Unit | Utility | changeset', () => { | |
|
||
it('it accepts async validations', async () => { | ||
const dummyChangeset = Changeset(dummyModel, lookupValidator(dummyValidations)); | ||
/* const dummyChangeset = Changeset(dummyModel, dummyValidator); */ | ||
const expectedChanges = [{ key: 'async', value: true }]; | ||
const expectedError = { async: { validation: 'is invalid', value: 'is invalid' } }; | ||
|
||
await dummyChangeset.set('async', true); | ||
expect(dummyChangeset.changes).toEqual(expectedChanges); | ||
|
||
dummyChangeset.set('async', 'is invalid'); | ||
await dummyChangeset.save(); | ||
expect(dummyChangeset.error).toEqual({}); | ||
|
||
await dummyChangeset.validate(); | ||
expect(dummyChangeset.error).toEqual(expectedError); | ||
|
||
await dummyChangeset.save(); | ||
// save clears errors | ||
expect(dummyChangeset.error).toEqual({}); | ||
}); | ||
|
||
it('it clears errors when setting to original value', () => { | ||
|
@@ -2664,6 +2669,7 @@ describe('Unit | Utility | changeset', () => { | |
...dummyModel, | ||
...{ | ||
name: 'Jim Bob', | ||
email: 'jimmy@bob.com', | ||
password: true, | ||
passwordConfirmation: true, | ||
async: true, | ||
|
@@ -3234,4 +3240,45 @@ describe('Unit | Utility | changeset', () => { | |
.then(() => c.set('org.usa.ny', 'should not fail')) | ||
.finally(done()); | ||
}); | ||
|
||
async function delay(duration: number) { | ||
return new Promise(function(resolve: Function) { | ||
setTimeout(resolve, duration); | ||
}); | ||
} | ||
|
||
it('it works with out of order async validations', async () => { | ||
let latestDelayedAsyncResolver: Function = () => {}; | ||
|
||
dummyValidations.delayedAsync = () => { | ||
return new Promise(resolve => { | ||
latestDelayedAsyncResolver = resolve; | ||
}); | ||
}; | ||
|
||
const dummyChangeset = Changeset(dummyModel, lookupValidator(dummyValidations)); | ||
|
||
dummyChangeset.set('delayedAsync', 'first'); | ||
let firstResolver = latestDelayedAsyncResolver; | ||
dummyChangeset.set('delayedAsync', 'second'); | ||
let secondResolver = latestDelayedAsyncResolver; | ||
|
||
// second one resolves first with false | ||
secondResolver(false); | ||
// then the first resolves first with true | ||
firstResolver(true); | ||
|
||
// allow promises to run | ||
await delay(1); | ||
|
||
// clean up before running expectations | ||
delete dummyValidations.delayedAsync; | ||
|
||
// current value state should be "second" | ||
// current error state should be invalid | ||
const expectedChanges = [{ key: 'delayedAsync', value: 'second' }]; | ||
const expectedError = { delayedAsync: { validation: false, value: 'second' } }; | ||
expect(dummyChangeset.changes).toEqual(expectedChanges); | ||
expect(dummyChangeset.error).toEqual(expectedError); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BryanCrotaz fyi |
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the idea here is to finish all existing validations before starting the next one in line. This won't block user interaction since the
key
already has been set on the changeset. This is just an async path that will update the changset *sometime later.