Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions packages/stack-shared/src/config/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from "vitest";
import { branchPaymentsSchema } from "./schema";

describe("branchPaymentsSchema", () => {
it("accepts partial payments config without products", async () => {
await expect(branchPaymentsSchema.validate({
blockNewPurchases: true,
})).resolves.toMatchObject({
blockNewPurchases: true,
});
});

it("accepts product lines without products", async () => {
await expect(branchPaymentsSchema.validate({
productLines: {
pro: {
displayName: "Pro",
customerType: "user",
},
},
})).resolves.toMatchObject({
productLines: {
pro: {
displayName: "Pro",
customerType: "user",
},
},
});
});

it("rejects a product that references a missing product line", async () => {
await expect(branchPaymentsSchema.validate({
products: {
pro: {
customerType: "user",
productLineId: "missing-line",
},
},
})).rejects.toThrow('Product "pro" specifies product line ID "missing-line", but that product line does not exist');
});

it("rejects a product whose customer type differs from its product line", async () => {
await expect(branchPaymentsSchema.validate({
productLines: {
teamLine: {
customerType: "team",
},
},
products: {
pro: {
customerType: "user",
productLineId: "teamLine",
},
},
})).rejects.toThrow('Product "pro" has customer type "user" but its product line "teamLine" has customer type "team"');
Comment thread
mantrakp04 marked this conversation as resolved.
Outdated
});
});
10 changes: 7 additions & 3 deletions packages/stack-shared/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ export const branchPaymentsSchema = yupObject({
'Product customer type must match its product line customer type',
function(this: yup.TestContext<yup.AnyObject>, value) {
if (!value) return true;
for (const [productId, product] of Object.entries(value.products)) {
if (!product.productLineId) continue;
const productLine = getOrUndefined(value.productLines, product.productLineId);
const products = Reflect.get(value, "products");
if (!isObjectLike(products)) return true;

const productLines = Reflect.get(value, "productLines");
Comment thread
mantrakp04 marked this conversation as resolved.
Outdated
for (const [productId, product] of Object.entries(products)) {
if (product.productLineId == null) continue;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Discussion: this is a stricter check than what we had before. Is there a reason you switched it? This change would mean empty product line ids are not skipped.

const productLine = isObjectLike(productLines) ? getOrUndefined(productLines, product.productLineId) : undefined;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment thread
mantrakp04 marked this conversation as resolved.
Outdated
if (productLine === undefined) {
return this.createError({
message: `Product "${productId}" specifies product line ID "${product.productLineId}", but that product line does not exist`,
Expand Down
Loading