-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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 #6140 from marmelab/fix-validation-documentation
Fix validation documentation & Submission Errors Cannot Have Translatable Error Objects
- Loading branch information
Showing
9 changed files
with
362 additions
and
30 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
174 changes: 174 additions & 0 deletions
174
packages/ra-core/src/form/submitErrorsMutators.spec.tsx
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,174 @@ | ||
import { getIn, setIn } from 'final-form'; | ||
|
||
import { resetSubmitErrors } from './submitErrorsMutators'; | ||
|
||
const makeFormState = ({ | ||
submitErrors, | ||
submitError, | ||
}: { | ||
submitErrors?: any; | ||
submitError?: any; | ||
}) => ({ | ||
formState: { | ||
submitError, | ||
submitErrors, | ||
}, | ||
}); | ||
|
||
describe('submitErrorsMutators', () => { | ||
test('should ignore when no changes occur', () => { | ||
const prev = { | ||
value: 'hello', | ||
}; | ||
|
||
const current = { | ||
value: 'hello', | ||
}; | ||
|
||
const state = makeFormState({ | ||
submitErrors: { | ||
value: 'error', | ||
}, | ||
}); | ||
|
||
resetSubmitErrors([{ prev, current }], state, { getIn, setIn }); | ||
|
||
expect(state.formState.submitErrors).toEqual({ | ||
value: 'error', | ||
}); | ||
}); | ||
|
||
test('should reset errors for basic types', () => { | ||
const prev = { | ||
bool: true, | ||
null: null, | ||
number: 1, | ||
string: 'one', | ||
}; | ||
|
||
const current = { | ||
bool: false, | ||
null: undefined, | ||
number: 2, | ||
string: 'two', | ||
}; | ||
|
||
const state = makeFormState({ | ||
submitErrors: { | ||
bool: 'error', | ||
null: 'error', | ||
number: 'error', | ||
string: 'error', | ||
}, | ||
}); | ||
|
||
resetSubmitErrors([{ prev, current }], state, { getIn, setIn }); | ||
|
||
expect(state.formState.submitErrors).toEqual({}); | ||
}); | ||
|
||
test('should reset errors for nested objects', () => { | ||
const prev = { | ||
nested: { | ||
deep: { | ||
field: 'one', | ||
other: { | ||
field: 'two', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const current = { | ||
nested: { | ||
deep: { | ||
field: 'two', | ||
}, | ||
}, | ||
}; | ||
|
||
const state = makeFormState({ | ||
submitErrors: { | ||
nested: { | ||
deep: { | ||
field: 'error', | ||
other: 'error', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
resetSubmitErrors([{ prev, current }], state, { getIn, setIn }); | ||
|
||
expect(state.formState.submitErrors).toEqual({}); | ||
}); | ||
|
||
test('should reset errors for arrays', () => { | ||
const prev = { | ||
array: [ | ||
{ | ||
some: [1, 2], | ||
}, | ||
{ | ||
value: 'one', | ||
}, | ||
1, | ||
], | ||
}; | ||
|
||
const current = { | ||
array: [ | ||
{ | ||
some: [2], | ||
}, | ||
{ | ||
value: 'one', | ||
}, | ||
2, | ||
], | ||
}; | ||
|
||
const state = makeFormState({ | ||
submitErrors: { | ||
array: [ | ||
{ | ||
some: ['error', 'error'], | ||
}, | ||
{ | ||
value: 'error', | ||
}, | ||
'error', | ||
], | ||
}, | ||
}); | ||
|
||
resetSubmitErrors([{ prev, current }], state, { getIn, setIn }); | ||
|
||
expect(state.formState.submitErrors).toEqual({ | ||
array: [undefined, { value: 'error' }], | ||
}); | ||
}); | ||
|
||
test('should reset errors for validation error objects', () => { | ||
const prev = { | ||
field: 'aaaa', | ||
}; | ||
|
||
const current = { | ||
field: 'aaaaa', | ||
}; | ||
|
||
const state = makeFormState({ | ||
submitErrors: { | ||
field: { | ||
message: 'ra.validation.min_length', | ||
args: { min: 5 }, | ||
}, | ||
}, | ||
}); | ||
|
||
resetSubmitErrors([{ prev, current }], state, { getIn, setIn }); | ||
|
||
expect(state.formState.submitErrors).toEqual({}); | ||
}); | ||
}); |
Oops, something went wrong.