diff --git a/packages/js/product-editor/changelog/add-new-data-validation b/packages/js/product-editor/changelog/add-new-data-validation new file mode 100644 index 00000000000..249aba96bcd --- /dev/null +++ b/packages/js/product-editor/changelog/add-new-data-validation @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Allow providing recently updated data for the validate function in the useValidation hook diff --git a/packages/js/product-editor/src/blocks/product-fields/schedule-sale/edit.tsx b/packages/js/product-editor/src/blocks/product-fields/schedule-sale/edit.tsx index 7a05b5b7b7d..92880696252 100644 --- a/packages/js/product-editor/src/blocks/product-fields/schedule-sale/edit.tsx +++ b/packages/js/product-editor/src/blocks/product-fields/schedule-sale/edit.tsx @@ -167,7 +167,7 @@ export function Edit( { dateOnSaleFromGmtValidationError && 'has-error' } help={ dateOnSaleFromGmtValidationError as string } - onBlur={ validateDateOnSaleFromGmt } + onBlur={ () => validateDateOnSaleFromGmt() } /> @@ -190,7 +190,7 @@ export function Edit( { .toISOString() ) } - onBlur={ validateDateOnSaleToGmt } + onBlur={ () => validateDateOnSaleToGmt() } className={ dateOnSaleToGmtValidationError && 'has-error' } diff --git a/packages/js/product-editor/src/contexts/validation-context/types.ts b/packages/js/product-editor/src/contexts/validation-context/types.ts index 76bce6243d0..48b45f8674a 100644 --- a/packages/js/product-editor/src/contexts/validation-context/types.ts +++ b/packages/js/product-editor/src/contexts/validation-context/types.ts @@ -2,7 +2,7 @@ export type ValidatorResponse = Promise< ValidationError >; export type Validator< T > = ( initialValue?: T, - newData?: Partial< T > + newData?: Record< string, unknown > ) => ValidatorResponse; export type ValidationContextProps< T > = { @@ -12,7 +12,10 @@ export type ValidationContextProps< T > = { validator: Validator< T > ): React.Ref< HTMLElement >; unRegisterValidator( validatorId: string ): void; - validateField( name: string ): ValidatorResponse; + validateField( + name: string, + newData?: Record< string, unknown > + ): ValidatorResponse; validateAll( newData?: Partial< T > ): Promise< ValidationErrors >; }; diff --git a/packages/js/product-editor/src/contexts/validation-context/use-validation.ts b/packages/js/product-editor/src/contexts/validation-context/use-validation.ts index 20bfbdb7cbe..6621d9e8672 100644 --- a/packages/js/product-editor/src/contexts/validation-context/use-validation.ts +++ b/packages/js/product-editor/src/contexts/validation-context/use-validation.ts @@ -33,11 +33,13 @@ export function useValidation< T >( ref, error: context.errors[ validatorId ], isValidating, - async validate() { + async validate( newData?: Record< string, unknown > ) { setIsValidating( true ); - return context.validateField( validatorId ).finally( () => { - setIsValidating( false ); - } ); + return context + .validateField( validatorId, newData ) + .finally( () => { + setIsValidating( false ); + } ); }, }; }