-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run refiners as long as validators passed (#1068)
* Run refiners as long as validators passed Previously refiners ran only when there were no past failures during validation of the value itself or its subvalues. This behavior was causing inferior UX when validating forms with additional validations defined as refiners. If there was a refinement error in some subfield, the refiners defined on the parent of that subfield were not executed. Only after fixing the error in the subfield did the user receive information about the failure on the parent field. This commit changes the condition under which refinements are run. Refinements are executed if there were no previous failures or those failures came exclusively from other refiners. The change helps in the form validation scenario because as long as the form structure is valid (which is checked using `validator`s), failures from all refiners are collected. Refiners still run only if the general structure of the value is correct. They will get passed a structurally-valid value. The only difference now is that those values were may not have been checked via a refiner. If some check is so important that other refiners should not run if that check fails, that check should be defined as a `validator`, not a `refiner`. Failing that check will not run other refiners. Fixes #979 * refactor refinement code Co-authored-by: Ian Storm Taylor <[email protected]>
- Loading branch information
1 parent
f2697ea
commit d1508a9
Showing
3 changed files
with
92 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { string, refine, object } from '../../..' | ||
|
||
const PasswordValidator = refine(string(), 'MinimumLength', (pw) => | ||
pw.length >= 8 ? true : 'required minimum length of 8' | ||
) | ||
const changePasswordStruct = object({ | ||
newPassword: PasswordValidator, | ||
confirmPassword: string(), | ||
}) | ||
|
||
export const Struct = refine( | ||
changePasswordStruct, | ||
'PasswordsDoNotMatch', | ||
(values) => { | ||
return values.newPassword === values.confirmPassword | ||
? true | ||
: 'Passwords do not match' | ||
} | ||
) | ||
|
||
export const data = { | ||
newPassword: '1234567', | ||
confirmPassword: '123456789', | ||
} | ||
|
||
export const failures = [ | ||
{ | ||
value: data.newPassword, | ||
type: 'string', | ||
refinement: 'MinimumLength', | ||
path: ['newPassword'], | ||
branch: [data, data.newPassword], | ||
}, | ||
{ | ||
value: data, | ||
type: 'object', | ||
refinement: 'PasswordsDoNotMatch', | ||
path: [], | ||
branch: [data], | ||
}, | ||
] |