Skip to content

Commit

Permalink
Schema.equivalence: fix discriminated tuples (#3789)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored and tim-smart committed Oct 17, 2024
1 parent 5d95f69 commit bba35b3
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
5 changes: 1 addition & 4 deletions packages/effect/src/ParseResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1383,8 +1383,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => {
let stepKey = 0
let candidates: Array<AST.AST> = []
if (len > 0) {
// if there is at least one key then input must be an object
if (isObject(input)) {
if (Predicate.isRecordOrArray(input)) {
for (let i = 0; i < len; i++) {
const name = ownKeys[i]
const buckets = searchTree.keys[name].buckets
Expand Down Expand Up @@ -1512,8 +1511,6 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => {
}
}

const isObject = (input: unknown): input is { [x: PropertyKey]: unknown } => typeof input === "object" && input !== null

const fromRefinement = <A>(ast: AST.AST, refinement: (u: unknown) => u is A): Parser => (u) =>
refinement(u) ? Either.right(u) : Either.left(new Type(ast, u))

Expand Down
4 changes: 3 additions & 1 deletion packages/effect/src/Predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ export const isNever: (input: unknown) => input is never = (_: unknown): _ is ne
*/
export const isUnknown: (input: unknown) => input is unknown = (_): _ is unknown => true

const isRecordOrArray = (input: unknown) => typeof input === "object" && input !== null
/** @internal */
export const isRecordOrArray = (input: unknown): input is { [x: PropertyKey]: unknown } =>
typeof input === "object" && input !== null

/**
* Tests if a value is an `object`.
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9813,7 +9813,7 @@ const go = (ast: AST.AST, path: ReadonlyArray<PropertyKey>): Equivalence.Equival
const len = ownKeys.length
return Equivalence.make((a, b) => {
let candidates: Array<AST.AST> = []
if (len > 0 && Predicate.isRecord(a)) {
if (len > 0 && Predicate.isRecordOrArray(a)) {
for (let i = 0; i < len; i++) {
const name = ownKeys[i]
const buckets = searchTree.keys[name].buckets
Expand Down
8 changes: 4 additions & 4 deletions packages/effect/test/Schema/Schema/Union/Union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Union", () => {
await Util.expectDecodeUnknownFailure(schema, 1, "Expected never, actual 1")
})

it("struct members", async () => {
it("discriminated structs", async () => {
const schema = S.Union(
S.Struct({ a: S.Literal(1), c: S.String }),
S.Struct({ b: S.Literal(2), d: S.Number })
Expand Down Expand Up @@ -82,7 +82,7 @@ describe("Union", () => {
)
})

it("struct members with multiple tags", async () => {
it("discriminated structs with multiple tags", async () => {
const schema = S.Union(
S.Struct({ category: S.Literal("catA"), tag: S.Literal("a") }),
S.Struct({ category: S.Literal("catA"), tag: S.Literal("b") }),
Expand Down Expand Up @@ -158,7 +158,7 @@ describe("Union", () => {
)
})

it("tuple members", async () => {
it("discriminated tuples", async () => {
const schema = S.Union(
S.Tuple(S.Literal("a"), S.String),
S.Tuple(S.Literal("b"), S.Number)
Expand Down Expand Up @@ -194,7 +194,7 @@ describe("Union", () => {
)
})

it("tuple members with multiple tags", async () => {
it("discriminated tuples with multiple tags", async () => {
const schema = S.Union(
S.Tuple(S.Literal("a"), S.Literal("b"), S.String),
S.Tuple(S.Literal("a"), S.Literal("c"), S.Number),
Expand Down
8 changes: 4 additions & 4 deletions packages/effect/test/Schema/Schema/equivalence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const MySymbol = S.SymbolFromSelf.annotations({
}
})

describe("SchemaEquivalence", () => {
it("the errors should disply a path", () => {
describe("equivalence", () => {
it("the errors should display a path", () => {
expect(() => S.equivalence(S.Tuple(S.Never as any))).toThrow(
new Error(`Unsupported schema
at path: [0]
Expand Down Expand Up @@ -301,7 +301,7 @@ schema (NeverKeyword): never`)
// propertyType(schema)
})

it("discriminated", () => {
it("discriminated structs", () => {
const schema = S.Union(
S.Struct({ tag: S.Literal("a"), a: MyString }),
S.Struct({ tag: S.Literal("b"), b: S.Number })
Expand All @@ -321,7 +321,7 @@ schema (NeverKeyword): never`)
S.Tuple(S.Literal("a"), S.String),
S.Tuple(S.Literal("b"), S.Number)
)
const equivalence = E.make(schema)
const equivalence = S.equivalence(schema)

expect(equivalence(["a", "x"], ["a", "x"])).toBe(true)
expect(equivalence(["a", "x"], ["a", "y"])).toBe(false)
Expand Down

0 comments on commit bba35b3

Please sign in to comment.