fix: enforce limit of 2 auction triggers at compile time#176
fix: enforce limit of 2 auction triggers at compile time#176
Conversation
jbergstroem
left a comment
There was a problem hiding this comment.
Left feedback. Also.. tests.
|
@jbergstroem changed to compile time validation, we'll need to version bump though. |
| @@ -0,0 +1,59 @@ | |||
| /** | |||
| * Compile-time validation tests for auction constraints | |||
| * This file should compile without errors if the type system is working correctly | |||
There was a problem hiding this comment.
how do we then test invalid combinations?
There was a problem hiding this comment.
// @ts-expect-error can be an exit
I've changed the interface to accept the wrong combination and it fails (it expects an error, there's no error, so the annotation fails the "test")
| auctions: [{ type: "banners", slots: 1, slotId: "slot123", category: { id: "cat123" } }], | ||
| }; | ||
|
|
||
| const _invalid3TriggersAuction: Auction = { |
There was a problem hiding this comment.
Wouldn't this allow any error, not just the error we're looking for? Are we sure this is the right path?
There was a problem hiding this comment.
added which specific error we're looking for 👍
|
Can we simplify your types? The permutations looks complicated. How about something like: type CategoryType =
| AuctionSingleCategory
| AuctionMultipleCategories
| AuctionDisjunctiveCategories;
type AuctionConstraints =
| { category: CategoryType; products: AuctionProduct; searchQuery?: never }
| { category: CategoryType; products?: never; searchQuery: string }
| { category?: never; products: AuctionProduct; searchQuery: string }
| { category: CategoryType; products?: never; searchQuery?: never }
| { category?: never; products: AuctionProduct; searchQuery?: never }
| { category?: never; products?: never; searchQuery: string }
| { category?: never; products?: never; searchQuery?: never };
type AuctionBase = AuctionBaseFields & AuctionConstraints;
interface SponsoredListingAuction extends Omit<AuctionBase, "type"> {
type: "listings";
}
interface BannerAuction extends Omit<AuctionBase, "type"> {
device?: DeviceType;
slotId: string;
type: "banners";
}
export interface Auction {
auctions: (SponsoredListingAuction | BannerAuction)[];
} |
The SDK allows an auction to have 3 triggers, but the API allows at most two.
Adde compile time validation to ensure correct formation.