2022-08-30 13:26:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-09-29 17:54:04 +00:00
|
|
|
import {
|
|
|
|
ProductStatus,
|
|
|
|
ProductType,
|
|
|
|
Product,
|
|
|
|
ProductDimensions,
|
|
|
|
} from '@woocommerce/data';
|
|
|
|
import type { FormErrors } from '@woocommerce/components';
|
2022-08-30 13:26:24 +00:00
|
|
|
|
|
|
|
export const validate = (
|
|
|
|
values: Partial< Product< ProductStatus, ProductType > >
|
|
|
|
) => {
|
2022-09-29 17:54:04 +00:00
|
|
|
const errors: FormErrors< typeof values > = {};
|
2022-08-30 13:26:24 +00:00
|
|
|
if ( ! values.name?.length ) {
|
|
|
|
errors.name = __( 'This field is required.', 'woocommerce' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.name && values.name.length > 120 ) {
|
|
|
|
errors.name = __(
|
|
|
|
'Please enter a product name shorter than 120 characters.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.regular_price && ! /^[0-9.,]+$/.test( values.regular_price ) ) {
|
|
|
|
errors.regular_price = __(
|
|
|
|
'Please enter a price with one monetary decimal point without thousand separators and currency symbols.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.sale_price && ! /^[0-9.,]+$/.test( values.sale_price ) ) {
|
|
|
|
errors.sale_price = __(
|
|
|
|
'Please enter a price with one monetary decimal point without thousand separators and currency symbols.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
2022-09-15 13:56:33 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
values.sale_price &&
|
|
|
|
( ! values.regular_price ||
|
|
|
|
parseFloat( values.sale_price ) >=
|
|
|
|
parseFloat( values?.regular_price ) )
|
|
|
|
) {
|
|
|
|
errors.sale_price = __(
|
|
|
|
'Sale price cannot be equal to or higher than list price.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-29 17:54:04 +00:00
|
|
|
if ( values.dimensions?.width && +values.dimensions.width <= 0 ) {
|
|
|
|
errors.dimensions = {
|
|
|
|
width: __( 'Width must be higher than zero.', 'woocommerce' ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.dimensions?.length && +values.dimensions.length <= 0 ) {
|
|
|
|
errors.dimensions = {
|
|
|
|
...( ( errors.dimensions as FormErrors< ProductDimensions > ) ??
|
|
|
|
{} ),
|
|
|
|
length: __( 'Length must be higher than zero.', 'woocommerce' ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.dimensions?.height && +values.dimensions.height <= 0 ) {
|
|
|
|
errors.dimensions = {
|
|
|
|
...( ( errors.dimensions as FormErrors< ProductDimensions > ) ??
|
|
|
|
{} ),
|
|
|
|
height: __( 'Height must be higher than zero.', 'woocommerce' ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.weight && +values.weight <= 0 ) {
|
|
|
|
errors.weight = __( 'Weight must be higher than zero.', 'woocommerce' );
|
|
|
|
}
|
|
|
|
|
2022-08-30 13:26:24 +00:00
|
|
|
return errors;
|
|
|
|
};
|