Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
add descriptions to leafs
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 31, 2023
1 parent f4b01a1 commit 5fc77be
Show file tree
Hide file tree
Showing 40 changed files with 258 additions and 135 deletions.
36 changes: 36 additions & 0 deletions docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Added in v1.0.0
- [renaming](#renaming)
- [rename](#rename)
- [string constructors](#string-constructors)
- [Char](#char)
- [Lowercased](#lowercased)
- [NonEmpty](#nonempty)
- [Trimmed](#trimmed)
Expand Down Expand Up @@ -2729,6 +2730,8 @@ Added in v1.0.0

## between

This filter checks whether the provided number falls within the specified minimum and maximum values.

**Signature**

```ts
Expand All @@ -2743,6 +2746,10 @@ Added in v1.0.0

## finite

Ensures that the provided value is a finite number.

This schema filters out non-finite numeric values, allowing only finite numbers to pass through.

**Signature**

```ts
Expand All @@ -2755,6 +2762,8 @@ Added in v1.0.0

## greaterThan

This filter checks whether the provided number is greater than the specified minimum.

**Signature**

```ts
Expand All @@ -2768,6 +2777,8 @@ Added in v1.0.0

## greaterThanOrEqualTo

This filter checks whether the provided number is greater than or equal to the specified minimum.

**Signature**

```ts
Expand All @@ -2793,6 +2804,8 @@ Added in v1.0.0

## lessThan

This filter checks whether the provided number is less than the specified maximum.

**Signature**

```ts
Expand All @@ -2806,6 +2819,8 @@ Added in v1.0.0

## lessThanOrEqualTo

This schema checks whether the provided number is less than or equal to the specified maximum.

**Signature**

```ts
Expand Down Expand Up @@ -3140,6 +3155,18 @@ Added in v1.0.0

# string constructors

## Char

A schema representing a single character.

**Signature**

```ts
export declare const Char: Schema<string, string>
```

Added in v1.0.0

## Lowercased

**Signature**
Expand Down Expand Up @@ -3172,6 +3199,11 @@ Added in v1.0.0

## ULID

Represents a Universally Unique Lexicographically Sortable Identifier (ULID).

ULIDs are designed to be compact, URL-safe, and ordered, making them suitable for use as identifiers.
This schema ensures that the provided string adheres to the standard ULID format.

**Signature**

```ts
Expand All @@ -3182,6 +3214,10 @@ Added in v1.0.0

## UUID

Represents a Universally Unique Identifier (UUID).

This schema ensures that the provided string adheres to the standard UUID format.

**Signature**

```ts
Expand Down
6 changes: 3 additions & 3 deletions src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ export const isLiteral = (ast: AST): ast is Literal => ast._tag === "Literal"

/** @internal */
export const _null = createLiteral(null, {
[IdentifierAnnotationId]: "null",
[DescriptionAnnotationId]: "null"
[IdentifierAnnotationId]: "null"
})

/**
Expand Down Expand Up @@ -619,7 +618,8 @@ export const objectKeyword: ObjectKeyword = {
_tag: "ObjectKeyword",
annotations: {
[IdentifierAnnotationId]: "object",
[TitleAnnotationId]: "object"
[TitleAnnotationId]: "object",
[DescriptionAnnotationId]: "an object in the TypeScript meaning, i.e. the `object` type"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ArrayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const format = (self: ParseIssue, path: ReadonlyArray<PropertyKey> = []): Array<
return [{
_tag,
path,
message: `Unexpected, expected ${Format.formatAST(self.expected)}`
message: `Unexpected, expected ${Format.formatAST(self.expected, true)}`
}]
case "Union":
return ReadonlyArray.flatMap(self.errors, (e) => {
Expand Down
27 changes: 18 additions & 9 deletions src/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,25 @@ const formatTemplateLiteral = (ast: AST.TemplateLiteral): string =>
formatTemplateLiteralSpan(span) + span.literal
).join("") + "`"

const getExpected = (ast: AST.AST, verbose: boolean): Option.Option<string> =>
verbose ?
AST.getDescriptionAnnotation(ast).pipe(
Option.orElse(() => AST.getTitleAnnotation(ast)),
Option.orElse(() => AST.getIdentifierAnnotation(ast))
) :
AST.getIdentifierAnnotation(ast).pipe(
Option.orElse(() => AST.getTitleAnnotation(ast)),
Option.orElse(() => AST.getDescriptionAnnotation(ast))
const getExpected = (ast: AST.AST, verbose: boolean): Option.Option<string> => {
if (verbose) {
const description = AST.getDescriptionAnnotation(ast).pipe(
Option.orElse(() => AST.getTitleAnnotation(ast))
)
return Option.match(AST.getIdentifierAnnotation(ast), {
onNone: () => description,
onSome: (identifier) =>
Option.match(description, {
onNone: () => Option.some(identifier),
onSome: (description) => Option.some(`${identifier} (${description})`)
})
})
}
return AST.getIdentifierAnnotation(ast).pipe(
Option.orElse(() => AST.getTitleAnnotation(ast)),
Option.orElse(() => AST.getDescriptionAnnotation(ast))
)
}

const formatTuple = (ast: AST.Tuple): string => {
const formattedElements = ast.elements.map((element) =>
Expand Down
Loading

0 comments on commit 5fc77be

Please sign in to comment.