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

Remove the mustBeString validation utility and its usage #4294

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import ProductName from '@woocommerce/base-components/product-name';
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
import {
__experimentalApplyCheckoutFilter,
mustBeString,
mustContain,
Label,
} from '@woocommerce/blocks-checkout';
Expand Down Expand Up @@ -48,7 +47,7 @@ const OrderSummaryItem = ( { cartItem } ) => {
const { receiveCart, ...cart } = useStoreCart();

const productPriceValidation = useCallback(
( value ) => mustBeString( value ) && mustContain( value, '<price/>' ),
( value ) => mustContain( value, '<price/>' ),
[]
);

Expand All @@ -68,7 +67,6 @@ const OrderSummaryItem = ( { cartItem } ) => {
defaultValue: initialName,
extensions,
arg,
validation: mustBeString,
} );

const regularPriceSingle = Dinero( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { RemovableChip } from '@woocommerce/base-components/chip';
import PropTypes from 'prop-types';
import {
__experimentalApplyCheckoutFilter,
mustBeString,
TotalsItem,
} from '@woocommerce/blocks-checkout';
import { getSetting } from '@woocommerce/settings';
Expand Down Expand Up @@ -60,7 +59,6 @@ const TotalsDiscount = ( {
{ cartCoupons.map( ( cartCoupon ) => {
const filteredCouponCode = __experimentalApplyCheckoutFilter(
{
validation: mustBeString,
arg: {
context: 'summary',
coupon: cartCoupon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-mone
import PropTypes from 'prop-types';
import {
__experimentalApplyCheckoutFilter,
mustBeString,
TotalsItem,
} from '@woocommerce/blocks-checkout';
import { useStoreCart } from '@woocommerce/base-context/hooks';
Expand All @@ -34,8 +33,6 @@ const TotalsFooterItem = ( { currency, values } ) => {
defaultValue: __( 'Total', 'woo-gutenberg-products-block' ),
extensions: cart.extensions,
arg: { cart },
// Only accept strings.
validation: mustBeString,
} );

const parsedTaxValue = parseInt( totalTax, 10 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
} from '@woocommerce/price-format';
import {
__experimentalApplyCheckoutFilter,
mustBeString,
mustContain,
} from '@woocommerce/blocks-checkout';
import Dinero from 'dinero.js';
Expand Down Expand Up @@ -112,7 +111,7 @@ const CartLineItemRow = ( {
const { dispatchStoreEvent } = useStoreEvents();

const productPriceValidation = useCallback(
( value ) => mustBeString( value ) && mustContain( value, '<price/>' ),
( value ) => mustContain( value, '<price/>' ),
[]
);

Expand All @@ -134,7 +133,6 @@ const CartLineItemRow = ( {
defaultValue: initialName,
extensions,
arg,
validation: mustBeString,
} );

const regularAmountSingle = Dinero( {
Expand Down
2 changes: 1 addition & 1 deletion packages/checkout/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const __experimentalApplyCheckoutFilter = < T >( {
/** Object containing arguments for the filter function. */
arg: CheckoutFilterArguments;
/** Function that needs to return true when the filtered value is passed in order for the filter to be applied. */
validation: ( value: unknown ) => true | Error;
validation?: ( value: T ) => true | Error;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value type isn't uknown, we know for sure that it's the same as defaultValue and return value T

} ): T => {
return useMemo( () => {
const filters = getCheckoutFilters( filterName );
Expand Down
19 changes: 0 additions & 19 deletions packages/checkout/utils/validation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@
*/
import { __, sprintf } from '@wordpress/i18n';

/**
* Checks if value passed is a string, throws an error if not.
*/
export const mustBeString = ( value: unknown ): true | Error => {
if ( typeof value !== 'string' ) {
throw Error(
sprintf(
/* translators: %s is type of value passed */
__(
'Returned value must be a string, you passed "%s"',
'woo-gutenberg-products-block'
),
typeof value
)
);
}
return true;
};

/**
* Checks if value passed contain passed label.
*/
Expand Down