Recursive schema fails to encode non primitive value
andcan opened this issue Β· comments
π Bug report
Current Behavior
Fail to encode non primitive value when using recursive schemas.
Looks like the encode function from transformResult
(PlainDateFromString
and InstantFromString
schemas) is not called.
Non primitive values are encoded as empty objects:
AssertionError: expected { date: {}, instant: {}, β¦(6) } to match object { date: '2022-01-01', β¦(7) }
- Expected
+ Received
Object {
"array": Array [
Object {
"boolean": true,
"null": null,
"number": 2,
"string": "string",
},
Object {
"boolean": true,
"null": null,
"number": 3,
"string": "string",
},
],
"boolean": true,
- "date": "2022-01-01",
- "instant": "2022-01-01T00:00Z",
+ "date": Object {},
+ "instant": Object {},
"null": null,
"number": 1,
"object": Object {
"boolean": true,
"null": null,
"number": 1,
"string": "string",
},
"string": "string",
}
β― src/xson.spec.ts:202:20
200| it("should encode valid xson", () => {
201| const result = S.encodeSync(Xson)(xson);
202| expect(result).toMatchObject(fromXson);
| ^
203| });
204| });
Expected behavior
To correctly encode non primitive values in recursive schemas.
Reproducible example
Additional context
The error occurrs only when using recursive schema (see PlainDateRecord
test).
Your environment
Tested with both node v16.20.0
and v18.16.1
.
OS: Linux 5.15.120-1-MANJARO
Which versions of @effect/schema
are affected by this issue? Did this work in previous versions of @effect/schema
?
Don't known if it used to work.
Software | Version(s) |
---|---|
@effect/schema | ^0.26.0 |
TypeScript | ^4.9.5 |
@andcan my hypothesis is that XsonObject
has higher precedence than PlainDateFromString
and InstantFromString
, which are therefore encoded as records, specifically {}
.
export type Xson =
| undefined
| null
| boolean
| number
| string
| Temporal.PlainDate
| Temporal.Instant
| XsonArray
| XsonObject; // <= this takes precedence
I've released a patch (v0.26.1) that makes the sorting algorithm for union members smarter. It should fix the problem. Let me know (tried the patch in your repro, there's still an error but it doesn't seem related to this issue).
It works! Thank you for the quick fix!