Effect CommunityEC
Effect Community2y ago
3 replies
alex91

Error When Overriding Properties in Union Schema Using '@effect/schema/Schema'

Hi! I'm trying to override some properties schemas from a base schema using extends from a union but I get the following error:
- Schemas:

import * as S from '@effect/schema/Schema'

export const CreateMissionInputSharedSchema = S.Struct({
  isPastEvent: S.NullOr(S.Boolean),
  endDate: S.NullOr(S.Date),
})

export const CreateRegisterMissionInputSchema = S.Struct({
  ...CreateMissionInputSharedSchema.fields,
  // Simpler example, but the actual schema is more complex
  otherFields: S.String,
})

export const CreateBridgeMissionInputSchema = S.Struct({
  ...CreateMissionInputSharedSchema.fields,
  // Simpler example, but the actual schema is more complex
  otherFields2: S.String,
})

// Simpler example, but the actual schema is more complex
export const CreateMissionInputSchema = S.Union(
  CreateRegisterMissionInputSchema,
  CreateBridgeMissionInputSchema,
)

const PastMissionOverrides = S.Struct({
  isPastEvent: S.Boolean.pipe(
    S.filter(isPastEvent =>
      isPastEvent || {
        path: ['isPastEvent'],
        message: 'isPastEvent must be true',
      }
    ),
  ),
  endDate: S.Date.pipe(S.filter(endDate =>
    endDate < new Date() || {
      path: ['endDate'],
      message: 'endDate must be a past date',
    }
  )),
})

export const CreatePastMissionInputSchema = S.extend(
  CreateMissionInputSchema,
  PastMissionOverrides,
)


- Error:
Error: Unsupported schema or overlapping types
at path: ["isPastEvent"]
details: cannot extend null with boolean


Does anybody know how can I fix that or what would be the good pattern to achieve that? I'm trying to avoid extending from each schema of the union type to have less boilerplate, since my union type it's conformed by more schemas. Thanks in advance
Was this page helpful?