Integrating default values in case of parsing errors can be achieved by using a combination of `S...

I am currently trying to integrate effect schemas into my tanstack search filter – I have succeeded so far but have a problem, instead of erroring out when something couldn't be parsed I want to fall back to a default value as otherwise the site won't load which I'd like to avoid. How can I achieve that? I currently have the following filter (snip):

Schema.Struct({
  view: Schema.Literal("grid", "list", "compact").pipe(
    Schema.optionalWith({ default: Function.constant("grid" as const) }),
  ),
  q: Schema.String.pipe(
    Schema.optionalWith({ default: Function.constant("") }),
  ),
  filter: Schema.Struct({
    status: UniqueArray(Schema.encodedSchema(ProjectSpace.Status.Status)).pipe(
      Schema.optionalWith({ default: Function.constant([]) }),
    ),
    usage: UniqueArray(Schema.Literal("high", "medium", "low")).pipe(
      Schema.optionalWith({ default: Function.constant([]) }),
    ),
    alerts: UniqueArray(Schema.Literal("critical", "warning", "none")).pipe(
      Schema.optionalWith({ default: Function.constant([]) }),
    ),
    location: UniqueArray(Schema.NonEmptyTrimmedString).pipe(
      Schema.optionalWith({ default: Function.constant([]) }),
    ),
    tags: UniqueArray(Schema.NonEmptyTrimmedString).pipe(
      Schema.optionalWith({ default: Function.constant([]) }),
    ),
    memberCount: Schema.Struct({
      min: Schema.String.pipe(
        Schema.filter((value) => Number.parseInt(value, 10) >= 0),
        Schema.optional,
      ),
      max: Schema.String.pipe(
        Schema.filter((value) => Number.parseInt(value, 10) >= 0),
        Schema.optional,
      ),
    }).pipe(
      Schema.optionalWith({
        default: () => ({ min: undefined, max: undefined }),
      }),
    ),
  }).pipe(
    Schema.optionalWith({
      default: () => ({
        status: [],
        usage: [],
        alerts: [],
        location: [],
        tags: [],
        memberCount: { min: undefined, max: undefined },
      }),
    }),
  ),
});


but I am unsure as to how to implement the fallback on error, e.g. if an alert doesn't happen to be one of the choices to fallback gracefully. Any ideas would be appreciated thank you!
Was this page helpful?