Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Exclude property signatures from index signatures validations, fix #433
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Sep 21, 2023
1 parent a8a270c commit 119f7d5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 55 deletions.
112 changes: 57 additions & 55 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,70 +726,72 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
const type = indexSignatures[i][1]
const keys = Internal.getKeysForIndexSignature(input, ast.indexSignatures[i].parameter)
for (const key of keys) {
// ---------------------------------------------
// handle keys
// ---------------------------------------------
const keu = ParseResult.eitherOrUndefined(parameter(key, options))
if (keu) {
if (Either.isLeft(keu)) {
const e = ParseResult.key(key, keu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return ParseResult.failures(mutableAppend(sortByIndex(es), e))
if (!(Object.prototype.hasOwnProperty.call(expectedKeys, key))) {
// ---------------------------------------------
// handle keys
// ---------------------------------------------
const keu = ParseResult.eitherOrUndefined(parameter(key, options))
if (keu) {
if (Either.isLeft(keu)) {
const e = ParseResult.key(key, keu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return ParseResult.failures(mutableAppend(sortByIndex(es), e))
}
}
}
}
// there's no else here because index signature parameters are restricted to primitives

// ---------------------------------------------
// handle values
// ---------------------------------------------
const vpr = type(input[key], options)
const veu = ParseResult.eitherOrUndefined(vpr)
if (veu) {
if (Either.isLeft(veu)) {
const e = ParseResult.key(key, veu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
// there's no else here because index signature parameters are restricted to primitives

// ---------------------------------------------
// handle values
// ---------------------------------------------
const vpr = type(input[key], options)
const veu = ParseResult.eitherOrUndefined(vpr)
if (veu) {
if (Either.isLeft(veu)) {
const e = ParseResult.key(key, veu.left.errors)
if (allErrors) {
es.push([stepKey++, e])
continue
} else {
return ParseResult.failures(mutableAppend(sortByIndex(es), e))
}
} else {
return ParseResult.failures(mutableAppend(sortByIndex(es), e))
if (!Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
output[key] = veu.right
}
}
} else {
if (!Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
output[key] = veu.right
const nk = stepKey++
const index = key
if (!queue) {
queue = []
}
}
} else {
const nk = stepKey++
const index = key
if (!queue) {
queue = []
}
queue.push(
({ es, output }: State) =>
Effect.flatMap(
Effect.either(vpr),
(tv) => {
if (Either.isLeft(tv)) {
const e = ParseResult.key(index, tv.left.errors)
if (allErrors) {
es.push([nk, e])
return Effect.unit
queue.push(
({ es, output }: State) =>
Effect.flatMap(
Effect.either(vpr),
(tv) => {
if (Either.isLeft(tv)) {
const e = ParseResult.key(index, tv.left.errors)
if (allErrors) {
es.push([nk, e])
return Effect.unit
} else {
return ParseResult.failures(mutableAppend(sortByIndex(es), e))
}
} else {
return ParseResult.failures(mutableAppend(sortByIndex(es), e))
}
} else {
if (!Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
output[key] = tv.right
if (!Object.prototype.hasOwnProperty.call(expectedKeys, key)) {
output[key] = tv.right
}
return Effect.unit
}
return Effect.unit
}
}
)
)
)
)
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/Schema/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ describe.concurrent("Schema/extend", () => {
)
})

it("struct extend record(templateLiteral, string)", async () => {
const schema = S.struct({ a: S.string }).pipe(
S.extend(S.record(
S.templateLiteral(
S.string,
S.literal("-"),
S.number
),
S.string
))
)
// type A = {
// [x: `${string}-${number}`]: string
// readonly a: string
// }
// const a: A = { a: "a" } // OK
await Util.expectParseSuccess(schema, { a: "a" })
})

describe.concurrent("both operands are transformations", () => {
const BoolFromString = S.transform(
S.string,
Expand Down

0 comments on commit 119f7d5

Please sign in to comment.