hi there
i’ll start and say im not a web developer so i dont know how to code
i wanted to do 3 things to my website:
- set a minimum quantity to a specific item
- set maximum quantity to all of the items
- set a minimum quantity to a specific category
all of the above has to grey out the proceed to checkout if requirement not met
using ecom-validations in chat gpt i managed to do the first 2 and its working great.
the only one i cant resolve is the min quantity to a category
can please someone help me with the correct code?
the code im using now
import * as ecomValidations from ‘interfaces-ecommerce-v1-validations-provider’;
// Assuming that the schema or validation rule involves a “description” property with minLength and maxLength rules.
const schema = {
properties: {
description: {
type: ‘string’, // Explicitly set the type to ‘string’
minLength: 1, // Set the minimum length for the description property
maxLength: 255, // Set the maximum length for the description property
},
},
};
export const getValidationViolations = async (options, context) => {
let violations = ;
const source = options.sourceInfo.source;
const severity = source == ecomValidations.Source.CART ? ecomValidations.Severity.ERROR : ecomValidations.Severity.ERROR;
const desiredCategory = ‘6ed1fc23-7319-3fe7-ce6d-c10fc0294107’;
const itemDescription = “must order more than 2 pcs”;
// Check all line items
options.validationInfo.lineItems.forEach((lineItem) => {
const catalogCategory = lineItem.catalogReference?.category?.categoryId;
if (catalogCategory === desiredCategory) {
const quantity = lineItem.quantity;
if (quantity > 0 && quantity < 2) {
violations.push({
severity,
target: { lineItem: { name: ecomValidations.NameInLineItem.LINE_ITEM_DEFAULT, _id: lineItem._id } },
description: itemDescription
});
}
}
});
const cartQuantityDescription = “You must purchase a minimum of 2 items.”;
const cartQuantity = options.validationInfo.lineItems.reduce((partialSum, lineItem) => partialSum + lineItem.quantity, 0);
if (cartQuantity < 1) {
violations.push({
severity,
target: { other: { name: ecomValidations.NameInOther.OTHER_DEFAULT } },
description: cartQuantityDescription
});
}
return { violations };
};
i get this error:
“root”:{
“insertId”:
“…7Mp5BWfu47tQwDW0pt0lUl”
“timestamp”:
“2023-12-15T09:19:46.767Z”
“labels”:{
“siteUrl”:
“n/a”
“revision”:
“1844”
“namespace”:
“Velo”
“tenantId”:
“7b787ec8-275d-4966-81c7-e406e1512859”
“viewMode”:
“Site”
}
“operation”:{
“id”:
“1702631984.42610877729076715435”
“producer”:
“backend”
}
“jsonPayload”:{
“message”:
“[“strict mode: missing type "string" for keyword "minLength" at "#/properties/description" (strictTypes)”]”
}
“severity”:
“WARNING”
“receiveTimestamp”:
“2023-12-15T09:19:47.324Z”
}