Effect CommunityEC
Effect Community2y ago
4 replies
BinaryArtifex

Dynamic Schema Design for Conditional Property Variation

is it possible to create a schema such that one of the properties can change depending on the value of another property? for instance, based on the schema below, there is a BaseTotals schema that all 3 aggregation values share, but there are some values unique to each one. is there a way to do this within this schema or would i need to extract out endTime and startTime as a 'base' and have a discriminated union of the 3 separate aggregation/totals schema combinations?

export const DataFeed = S.struct({
  aggregation: S.literal("5 minute", "day", "month"),
  endTime: S.NonEmpty,
  startTime: S.NonEmpty,
  totals: S.extend(BaseTotals, S.struct({...additional totals depending on aggregation})),
}).pipe(S.identifier("DataFeed"));
Was this page helpful?