-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add and pass more regression tests for PerseusItem parser #1952
base: main
Are you sure you want to change the base?
Changes from 1 commit
aff016f
1e32c7b
9732a53
df61b01
6a70fc6
90816b0
8ddd620
981fdb4
94fc661
e0f10ec
0e3a235
7fba425
8b979f1
f5c4090
026af5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import {discriminatedUnion} from "./discriminated-union"; | ||
import {constant} from "./constant"; | ||
import {object} from "./object"; | ||
import {number} from "./number"; | ||
import {parse} from "../parse"; | ||
import {failure, success} from "../result"; | ||
|
||
describe("a discriminatedUnion with one variant", () => { | ||
const unionParser = discriminatedUnion( | ||
object({type: constant("ok")}), | ||
object({type: constant("ok"), value: number}), | ||
).parser; | ||
|
||
it("parses a valid value", () => { | ||
const input = {type: "ok", value: 3}; | ||
|
||
expect(parse(input, unionParser)).toEqual(success(input)) | ||
}) | ||
|
||
it("rejects a value with the wrong `type`", () => { | ||
const input = {type: "bad", value: 3}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).type -- expected "ok", but got "bad"`), | ||
) | ||
}) | ||
|
||
it("rejects a value with a valid type but wrong fields", () => { | ||
const input = {type: "ok", value: "foobar"}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).value -- expected number, but got "foobar"`), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be useful to include the discriminant info here? Otherwise we don't know which union entry has bad data. 🤔
Maybe that gets too verbose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that would be better. The parser context currently doesn't have a way to track which variant of a union you're currently trying to parse, so I can't think of a way to add this info to the message easily. I'll try adding a |
||
) | ||
}) | ||
}) | ||
|
||
describe("a discriminatedUnion with two variants", () => { | ||
const unionParser = discriminatedUnion( | ||
object({type: constant("rectangle")}), | ||
object({type: constant("rectangle"), width: number}), | ||
).or( | ||
object({type: constant("circle")}), | ||
object({type: constant("circle"), radius: number}), | ||
).parser; | ||
|
||
it("parses a valid rectangle", () => { | ||
const input = {type: "rectangle", width: 42}; | ||
|
||
expect(parse(input, unionParser)).toEqual(success(input)) | ||
}) | ||
|
||
it("rejects a rectangle with no width", () => { | ||
const input = {type: "rectangle", radius: 99}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).width -- expected number, but got undefined`)) | ||
}) | ||
|
||
it("parses a valid circle", () => { | ||
const input = {type: "circle", radius: 7}; | ||
|
||
expect(parse(input, unionParser)).toEqual(success(input)) | ||
}) | ||
|
||
it("rejects a circle with no radius", () => { | ||
const input = {type: "circle", width: 99}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).radius -- expected number, but got undefined`) | ||
) | ||
}) | ||
|
||
it("rejects a value with an unrecognized `type`", () => { | ||
const input = {type: "triangle", width: -1, radius: 99}; | ||
|
||
expect(parse(input, unionParser)).toEqual( | ||
failure(`At (root).type -- expected "rectangle", but got "triangle"`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice of this could mention all eligible types. |
||
) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type {Parser} from "../parser-types"; | ||
import {isSuccess} from "../result"; | ||
import {pipeParsers} from "./pipe-parsers"; | ||
|
||
// discriminatedUnion() should be preferred over union() when parsing a | ||
// discriminated union type, because discriminatedUnion() produces more | ||
// understandable failure messages. It takes the discriminant as the source of | ||
// truth for which variant is to be parsed, and expects the other data to match | ||
// that variant. | ||
export function discriminatedUnion<T>(narrow: Parser<unknown>, parseVariant: Parser<T>): DiscriminatedUnionBuilder<T> { | ||
return new DiscriminatedUnionBuilder(pipeParsers(narrow).then(parseVariant).parser) | ||
} | ||
|
||
class DiscriminatedUnionBuilder<Variant> { | ||
constructor(public parser: Parser<Variant>) { | ||
} | ||
|
||
or<Variant2>( | ||
narrow: Parser<unknown>, | ||
parseVariant: Parser<Variant2>, | ||
): DiscriminatedUnionBuilder<Variant | Variant2> { | ||
return new DiscriminatedUnionBuilder(either(narrow, parseVariant, this.parser)); | ||
} | ||
} | ||
|
||
function either<A, B>(narrowToA: Parser<unknown>, parseA: Parser<A>, parseB: Parser<B>): Parser<A | B> { | ||
return (rawValue, ctx) => { | ||
if (isSuccess(narrowToA(rawValue, ctx))) { | ||
return parseA(rawValue, ctx) | ||
} | ||
|
||
return parseB(rawValue, ctx) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This confused me until I went and looked at the implementation and docs. I guess without having a "shape" that represents the discriminant, we'd have to rebuild how TypeScript decides what it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree that this is ugly. For comparison, here's how Zod does it:
I suppose we could do something like:
That would mirror how TypeScript thinks about discriminated unions. But the problem is, it wouldn't handle our versioned widget options. Those aren't (in TS's view) valid discriminated unions, since the discriminant,
version.major
, is a nested field. My current parser design allows us to treat them kind of like discriminated unions anyway, by splitting the choice of variant and the parsing of that variant into separate steps that can run arbitrary parsing logic.If only
version
were simply a number (like it should have been all along), we could just do the thing that makes intuitive sense here. The curse of pre-TS code strikes again! :/...and now, after writing all that out, I am tempted to create a special parser abstraction just for versioned widget options. Then we could simplify the one for discriminated unions to be more like Zod's API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like: