Schema Annotations
One of the key features of the Schema design is its flexibility and ability to be customized.
This is achieved through “annotations.”
Each node in the ast
field of a schema has an annotations: Record<string | symbol, unknown>
field,
which allows you to attach additional information to the schema.
You can manage these annotations using the annotations
method or the Schema.annotations
API.
Example (Using Annotations to Customize Schema)
The following table provides an overview of common built-in annotations and their uses:
Annotation | Description |
---|---|
identifier | Assigns a unique identifier to the schema, ideal for TypeScript identifiers and code generation purposes. Commonly used in tools like TreeFormatter to clarify output. Examples include "Person" , "Product" . |
title | Sets a short, descriptive title for the schema, similar to a JSON Schema title. Useful for documentation or UI headings. It is also used by TreeFormatter to enhance readability of error messages. |
description | Provides a detailed explanation about the schema’s purpose, akin to a JSON Schema description. Used by TreeFormatter to provide more detailed error messages. |
documentation | Extends detailed documentation for the schema, beneficial for developers or automated documentation generation. |
examples | Lists examples of valid schema values, akin to the examples attribute in JSON Schema, useful for documentation and validation testing. |
default | Defines a default value for the schema, similar to the default attribute in JSON Schema, to ensure schemas are pre-populated where applicable. |
message | Customizes the error message for validation failures, improving clarity in outputs from tools like TreeFormatter and ArrayFormatter during decoding or validation errors. |
jsonSchema | Specifies annotations that affect the generation of JSON Schema documents, customizing how schemas are represented. |
arbitrary | Configures settings for generating Arbitrary test data. |
pretty | Configures settings for generating Pretty output. |
equivalence | Configures settings for evaluating data Equivalence. |
concurrency | Controls concurrency behavior, ensuring schemas perform optimally under concurrent operations. Refer to Concurrency Annotation for detailed usage. |
batching | Manages settings for batching operations to enhance performance when operations can be grouped. |
parseIssueTitle | Provides a custom title for parsing issues, enhancing error descriptions in outputs from TreeFormatter. See ParseIssueTitle Annotation for more information. |
parseOptions | Allows overriding of parsing options at the schema level, offering granular control over parsing behaviors. See Customizing Parsing Behavior at the Schema Level for application details. |
decodingFallback | Provides a way to define custom fallback behaviors that trigger when decoding operations fail. Refer to Handling Decoding Errors with Fallbacks for detailed usage. |
For more complex schemas like Struct
, Array
, or Union
that contain multiple nested schemas, the concurrency
annotation provides a way to control how validations are executed concurrently.
Here’s a shorter version presented in a table:
Value | Description |
---|---|
number | Limits the maximum number of concurrent tasks. |
"unbounded" | All tasks run concurrently with no limit. |
"inherit" | Inherits concurrency settings from the parent context. |
undefined | Tasks run sequentially, one after the other (default behavior). |
Example (Sequential Execution)
In this example, we define three tasks that simulate asynchronous operations with different durations. Since no concurrency is specified, the tasks are executed sequentially, one after the other.
Example (Concurrent Execution)
By adding a concurrency
annotation set to "unbounded"
, the tasks can now run concurrently, meaning they don’t wait for one another to finish before starting. This allows faster execution when multiple tasks are involved.
The DecodingFallbackAnnotation
allows you to handle decoding errors by providing a custom fallback logic.
This annotation enables you to specify fallback behavior when decoding fails, making it possible to recover gracefully from errors.
Example (Basic Fallback)
In this basic example, when decoding fails (e.g., the input is null
), the fallback value is returned instead of an error.
Example (Advanced Fallback with Logging)
In this advanced example, when a decoding error occurs, the schema logs the issue and then returns a fallback value. This demonstrates how you can incorporate logging and other side effects during error handling.
In addition to built-in annotations, you can define custom annotations to meet specific requirements. For instance, here’s how to create a deprecated
annotation:
Example (Defining a Custom Annotation)
You can retrieve custom annotations using the AST.getAnnotation
helper method.
Example (Reading a Custom Annotation)