Effect CommunityEC
Effect Community2y ago
11 replies
Ciferblat

Transforming Date Fields in TypeScript Schemas

Hi. I need an advice. So, we have two schemas:

const DecodedObject = {
    id: Number,
    ...
    planDate: Date,
    factDate: Date
}

const EncodedObject = {
    id: string,
    ...
    planDate_year: number,
    planDate_month: number,
    planDate_day: number,

    factDate_year: number,
    factDate_month: number,
    factDate_day: number,
}


I need somehow transform one field from DecodedObject to three different fields in EncodedObject.
Also planDate and factDate should have their corresponding mirror names with suffixes (_year, _month, _day).
planDate and factDate are just for example, the fields with desired type can have any name.

The ideal solution would be:

const DateFromYearMonthDayFields = Schema. <some transformation here>

const ObjectSchema = Schema.Struct({
  id: Schema.NumberFromString,
  dueDate: DateFromYearMonthDayFields,
  factDate: DateFromYearMonthDayFields
})


so the question is: how to implement DateFromYearMonthDayFields? Is this even possible or should I use some other algorithm for this kind of task?
Was this page helpful?