Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check if max commits is greater than quantity in product creati… #920

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/components/product/utils/validationSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
OPTIONS_LENGTH,
OPTIONS_PERIOD,
OPTIONS_UNIT,
ProductTypeValues,
TOKEN_CRITERIA,
TOKEN_TYPES,
TokenTypes
Expand Down Expand Up @@ -355,7 +356,42 @@ export const getTokenGatingValidationSchema = ({
}),
maxCommits: Yup.string()
.required(validationMessage.required)
.matches(/^\+?[1-9]\d*$/, "Value must greater than 0"),
.matches(/^\+?[1-9]\d*$/, "Value must greater than 0")
.test("notGreaterThan_initialQuantity", function (value, context) {
if (value && Number.isInteger(Number.parseInt(value))) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const formValues = context.from[1].value;
const isOneVariant =
formValues.productType.productVariant ===
ProductTypeValues.oneItemType;
const variantsQuantity = isOneVariant
? 0
: (
formValues.productVariants
.variants as Yup.InferType<ProductVariantsValidationSchema>["productVariants"]["variants"]
).reduce((acum, current) => {
acum = acum + current.quantity;
return acum;
}, 0);
const quantity = isOneVariant
? formValues.coreTermsOfSale.quantity
: variantsQuantity;

const asNumber = Number.parseInt(value);
const isGreater = asNumber > quantity;
if (isGreater) {
throw this.createError({
path: this.path,
message: isOneVariant
? `This is greater than quantity (${formValues.coreTermsOfSale.quantity})`
: `This is greater than adding up the quantity of all variants (${variantsQuantity})`
});
}
return true;
}
return false;
}),
tokenType: Yup.object({
value: Yup.string(),
label: Yup.string()
Expand Down