Skip to content

Commit

Permalink
fix: propagate description on unions (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Jun 6, 2024
1 parent a7252a6 commit 21c0105
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-falcons-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arktype/schema": patch
---

Fix chained .describe() on union types (see [arktype CHANGELOG](../type/CHANGELOG.md))
14 changes: 3 additions & 11 deletions ark/schema/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
type Dict,
type Json,
type JsonData,
type PartialRecord,
type listable,
type propValueOf
type listable
} from "@arktype/util"
import {
nodeClassesByKind,
Expand All @@ -20,14 +18,12 @@ import {
import type { BaseNode } from "./node.js"
import type { UnknownRoot } from "./roots/root.js"
import type { RawRootScope } from "./scope.js"
import type { RawNodeDeclaration } from "./shared/declare.js"
import { Disjoint } from "./shared/disjoint.js"
import {
constraintKeys,
defaultValueSerializer,
isNodeKind,
precedenceOfKind,
type KeySchemainitions,
type NodeKind,
type RootKind,
type UnknownAttachments
Expand All @@ -53,10 +49,6 @@ export interface NodeParseContext<kind extends NodeKind = NodeKind>
schema: NormalizedSchema<kind>
}

const baseKeys: PartialRecord<string, propValueOf<KeySchemainitions<any>>> = {
description: { meta: true }
} satisfies KeySchemainitions<RawNodeDeclaration> as never

export const schemaKindOf = <kind extends RootKind = RootKind>(
schema: unknown,
allowedKinds?: readonly kind[]
Expand Down Expand Up @@ -122,7 +114,7 @@ export const parseNode = (kind: NodeKind, ctx: NodeParseContext): BaseNode => {
const children: BaseNode[] = []
for (const entry of schemaEntries) {
const k = entry[0]
const keyImpl = impl.keys[k] ?? baseKeys[k]
const keyImpl = impl.keys[k]
if (!keyImpl)
return throwParseError(`Key ${k} is not valid on ${kind} schema`)

Expand All @@ -136,7 +128,7 @@ export const parseNode = (kind: NodeKind, ctx: NodeParseContext): BaseNode => {
let typeJson: Record<string, unknown> = {}
entries.forEach(([k, v]) => {
const listableNode = v as listable<BaseNode>
const keyImpl = impl.keys[k] ?? baseKeys[k]
const keyImpl = impl.keys[k]
const serialize =
keyImpl.serialize ??
(keyImpl.child ? serializeListableChild : defaultValueSerializer)
Expand Down
4 changes: 2 additions & 2 deletions ark/schema/refinements/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { RawPrimitiveConstraint } from "../constraint.js"
import type { Node } from "../kinds.js"
import type { BaseMeta, RawNodeDeclaration } from "../shared/declare.js"
import type { KeySchemainitions, RangeKind } from "../shared/implement.js"
import type { KeySchemaDefinitions, RangeKind } from "../shared/implement.js"

export interface BaseRangeDeclaration extends RawNodeDeclaration {
kind: RangeKind
Expand Down Expand Up @@ -172,7 +172,7 @@ export type NumericallyBoundable = string | number | array

export type Boundable = NumericallyBoundable | Date

export const parseExclusiveKey: KeySchemainitions<BaseRangeDeclaration>["exclusive"] =
export const parseExclusiveKey: KeySchemaDefinitions<BaseRangeDeclaration>["exclusive"] =
{
// omit key with value false since it is the default
parse: (flag: boolean) => flag || undefined
Expand Down
18 changes: 14 additions & 4 deletions ark/schema/shared/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
type Entry,
type Json,
type JsonData,
type PartialRecord,
type entryOf,
type indexOf,
type keySet,
type keySetOf,
type listable,
type propValueOf,
type requireKeys,
type show
} from "@arktype/util"
Expand Down Expand Up @@ -233,11 +235,11 @@ export const schemaKindsRightOf = <kind extends RootKind>(
): schemaKindRightOf<kind>[] =>
rootKinds.slice(precedenceOfKind(kind) + 1) as never

export type KeySchemainitions<d extends RawNodeDeclaration> = {
[k in keyRequiringSchemainition<d>]: NodeKeyImplementation<d, k>
export type KeySchemaDefinitions<d extends RawNodeDeclaration> = {
[k in keyRequiringSchemaDefinition<d>]: NodeKeyImplementation<d, k>
}

type keyRequiringSchemainition<d extends RawNodeDeclaration> = Exclude<
type keyRequiringSchemaDefinition<d extends RawNodeDeclaration> = Exclude<
keyof d["normalizedSchema"],
keyof BaseMeta
>
Expand Down Expand Up @@ -279,7 +281,7 @@ export type NodeKeyImplementation<

interface CommonNodeImplementationInput<d extends RawNodeDeclaration> {
kind: d["kind"]
keys: KeySchemainitions<d>
keys: KeySchemaDefinitions<d>
normalize: (schema: d["schema"]) => d["normalizedSchema"]
hasAssociatedError: d["errorContext"] extends null ? false : true
finalizeJson?: (json: { [k in keyof d["inner"]]: JsonData }) => Json
Expand Down Expand Up @@ -366,6 +368,13 @@ export interface NarrowedAttachments<d extends RawNodeDeclaration>
children: Node<d["childKind"]>[]
}

export const baseKeys: PartialRecord<
string,
propValueOf<KeySchemaDefinitions<any>>
> = {
description: { meta: true }
} satisfies KeySchemaDefinitions<RawNodeDeclaration> as never

export const implementNode = <d extends RawNodeDeclaration = never>(
_: nodeImplementationInputOf<d>
): nodeImplementationOf<d> => {
Expand All @@ -388,5 +397,6 @@ export const implementNode = <d extends RawNodeDeclaration = never>(
return problemWithLocation
}
}
Object.assign(implementation.keys, baseKeys)
return implementation as never
}
9 changes: 9 additions & 0 deletions ark/type/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# arktype

## 2.0.0-dev.21

### Fix chained .describe() on union types

```ts
// now correctly adds the description to the union and its branches
const t = type("number|string").describe("My custom type")
```

## 2.0.0-dev.20

### Fix autocomplete for private aliases
Expand Down
11 changes: 11 additions & 0 deletions ark/type/__tests__/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,15 @@ contextualize(() => {
writeUnresolvableMessage("nummer")
)
})

it("chained description", () => {
const t = type("number|string").describe("My custom type")
attest(t.json).snap({
branches: [
{ description: "My custom type", domain: "number" },
{ description: "My custom type", domain: "string" }
],
description: "My custom type"
})
})
})
2 changes: 1 addition & 1 deletion ark/type/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "arktype",
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
"version": "2.0.0-dev.20",
"version": "2.0.0-dev.21",
"license": "MIT",
"author": {
"name": "David Blass",
Expand Down

0 comments on commit 21c0105

Please sign in to comment.