export class Project extends S.Class<Project>("Project")({
id: withDefaultId(ProjectId),
code: S.String,
subCode: S.optional(S.String),
description: S.optional(S.String),
requiresClient: S.Boolean.pipe(withDefault(() => false)),
timestamp: S.DateFromNumber.pipe(withDefault(() => new Date()))
}) {}
//...
export const ProjectFromId = S.transformOrFail(ProjectId, Project, {
strict: true,
decode: (id, _, ast) =>
E.gen(function*() {
const projects = yield* ProvidedProjects
const project = yield* projects.find(id)
// this doesn't work
// return project
// but this does, which doesn't make sense - timestamp is `S.DateFromNumber`
// so the _encoded_ timestamp is a number, the _decoded_ timestamp is a date
// but it's only happy here if I coerce timestamp back to a number
return {
...project,
timestamp: project.timestamp instanceof Date ? project.timestamp.getTime() : project.timestamp
}
}).pipe(E.mapError((error) => new ParseResult.Type(ast, id, error.message))),
encode: (p) => ParseResult.succeed(ProjectId.make(p.id))
})
export class Project extends S.Class<Project>("Project")({
id: withDefaultId(ProjectId),
code: S.String,
subCode: S.optional(S.String),
description: S.optional(S.String),
requiresClient: S.Boolean.pipe(withDefault(() => false)),
timestamp: S.DateFromNumber.pipe(withDefault(() => new Date()))
}) {}
//...
export const ProjectFromId = S.transformOrFail(ProjectId, Project, {
strict: true,
decode: (id, _, ast) =>
E.gen(function*() {
const projects = yield* ProvidedProjects
const project = yield* projects.find(id)
// this doesn't work
// return project
// but this does, which doesn't make sense - timestamp is `S.DateFromNumber`
// so the _encoded_ timestamp is a number, the _decoded_ timestamp is a date
// but it's only happy here if I coerce timestamp back to a number
return {
...project,
timestamp: project.timestamp instanceof Date ? project.timestamp.getTime() : project.timestamp
}
}).pipe(E.mapError((error) => new ParseResult.Type(ast, id, error.message))),
encode: (p) => ParseResult.succeed(ProjectId.make(p.id))
})