Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
79 changes: 79 additions & 0 deletions packages/stack-shared/src/config/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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 null product entries without throwing a raw TypeError", async () => {
await expect(branchPaymentsSchema.validate({
products: {
pro: null,
},
})).rejects.toMatchObject({ name: "ValidationError" });
});

it("rejects null product line entries without throwing a raw TypeError", async () => {
await expect(branchPaymentsSchema.validate({
productLines: {
teamLine: null,
},
products: {
pro: {
customerType: "user",
productLineId: "teamLine",
},
},
})).rejects.toMatchObject({ name: "ValidationError" });
});

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"');
});
});
12 changes: 9 additions & 3 deletions packages/stack-shared/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,21 @@ 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 = value.products;
if (!isObjectLike(products)) return true;

const productLines = value.productLines;
for (const [productId, product] of Object.entries(products)) {
if (!isObjectLike(product)) continue;
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;
if (productLine === undefined) {
return this.createError({
message: `Product "${productId}" specifies product line ID "${product.productLineId}", but that product line does not exist`,
path: `${this.path}.products.${productId}.productLineId`,
});
}
if (!isObjectLike(productLine)) continue;
if (product.customerType !== productLine.customerType) {
return this.createError({
message: `Product "${productId}" has customer type "${product.customerType}" but its product line "${product.productLineId}" has customer type "${productLine.customerType}"`,
Expand Down
Loading