Skip to content

Add support for derived fields #237

@DawidWraga

Description

@DawidWraga

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


Split from other PR due to complexity (#)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions