This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Add client side postcode validation #8503
Merged
nielslange
merged 28 commits into
trunk
from
add/7722-add-client-side-postcode-validation
Mar 14, 2023
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
591f4d9
Add client side postcode validation
nielslange 4c53bb8
Prevent server-side validation
nielslange 4dbfb7c
Adjust translation
nielslange 779a51c
Only validate postcode if country is available
nielslange fb437fd
Specify return type of isPostcode()
nielslange 8028cce
Convert function to static variable set
nielslange e1b4470
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 711bddd
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange f7c6cc1
Refactor <ValidatedTextInput> for postcode validation
nielslange 59be85f
Refactor customValidationHandler
nielslange da07670
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 08a1b63
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange a1b0146
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange c754683
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange d9d17fb
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange a6077cd
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange b6dca6f
Use customValidationHandler as intermediate function
nielslange c543789
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 6db40a8
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 0902665
Hyphenate file names
nielslange 36eb53b
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 7945760
Update packages/checkout/utils/validation/is-postcode.ts
nielslange 4b39785
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 370263f
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange c849572
Normalise postcode on input
nielslange de8ee24
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange 3cc951b
Merge branch 'trunk' into add/7722-add-client-side-postcode-validation
nielslange c29f8b8
Fix usage of out of date value from input field
mikejolley 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
packages/checkout/utils/validation/get-validity-message-for-input.ts
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,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Converts an input's validityState to a string to display on the frontend. | ||
* | ||
* This returns custom messages for invalid/required fields. Other error types use defaults from the browser (these | ||
* could be implemented in the future but are not currently used by the block checkout). | ||
*/ | ||
const getValidityMessageForInput = ( | ||
label: string, | ||
inputElement: HTMLInputElement | ||
): string => { | ||
const { valid, customError, valueMissing, badInput, typeMismatch } = | ||
inputElement.validity; | ||
|
||
// No errors, or custom error - return early. | ||
if ( valid || customError ) { | ||
return inputElement.validationMessage; | ||
} | ||
|
||
const invalidFieldMessage = sprintf( | ||
/* translators: %s field label */ | ||
__( 'Please enter a valid %s', 'woo-gutenberg-products-block' ), | ||
label.toLowerCase() | ||
); | ||
|
||
if ( valueMissing || badInput || typeMismatch ) { | ||
return invalidFieldMessage; | ||
} | ||
|
||
return inputElement.validationMessage || invalidFieldMessage; | ||
}; | ||
|
||
export default getValidityMessageForInput; |
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 |
---|---|---|
@@ -1,58 +1,3 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Ensures that a given value contains a string, or throws an error. | ||
*/ | ||
export const mustContain = ( | ||
value: string, | ||
requiredValue: string | ||
): true | never => { | ||
if ( ! value.includes( requiredValue ) ) { | ||
throw Error( | ||
sprintf( | ||
/* translators: %1$s value passed to filter, %2$s : value that must be included. */ | ||
__( | ||
'Returned value must include %1$s, you passed "%2$s"', | ||
'woo-gutenberg-products-block' | ||
), | ||
requiredValue, | ||
value | ||
) | ||
); | ||
} | ||
return true; | ||
}; | ||
|
||
/** | ||
* Converts an input's validityState to a string to display on the frontend. | ||
* | ||
* This returns custom messages for invalid/required fields. Other error types use defaults from the browser (these | ||
* could be implemented in the future but are not currently used by the block checkout). | ||
*/ | ||
export const getValidityMessageForInput = ( | ||
label: string, | ||
inputElement: HTMLInputElement | ||
): string => { | ||
const { valid, customError, valueMissing, badInput, typeMismatch } = | ||
inputElement.validity; | ||
|
||
// No errors, or custom error - return early. | ||
if ( valid || customError ) { | ||
return inputElement.validationMessage; | ||
} | ||
|
||
const invalidFieldMessage = sprintf( | ||
/* translators: %s field label */ | ||
__( 'Please enter a valid %s', 'woo-gutenberg-products-block' ), | ||
label.toLowerCase() | ||
); | ||
|
||
if ( valueMissing || badInput || typeMismatch ) { | ||
return invalidFieldMessage; | ||
} | ||
|
||
return inputElement.validationMessage || invalidFieldMessage; | ||
}; | ||
export { default as mustContain } from './must-contain'; | ||
export { default as getValidityMessageForInput } from './get-validity-message-for-input'; | ||
export { default as isPostcode } from './is-postcode'; |
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 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { POSTCODE_REGEXES } from 'postcode-validator/lib/cjs/postcode-regexes.js'; | ||
|
||
const CUSTOM_REGEXES = new Map< string, RegExp >( [ | ||
[ 'BA', /^([7-8]{1})([0-9]{4})$/ ], | ||
[ | ||
'GB', | ||
/^([A-Z]){1}([0-9]{1,2}|[A-Z][0-9][A-Z]|[A-Z][0-9]{2}|[A-Z][0-9]|[0-9][A-Z]){1}([ ])?([0-9][A-Z]{2}){1}|BFPO(?:\s)?([0-9]{1,4})$|BFPO(c\/o[0-9]{1,3})$/i, | ||
], | ||
[ 'IN', /^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$/ ], | ||
[ 'JP', /^([0-9]{3})([-]?)([0-9]{4})$/ ], | ||
[ 'LI', /^(94[8-9][0-9])$/ ], | ||
[ 'NL', /^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$/i ], | ||
[ 'SI', /^([1-9][0-9]{3})$/ ], | ||
] ); | ||
|
||
const DEFAULT_REGEXES = new Map< string, RegExp >( [ | ||
...POSTCODE_REGEXES, | ||
...CUSTOM_REGEXES, | ||
] ); | ||
|
||
export interface IsPostcodeProps { | ||
postcode: string; | ||
country: string; | ||
} | ||
|
||
const isPostcode = ( { postcode, country }: IsPostcodeProps ): boolean => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return DEFAULT_REGEXES.get( country )!.test( postcode ); | ||
}; | ||
|
||
export default isPostcode; |
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,26 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Ensures that a given value contains a string, or throws an error. | ||
*/ | ||
const mustContain = ( value: string, requiredValue: string ): true | never => { | ||
if ( ! value.includes( requiredValue ) ) { | ||
throw Error( | ||
sprintf( | ||
/* translators: %1$s value passed to filter, %2$s : value that must be included. */ | ||
__( | ||
'Returned value must include %1$s, you passed "%2$s"', | ||
'woo-gutenberg-products-block' | ||
), | ||
requiredValue, | ||
value | ||
) | ||
); | ||
} | ||
return true; | ||
}; | ||
|
||
export default mustContain; |
Oops, something went wrong.
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.
When the input field is
postcode
, then remove leading spaces and convert lowercase to uppercase characters on input. This part ensures that the client-side postcode validation of WooCommerce Blocks, is in sync with the server-side postcode validation of WooCommerce.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.
@mikejolley Based on our internal discussion, I've updated the PR. Would you mind reviewing it again?