Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-keys-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Preserve broad index constraints when generating JSON Schema pattern properties.
4 changes: 3 additions & 1 deletion packages/effect/src/internal/schema/toJsonSchemaDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ function compileJsonSchema(
if (representation.propertySignatures.length > 0) out.properties = properties
if (required.length > 0) out.required = required
out.additionalProperties = options?.additionalProperties ?? false
let hasBroadIndexSignature = false
const patternProperties: Record<string, JsonSchema.JsonSchema | false> = {}
for (let index = 0; index < representation.indexSignatures.length; index++) {
const signature = representation.indexSignatures[index]
Expand All @@ -383,14 +384,15 @@ function compileJsonSchema(
new Set()
)
if (patterns.length === 0) {
hasBroadIndexSignature = true
out.additionalProperties = type
} else {
for (const pattern of patterns) InternalRecord.assignProperty(patternProperties, pattern, type)
}
}
if (Object.keys(patternProperties).length > 0) {
out.patternProperties = patternProperties
delete out.additionalProperties
if (!hasBroadIndexSignature) delete out.additionalProperties
}
if (
typeof out.additionalProperties === "object" &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,31 @@ describe("SchemaRepresentation.toJsonSchemaDocument", () => {
{ type: "object" }
)
})

it("retains a broad index constraint alongside patternProperties", () => {
const output = compile({
_tag: "Objects",
propertySignatures: [],
indexSignatures: [
{ parameter: StringRepresentation, type: NumberRepresentation },
{
parameter: {
_tag: "TemplateLiteral",
parts: [
{ _tag: "Literal", literal: "x_", checks: [] },
StringRepresentation
],
checks: []
},
type: StringRepresentation
}
],
checks: []
})

assert.isDefined(output.patternProperties)
assert.isDefined(output.additionalProperties)
})
})

describe("unions", () => {
Expand Down
Loading