Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
handle excess properties for records (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored May 17, 2023
1 parent 1d5bb36 commit 9b9c3ee
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 107 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-mangos-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

handle excess properties for records
3 changes: 2 additions & 1 deletion src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,8 @@ const unify = (candidates: ReadonlyArray<AST>): ReadonlyArray<AST> => {
return out
}

const getParameterBase = (
/** @internal */
export const getParameterBase = (
ast: Parameter
): StringKeyword | SymbolKeyword | TemplateLiteral => {
switch (ast._tag) {
Expand Down
143 changes: 74 additions & 69 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ const go = untracedMethod(() =>
const indexSignatures = ast.indexSignatures.map((is) =>
[go(is.parameter, isBoundary), go(is.type, isBoundary)] as const
)
const parameter = go(AST.createUnion(
ast.indexSignatures.map((is) => AST.getParameterBase(is.parameter))
))
const expectedKeys: any = {}
for (let i = 0; i < propertySignatures.length; i++) {
expectedKeys[ast.propertySignatures[i].name] = null
Expand All @@ -605,15 +608,19 @@ const go = untracedMethod(() =>
// handle excess properties
// ---------------------------------------------
const onExcessPropertyError = options?.onExcessProperty === "error"
if (onExcessPropertyError && indexSignatures.length === 0) {
if (onExcessPropertyError) {
for (const key of I.ownKeys(input)) {
if (!(Object.prototype.hasOwnProperty.call(expectedKeys, key))) {
const e = PR.key(key, [PR.unexpected(input[key])])
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
const te = parameter(key)
const eu = PR.eitherOrUndefined(te)
if (eu && E.isLeft(eu)) {
const e = PR.key(key, [PR.unexpected(input[key])])
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
}
}
}
}
Expand Down Expand Up @@ -695,78 +702,76 @@ const go = untracedMethod(() =>
// ---------------------------------------------
// handle index signatures
// ---------------------------------------------
if (indexSignatures.length > 0) {
for (let i = 0; i < indexSignatures.length; i++) {
const parameter = indexSignatures[i][0]
const type = indexSignatures[i][1]
const keys = I.getKeysForIndexSignature(input, ast.indexSignatures[i].parameter)
for (const key of keys) {
if (Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
continue
}
// ---------------------------------------------
// handle keys
// ---------------------------------------------
const keu = PR.eitherOrUndefined(parameter(key, options))
if (keu) {
if (E.isLeft(keu)) {
const e = PR.key(key, keu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
}
for (let i = 0; i < indexSignatures.length; i++) {
const parameter = indexSignatures[i][0]
const type = indexSignatures[i][1]
const keys = I.getKeysForIndexSignature(input, ast.indexSignatures[i].parameter)
for (const key of keys) {
if (Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
continue
}
// ---------------------------------------------
// handle keys
// ---------------------------------------------
const keu = PR.eitherOrUndefined(parameter(key, options))
if (keu) {
if (E.isLeft(keu)) {
const e = PR.key(key, keu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
}
}
// there's no else here because index signature parameters are restricted to primitives
}
// there's no else here because index signature parameters are restricted to primitives

// ---------------------------------------------
// handle values
// ---------------------------------------------
const vpr = type(input[key], options)
const veu = PR.eitherOrUndefined(vpr)
if (veu) {
if (E.isLeft(veu)) {
const e = PR.key(key, veu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
}
// ---------------------------------------------
// handle values
// ---------------------------------------------
const vpr = type(input[key], options)
const veu = PR.eitherOrUndefined(vpr)
if (veu) {
if (E.isLeft(veu)) {
const e = PR.key(key, veu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
output[key] = veu.right
return PR.failures(mutableAppend(sortByIndex(es), e))
}
} else {
const nk = stepKey++
const index = key
if (!queue) {
queue = []
}
queue.push(
untracedMethod(() =>
({ es, output }: State) =>
Effect.flatMap(
Effect.either(vpr),
(tv) => {
if (E.isLeft(tv)) {
const e = PR.key(index, tv.left.errors)
if (allErrors) {
es.push([nk, e])
return Effect.unit()
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
}
} else {
output[key] = tv.right
output[key] = veu.right
}
} else {
const nk = stepKey++
const index = key
if (!queue) {
queue = []
}
queue.push(
untracedMethod(() =>
({ es, output }: State) =>
Effect.flatMap(
Effect.either(vpr),
(tv) => {
if (E.isLeft(tv)) {
const e = PR.key(index, tv.left.errors)
if (allErrors) {
es.push([nk, e])
return Effect.unit()
} else {
return PR.failures(mutableAppend(sortByIndex(es), e))
}
} else {
output[key] = tv.right
return Effect.unit()
}
)
)
}
)
)
}
)
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions test/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,18 @@ describe.concurrent("Decoder", () => {
{ a: "a" },
`/a Expected number, actual "a"`
)
const b = Symbol.for("@effect/schema/test/b")
await Util.expectParseFailure(
schema,
{ a: 1, [b]: "b" },
"/Symbol(@effect/schema/test/b) is unexpected",
Util.onExcessPropertyError
)
await Util.expectParseSuccess(
schema,
{ a: 1, [b]: "b" },
{ a: 1 }
)
})

it("struct/ record(symbol, number)", async () => {
Expand All @@ -552,6 +564,17 @@ describe.concurrent("Decoder", () => {
{ [a]: "a" },
`/Symbol(@effect/schema/test/a) Expected number, actual "a"`
)
await Util.expectParseFailure(
schema,
{ [a]: 1, b: "b" },
"/b is unexpected",
Util.onExcessPropertyError
)
await Util.expectParseSuccess(
schema,
{ [a]: 1, b: "b" },
{ [a]: 1 }
)
})

it("struct/ record(never, number)", async () => {
Expand Down Expand Up @@ -731,8 +754,7 @@ describe.concurrent("Decoder", () => {
await Util.expectParseSuccess(
schema,
{ a: "a", c: 1 },
{ a: "a" },
Util.onExcessPropertyIgnore
{ a: "a" }
)
await Util.expectParseSuccess(
schema,
Expand Down
3 changes: 1 addition & 2 deletions test/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ describe.concurrent("Encoder", () => {
await Util.expectEncodeSuccess(
schema,
{ a: "a", c: 1 },
{ a: "a" },
Util.onExcessPropertyIgnore
{ a: "a" }
)
await Util.expectEncodeSuccess(
schema,
Expand Down
14 changes: 8 additions & 6 deletions test/dev.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { pipe } from "@effect/data/Function"
import * as S from "@effect/schema/Schema"
import * as Util from "@effect/schema/test/util"

describe.concurrent("dev", () => {
it.skip("dev", async () => {
const schema = pipe(
S.string,
S.nonEmpty(),
S.message(() => "bla")
const schema = S.record(S.string, S.number)
const b = Symbol.for("@effect/schema/test/b")
await Util.expectParseFailure(
schema,
{ a: 1, [b]: "b" },
"/Symbol(@effect/schema/test/b) is unexpected",
Util.onExcessPropertyError
)
console.log(S.parse(schema)(""))
})
})
35 changes: 12 additions & 23 deletions test/onExcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import * as Util from "@effect/schema/test/util"
describe.concurrent("onExcess", () => {
it("ignore should not change tuple behaviour", async () => {
const schema = S.tuple(S.number)
await Util.expectParseFailure(schema, [1, "b"], "/1 is unexpected", Util.onExcessPropertyIgnore)
await Util.expectParseFailure(schema, [1, "b"], "/1 is unexpected")
await Util.expectEncodeFailure(
schema,
[1, "b"] as any,
`/1 is unexpected`,
Util.onExcessPropertyIgnore
`/1 is unexpected`
)
})

Expand All @@ -22,8 +21,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseSuccess(
schema,
{ a: 1, b: "b", c: true },
{ a: 1, b: "b" },
Util.onExcessPropertyIgnore
{ a: 1, b: "b" }
)
await Util.expectParseFailure(
schema,
Expand All @@ -34,8 +32,7 @@ describe.concurrent("onExcess", () => {
await Util.expectEncodeSuccess(
schema,
{ a: 1, b: "b" },
{ a: 1, b: "b" },
Util.onExcessPropertyIgnore
{ a: 1, b: "b" }
)
await Util.expectEncodeSuccess(
schema,
Expand All @@ -52,8 +49,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseFailure(
schema,
[1, "b", true],
`union member: /2 is unexpected, union member: /1 is unexpected`,
Util.onExcessPropertyIgnore
`union member: /2 is unexpected, union member: /1 is unexpected`
)
await Util.expectParseFailure(
schema,
Expand All @@ -64,8 +60,7 @@ describe.concurrent("onExcess", () => {
await Util.expectEncodeSuccess(
schema,
[1, "b"],
[1, "b"],
Util.onExcessPropertyIgnore
[1, "b"]
)
await Util.expectEncodeSuccess(
schema,
Expand All @@ -82,8 +77,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseSuccess(
schema,
[{ b: 1, c: "c" }],
[{ b: 1 }],
Util.onExcessPropertyIgnore
[{ b: 1 }]
)
})

Expand All @@ -92,8 +86,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseSuccess(
schema,
[{ b: 1, c: "c" }],
[{ b: 1 }],
Util.onExcessPropertyIgnore
[{ b: 1 }]
)
})

Expand All @@ -103,8 +96,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseSuccess(
schema,
[{ b: 1, c: "c" }],
[{ b: 1 }],
Util.onExcessPropertyIgnore
[{ b: 1 }]
)
})

Expand All @@ -113,8 +105,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseSuccess(
schema,
{ a: 1, b: "b" },
{ a: 1 },
Util.onExcessPropertyIgnore
{ a: 1 }
)
})

Expand All @@ -125,8 +116,7 @@ describe.concurrent("onExcess", () => {
{ a: { b: 1, c: "c" } },
{
a: { b: 1 }
},
Util.onExcessPropertyIgnore
}
)
})

Expand All @@ -135,8 +125,7 @@ describe.concurrent("onExcess", () => {
await Util.expectParseSuccess(
schema,
{ a: { b: 1, c: "c" } },
{ a: { b: 1 } },
Util.onExcessPropertyIgnore
{ a: { b: 1 } }
)
})
})
Expand Down
4 changes: 0 additions & 4 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ export const roundtrip = <I, A>(schema: Schema<I, A>) => {
}
}

export const onExcessPropertyIgnore: ParseOptions = {
onExcessProperty: "ignore"
}

export const onExcessPropertyError: ParseOptions = {
onExcessProperty: "error"
}
Expand Down

0 comments on commit 9b9c3ee

Please sign in to comment.