-
Notifications
You must be signed in to change notification settings - Fork 62
Open
Description
Add support for collection schema to calculate a derived value which cannot be set by a user
Example:
The Input
takes a price and quantity, but the Output
automatically calculates and includes the finalPrice
.
-
Zod Schema:
import { z } from 'zod'; const CartItemSchema = z.object({ price: z.number().positive(), discountPercent: z.number().min(0).max(100).default(0), }).transform(data => { return { ...data, finalPrice: data.price * data.discountPercent; }; });
-
Substantial Difference for
update
:
Then insert / update should be using the schema input type instead of output.
The draft
would not have a finalPrice
field. It would be impossible to set it directly, correctly forcing the developer to update a source field like draft.quantity = 3
and letting the transform logic do its job. This prevents a whole class of bugs.
Since input would then not be specific to insert, it's might be worth updating the name:
CollectionInsertInput
-> CollectionInput
TInsertInput
-> TInput
baiirun
Metadata
Metadata
Assignees
Labels
No labels