Skip to content
Closed
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
23 changes: 23 additions & 0 deletions ark/json-schema/__tests__/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { attest, contextualize } from "@ark/attest"
import { jsonSchemaToType } from "@ark/json-schema"
import type { JsonSchemaOrBoolean } from "@ark/schema"

contextualize(() => {
it("type string", () => {
Expand Down Expand Up @@ -47,6 +48,17 @@ contextualize(() => {
)
})

it("minLength (0)", () => {
const schema = {
type: "string",
minLength: 0
} satisfies JsonSchemaOrBoolean
const pattern = jsonSchemaToType(schema)

attest(() => jsonSchemaToType(schema))
attest(pattern.expression).snap("string")
})

it("pattern", () => {
const tPatternString = jsonSchemaToType({
type: "string",
Expand All @@ -57,4 +69,15 @@ contextualize(() => {
// https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.4.3
attest(tPatternString.allows("expression")).equals(true)
})

it("string enums", () => {
const enumKeys = ["keyOne", "keyTwo"]

const stringEnums = jsonSchemaToType({
type: "string",
enum: enumKeys
})

attest(stringEnums.expression).snap('"keyOne" | "keyTwo"')
})
})
2 changes: 1 addition & 1 deletion ark/json-schema/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export const parseCommonJsonSchema = (
return type.unit(jsonSchema.const)
}

if ("enum" in jsonSchema) return type.enumerated(jsonSchema.enum)
if ("enum" in jsonSchema) return type.enumerated(...jsonSchema.enum)
}
5 changes: 5 additions & 0 deletions ark/schema/__tests__/bounds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ contextualize(() => {
)
})

it("minLength 0 reduces to unconstrained", () => {
const T = rootSchema({ domain: "string", minLength: 0 })
attest(T.expression).snap("string")
})

for (const [min, max] of entriesOf(boundKindPairsByLower)) {
describe(`${min}/${max}`, () => {
const basis =
Expand Down
3 changes: 3 additions & 0 deletions ark/schema/constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ export const constraintKeyParser =
return nodes.sort((l, r) => (l.hash < r.hash ? -1 : 1)) as never
}
const child = ctx.$.node(kind, schema)
// If the constraint was reduced to a root node (like unknown for minLength: 0),
// omit it from the schema since it's trivially satisfied
if (child.isRoot()) return
return (child.hasOpenIntersection() ? [child] : child) as never
}

Expand Down
2 changes: 1 addition & 1 deletion ark/schema/roots/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ const writeIntersectionExpression = (node: Intersection.Node) => {
.map(n => n.expression)
.join(" & ")

const fullExpression = `${basisExpression}${basisExpression ? " " : ""}${refinementsExpression}`
const fullExpression = `${basisExpression}${basisExpression && refinementsExpression ? " " : ""}${refinementsExpression}`

if (fullExpression === "Array == 0") return "[]"

Expand Down
2 changes: 1 addition & 1 deletion ark/type/__tests__/pipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ contextualize(() => {

attest(indiscriminable).throws
.snap(`ParseError: An unordered union of a type including a morph and a type with overlapping input is indeterminate:
Left: { foo: (In: string ) => Out<Date> | false | true }
Left: { foo: (In: string) => Out<Date> | false | true }
Right: { foo: (In: string) => Out<{ [string]: $jsonObject | number | string | false | null | true }> | false | true }`)
})

Expand Down