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
31 changes: 31 additions & 0 deletions ark/schema/structure/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
append,
conflatenate,
conflatenateAll,
printable,
throwInternalError,
throwParseError,
Expand Down Expand Up @@ -481,6 +482,36 @@ export class SequenceNode extends BaseConstraint<Sequence.Declaration> {
return result
}

optionalize(): SequenceNode {
if (this.postfix) return this
if (!this.prefix?.length && !this.defaultables?.length) return this

const { prefix, defaultables, ...inner } = this.inner
return this.$.node("sequence", {
...inner,
optionals: conflatenateAll(
prefix,
defaultables?.map(d => d[0]),
inner.optionals
)
})
}

require(): SequenceNode {
if (this.postfix) return this
if (!this.optionals?.length && !this.defaultables?.length) return this

const { optionals, defaultables, ...inner } = this.inner
return this.$.node("sequence", {
...inner,
prefix: conflatenateAll(
inner.prefix,
defaultables?.map(d => d[0]),
optionals
)
})
}

// this depends on tuple so needs to come after it
expression: string = this.description

Expand Down
6 changes: 4 additions & 2 deletions ark/schema/structure/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,21 @@ export class StructureNode extends BaseConstraint<Structure.Declaration> {
}

optionalize(): StructureNode {
const { required, ...inner } = this.inner
const { required: _, sequence, ...inner } = this.inner
return this.$.node("structure", {
...inner,
...(sequence ? { sequence: sequence.optionalize() } : {}),
optional: this.props.map(prop =>
prop.hasKind("required") ? this.$.node("optional", prop.inner) : prop
)
})
}

require(): StructureNode {
const { optional, ...inner } = this.inner
const { optional: _, sequence, ...inner } = this.inner
return this.$.node("structure", {
...inner,
...(sequence ? { sequence: sequence.require() } : {}),
required: this.props.map(prop =>
prop.hasKind("optional") ?
{
Expand Down
35 changes: 35 additions & 0 deletions ark/type/__tests__/keywords/partial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,39 @@ contextualize(() => {

attest(T.expression).snap("{ [string]: number, bar?: 1, foo?: 1 }")
})

// https://github.com/arktypeio/arktype/issues/1480
it("tuple literal", () => {
const T = type(["'foo'", "'bar'"]).partial()
const Expected = type(["'foo'?", "'bar'?"])

attest<typeof Expected.t>(T.t)
attest<["foo"?, "bar"?]>(T.infer)
attest(T([])).equals([])
attest(T(["foo"])).equals(["foo"])
attest(T(["foo", "bar"])).equals(["foo", "bar"])
})

it("string syntax tuple", () => {
const tupleScope = scope({
tuple: ["'foo'", "'bar'"]
})

const T = tupleScope.type("Partial<tuple>")
const Expected = type(["'foo'?", "'bar'?"])

attest<typeof Expected.t>(T.t)
attest<["foo"?, "bar"?]>(T.infer)
attest(T.expression).equals(Expected.expression)
})

it("tuple with defaultable", () => {
const T = type(["string", "number = 5"]).partial()
const Expected = type(["string?", "number?"])

// https://github.com/arktypeio/arktype/issues/1160
// attest<typeof Expected.t>(T.t)
attest(T.expression).equals(Expected.expression)
attest(T([])).equals([])
})
})
33 changes: 33 additions & 0 deletions ark/type/__tests__/keywords/required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,37 @@ contextualize(() => {

attest(T.expression).equals(Expected.expression)
})

// reverse of https://github.com/arktypeio/arktype/issues/1480
it("tuple literal", () => {
const T = type(["'foo'?", "'bar'?"]).required()
const Expected = type(["'foo'", "'bar'"])

attest<typeof Expected.t>(T.t)
attest<["foo", "bar"]>(T.infer)
attest(T.expression).equals(Expected.expression)
attest(T(["foo", "bar"])).equals(["foo", "bar"])
})

it("string syntax tuple", () => {
const tupleScope = scope({
tuple: ["'foo'?", "'bar'?"]
})
const T = tupleScope.type("Required<tuple>")
const Expected = type(["'foo'", "'bar'"])

attest<typeof Expected.t>(T.t)
attest<["foo", "bar"]>(T.infer)
attest(T.expression).equals(Expected.expression)
attest(T(["foo", "bar"])).equals(["foo", "bar"])
})

it("tuple with defaultable", () => {
const T = type(["string", "number = 5"]).required()
const Expected = type(["string", "number"])

// https://github.com/arktypeio/arktype/issues/1160
// attest<typeof Expected.t>(T.t)
attest(T.expression).equals(Expected.expression)
})
})