Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 21d7d66

Browse files
authored
Add clearValidationErrors action to validation data store (#7601)
* Add clearValidationErrors to validation data store actions * Add reducer case for CLEAR_VALIDATION_ERRORS * Add tests for CLEAR_VALIDATION_ERRORS * Add documentation for clearValidationErrors * Deprecate clearAllValidationErrors in actions.ts * Remove CLEAR_VALIDATION_ERRORS action and reducer case * Update reducer test for clearAllValidationErrors * Make error message in test describe the test better * Update reducer to handle CLEAR_VALIDATION_ERRORS with no error passed * Update documentation for validation data store * Remove unnecessary linebreaks in documentation
1 parent 20294fe commit 21d7d66

File tree

5 files changed

+92
-31
lines changed

5 files changed

+92
-31
lines changed

assets/js/data/validation/action-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export const ACTION_TYPES = {
22
SET_VALIDATION_ERRORS: 'SET_VALIDATION_ERRORS',
3-
CLEAR_ALL_VALIDATION_ERRORS: 'CLEAR_ALL_VALIDATION_ERRORS',
43
CLEAR_VALIDATION_ERROR: 'CLEAR_VALIDATION_ERROR',
4+
CLEAR_VALIDATION_ERRORS: 'CLEAR_VALIDATION_ERRORS',
55
HIDE_VALIDATION_ERROR: 'HIDE_VALIDATION_ERROR',
66
SHOW_VALIDATION_ERROR: 'SHOW_VALIDATION_ERROR',
77
SHOW_ALL_VALIDATION_ERRORS: 'SHOW_ALL_VALIDATION_ERRORS',

assets/js/data/validation/actions.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import deprecated from '@wordpress/deprecated';
5+
16
/**
27
* Internal dependencies
38
*/
@@ -12,10 +17,29 @@ export const setValidationErrors = (
1217
errors,
1318
} );
1419

15-
export const clearAllValidationErrors = () => ( {
16-
type: types.CLEAR_ALL_VALIDATION_ERRORS,
20+
/**
21+
* Clears validation errors for the given ids.
22+
*
23+
* @param errors Array of error ids to clear.
24+
*/
25+
export const clearValidationErrors = ( errors?: string[] | undefined ) => ( {
26+
type: types.CLEAR_VALIDATION_ERRORS,
27+
errors,
1728
} );
1829

30+
export const clearAllValidationErrors = () => {
31+
deprecated( 'clearAllValidationErrors', {
32+
version: '9.0.0',
33+
alternative: 'clearValidationErrors',
34+
plugin: 'WooCommerce Blocks',
35+
link: 'https://github.com/woocommerce/woocommerce-blocks/pull/7601',
36+
hint: 'Calling `clearValidationErrors` with no arguments will clear all validation errors.',
37+
} );
38+
39+
// Return clearValidationErrors which will clear all errors by defaults if no error ids are passed.
40+
return clearValidationErrors();
41+
};
42+
1943
export const clearValidationError = ( error: string ) => ( {
2044
type: types.CLEAR_VALIDATION_ERROR,
2145
error,
@@ -39,6 +63,7 @@ export type ValidationAction = ReturnOrGeneratorYieldUnion<
3963
| typeof setValidationErrors
4064
| typeof clearAllValidationErrors
4165
| typeof clearValidationError
66+
| typeof clearValidationErrors
4267
| typeof hideValidationError
4368
| typeof showValidationError
4469
| typeof showAllValidationErrors

assets/js/data/validation/reducers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ const reducer: Reducer< Record< string, FieldValidationStatus > > = (
3333
return state;
3434
}
3535
return { ...state, ...action.errors };
36-
case types.CLEAR_ALL_VALIDATION_ERRORS:
37-
return {};
3836

3937
case types.CLEAR_VALIDATION_ERROR:
4038
if (
@@ -45,6 +43,20 @@ const reducer: Reducer< Record< string, FieldValidationStatus > > = (
4543
}
4644
delete newState[ action.error ];
4745
return newState;
46+
case types.CLEAR_VALIDATION_ERRORS:
47+
const { errors } = action;
48+
if ( typeof errors === 'undefined' ) {
49+
return {};
50+
}
51+
if ( ! Array.isArray( errors ) ) {
52+
return newState;
53+
}
54+
errors.forEach( ( error ) => {
55+
if ( newState.hasOwnProperty( error ) ) {
56+
delete newState[ error ];
57+
}
58+
} );
59+
return newState;
4860
case types.HIDE_VALIDATION_ERROR:
4961
if (
5062
! isString( action.error ) ||

assets/js/data/validation/test/reducers.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ describe( 'Validation reducer', () => {
129129
},
130130
};
131131
const clearAllErrors: ValidationAction = {
132-
type: types.CLEAR_ALL_VALIDATION_ERRORS,
132+
type: types.CLEAR_VALIDATION_ERRORS,
133+
errors: undefined,
133134
};
134135
const nextState = reducer( state, clearAllErrors );
135136
expect( nextState ).toEqual( {} );
@@ -155,6 +156,26 @@ describe( 'Validation reducer', () => {
155156
expect( nextState ).toHaveProperty( 'testError' );
156157
} );
157158

159+
it( 'Clears multiple validation errors', () => {
160+
const state: Record< string, FieldValidationStatus > = {
161+
existingError: {
162+
message: 'This is an existing error message',
163+
hidden: false,
164+
},
165+
testError: {
166+
message: 'This is error should also be removed',
167+
hidden: false,
168+
},
169+
};
170+
const clearError: ValidationAction = {
171+
type: types.CLEAR_VALIDATION_ERRORS,
172+
errors: [ 'existingError', 'testError' ],
173+
};
174+
const nextState = reducer( state, clearError );
175+
expect( nextState ).not.toHaveProperty( 'existingError' );
176+
expect( nextState ).not.toHaveProperty( 'testError' );
177+
} );
178+
158179
it( 'Hides a single validation error', () => {
159180
const state: Record< string, FieldValidationStatus > = {
160181
existingError: {

docs/third-party-developers/extensibility/data-store/validation.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- [hasValidationErrors](#hasvalidationerrors)
1010
- [Actions](#actions)
1111
- [clearValidationError](#clearvalidationerror)
12-
- [clearAllValidationErrors](#clearallvalidationerrors)
12+
- [clearValidationErrors](#clearvalidationerrors)
1313
- [setValidationErrors](#setvalidationerrors)
1414
- [hideValidationError](#hidevalidationerror)
1515
- [showValidationError](#showvalidationerror)
@@ -19,9 +19,7 @@
1919

2020
The validation data store provides a way to show errors for fields in the Cart or Checkout blocks.
2121

22-
The data in the store should be a single object, the keys of which are the _error IDs_ and values are the data
23-
associated with that error message. The values in the object should contain _message_ and _hidden_. The _message_
24-
is the error message to display and _hidden_ is a boolean indicating whether the error should be shown or not.
22+
The data in the store should be a single object, the keys of which are the _error IDs_ and values are the data associated with that error message. The values in the object should contain _message_ and _hidden_. The _message_ is the error message to display and _hidden_ is a boolean indicating whether the error should be shown or not.
2523

2624
An example of how the data should be structured:
2725

@@ -38,9 +36,7 @@ An example of how the data should be structured:
3836
}
3937
```
4038

41-
When the checkout process begins, it will check if this data store has any entries, and if so, it will stop the checkout
42-
process from proceeding. It will also show any errors that are hidden. Setting an error to hidden will not clear it from
43-
the data store!
39+
When the checkout process begins, it will check if this data store has any entries, and if so, it will stop the checkout process from proceeding. It will also show any errors that are hidden. Setting an error to hidden will not clear it from the data store!
4440

4541
## Selectors
4642

@@ -66,9 +62,7 @@ const billingFirstNameError = store.getValidationError( 'billing-first-name' );
6662

6763
### getValidationErrorId
6864

69-
Gets a validation error ID for use in HTML which can be used as a CSS selector, or to reference an error message.
70-
This will return the error ID prefixed with `validate-error-`, unless the validation error has `hidden` set to true, or
71-
the validation error does not exist in the store.
65+
Gets a validation error ID for use in HTML which can be used as a CSS selector, or to reference an error message. This will return the error ID prefixed with `validate-error-`, unless the validation error has `hidden` set to true, or the validation error does not exist in the store.
7266

7367
#### _Parameters_
7468

@@ -103,19 +97,37 @@ const store = dispatch( 'wc/store/validation' );
10397
store.clearValidationError( 'billing-first-name' );
10498
```
10599

106-
### clearAllValidationErrors
100+
### clearValidationErrors
107101

108-
Clears all validation errors.
102+
Clears multiple validation errors at once. If no error IDs are passed, all validation errors will be cleared.
103+
104+
#### _Parameters_
105+
106+
- _errors_ `string[] | undefined` - The error IDs to clear validation errors for. This can be undefined, and if it is, all validation errors will be cleared.
107+
108+
#### Example
109+
110+
1. This will clear only the validation errors passed in the array.
111+
112+
```js
113+
const store = dispatch( 'wc/store/validation' );
114+
store.clearValidationErrors( [ 'billing-first-name', 'billing-last-name', 'terms-and-conditions' ] );
115+
```
116+
117+
2. This will clear all validation errors.
118+
119+
```js
120+
const store = dispatch( 'wc/store/validation' );
121+
store.clearValidationErrors();
122+
```
109123

110124
### setValidationErrors
111125

112126
#### _Parameters_
113127

114-
- _errors_ `object`: An object containing new validation errors, the keys of the object are the validation error IDs,
115-
and the values should be objects containing _message_ (`string`) and _hidden_ `boolean`.
128+
- _errors_ `object`: An object containing new validation errors, the keys of the object are the validation error IDs, and the values should be objects containing _message_ (`string`) and _hidden_ `boolean`.
116129

117-
Sets the validation errors. The entries in _errors_ will be _added_ to the list of validation errors. Any entries that
118-
already exist in the list will be _updated_ with the new values.
130+
Sets the validation errors. The entries in _errors_ will be _added_ to the list of validation errors. Any entries that already exist in the list will be _updated_ with the new values.
119131

120132
#### Example
121133

@@ -135,15 +147,6 @@ setValidationErrors( {
135147
} );
136148
```
137149

138-
#### Example
139-
140-
```js
141-
const { dispatch } = wp.data;
142-
const { clearAllValidationErrors } = dispatch( 'wc/store/validation' );
143-
144-
clearAllValidationErrors();
145-
```
146-
147150
### hideValidationError
148151

149152
Hides a validation error by setting the `hidden` property to `true`. This will _not_ clear it from the data store!

0 commit comments

Comments
 (0)