Effect-TS / schema

Modeling the schema of data structures as first-class values

Home Page:https://effect.website

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Combining `transform`, `withDefault`, `extend`, and `array` causes unexpected parsing errors

nspaeth opened this issue Β· comments

commented

πŸ› Bug report

Current Behavior

const boolString = S.transform(
  S.string,
  S.boolean,
  x => !!x,
  x => '' + x
);

const Data = S.struct({ flag: boolString })
// The combination of `transform`, `withDefault`, `extend`, and `array`
const MySchema = pipe(
  S.struct({
    data: Data,
    wDefault: pipe(S.boolean, S.optional)
      .withDefault(() => true), // <- Commenting this out removes error
    //other: S.array(Data), <- This would work because it is not in extend
  }),
  S.extend(       // <- Taking it out of extends also fixes it
    S.struct({
      other: pipe(
        Data,
        S.array, // <- Commenting this out also fixes it
      ), 
    })
  )
)

const testData = {
  data: { flag: 'a string' },
  other: [{ flag: 'another string' }],
};
pipe(
  S.parseEither(MySchema)(testData, {errors: "all"}),
  E.map(r => console.log(JSON.stringify(r, null, 2))),
  E.mapLeft(e => console.log(formatErrors(e.errors))),
);

Results in

error(s) found
└─ ["other"][0]["flag"]
   └─ Expected string, actual true

It expects ["other"][0]["flag"] to be a string, but it is a string.

Expected behavior

I expect no errors.

Reproducible example

CcodeSandbox repro

Suggested solution(s)

Haven't investigated this yet.

Additional context

This example is just a useless contrived reproduction, my actual use-case is a recursive structure which requires S.extend, S.array, and the main structure uses both withDefault and a transform.

Your environment

I haven't tested other versions of schema yet.

Software Version(s)
@effect/schema 0.20.2
TypeScript 5. 1.3

@nspaeth thanks for the bug report, closed by #307