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-11-14 21:03:13 +00:00
|
|
|
import moment from 'moment';
|
2022-08-30 13:26:24 +00:00
|
|
|
|
2022-10-12 21:30:02 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { validate as validateInventory } from './sections/product-inventory-section';
|
|
|
|
|
2022-11-14 21:03:13 +00:00
|
|
|
function validateScheduledSaleFields(
|
|
|
|
values: Partial< Product< ProductStatus, ProductType > >
|
|
|
|
): FormErrors< typeof values > {
|
|
|
|
const errors: FormErrors< typeof values > = {};
|
|
|
|
|
|
|
|
const dateOnSaleFrom = moment(
|
|
|
|
values.date_on_sale_from_gmt,
|
|
|
|
moment.ISO_8601,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
const dateOnSaleTo = moment(
|
|
|
|
values.date_on_sale_to_gmt,
|
|
|
|
moment.ISO_8601,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( values.date_on_sale_from_gmt && ! dateOnSaleFrom.isValid() ) {
|
|
|
|
errors.date_on_sale_from_gmt = __(
|
|
|
|
'Please enter a valid date.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( values.date_on_sale_to_gmt && ! dateOnSaleTo.isValid() ) {
|
|
|
|
errors.date_on_sale_to_gmt = __(
|
|
|
|
'Please enter a valid date.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( dateOnSaleFrom.isAfter( dateOnSaleTo ) ) {
|
|
|
|
errors.date_on_sale_from_gmt = __(
|
|
|
|
'The start date of the sale must be before the end date.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( dateOnSaleTo.isBefore( dateOnSaleFrom ) ) {
|
|
|
|
errors.date_on_sale_to_gmt = __(
|
|
|
|
'The end date of the sale must be after the start date.',
|
|
|
|
'woocommerce'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2022-08-30 13:26:24 +00:00
|
|
|
export const validate = (
|
|
|
|
values: Partial< Product< ProductStatus, ProductType > >
|
|
|
|
) => {
|
2022-10-12 21:30:02 +00:00
|
|
|
let errors: FormErrors< typeof values > = {};
|
|
|
|
errors = validateInventory( values, errors );
|
|
|
|
|
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-11-14 21:03:13 +00:00
|
|
|
errors = {
|
|
|
|
...errors,
|
|
|
|
...validateScheduledSaleFields( values ),
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|