When working with schemas, you have a choice beyond the Schema.Struct constructor.
You can leverage the power of classes through the Schema.Class utility, which comes with its own set of advantages tailored to common use cases:
Classes offer several features that simplify the schema creation process:
All-in-One Definition: With classes, you can define both a schema and an opaque type simultaneously.
Shared Functionality: You can incorporate shared functionality using class methods or getters.
Value Hashing and Equality: Utilize the built-in capability for checking value equality and applying hashing (thanks to Class implementing Data.Class).
Definition
To define a class using Schema.Class, you need to specify:
The type of the class being created.
A unique identifier for the class.
The desired fields.
Example (Defining a Schema Class)
In this example, Person is both a schema and a TypeScript class. Instances of Person are created using the defined schema, ensuring compliance with the specified fields.
Example (Creating Instances)
Defining Classes Without Fields
When your schema does not require any fields, you can define a class with an empty object.
Example (Defining and Using a Class Without Arguments)
Validating Properties via Class Constructors
When you define a class using Schema.Class, the constructor automatically checks that the provided properties adhere to the schema’s rules.
Defining and Instantiating a Valid Class Instance
The constructor ensures that each property, like id and name, adheres to the schema. For instance, id must be a number, and name must be a non-empty string.
Example (Creating a Valid Instance)
Handling Invalid Properties
If invalid properties are provided during instantiation, the constructor throws an error, explaining why the validation failed.
Example (Creating an Instance with Invalid Properties)
The error clearly specifies that the name field failed to meet the NonEmptyString requirement.
Bypassing Validation
In some scenarios, you might want to bypass the validation logic. While not generally recommended, the library provides an option to do so.
Example (Bypassing Validation)
Automatic Hashing and Equality in Classes
Instances of classes created with Schema.Class support the Equal trait through their integration with Data.Class. This enables straightforward value comparisons, even across different instances.
Basic Equality Check
Two class instances are considered equal if their properties have identical values.
Example (Comparing Instances with Equal Properties)
Nested or Complex Properties
The Equal trait performs comparisons at the first level. If a property is a more complex structure, such as an array, instances may not be considered equal, even if the arrays themselves have identical values.
Example (Shallow Equality for Arrays)
To achieve deep equality for nested structures like arrays, use Schema.Data in combination with Data.array. This enables the library to compare each element of the array rather than treating it as a single entity.
Example (Using Schema.Data for Deep Equality)
Extending Classes with Custom Logic
Schema classes provide the flexibility to include custom getters and methods, allowing you to extend their functionality beyond the defined fields.
Adding Custom Getters
A getter can be used to derive computed values from the fields of the class. For example, a Person class can include a getter to return the name property in uppercase.
Example (Adding a Getter for Uppercase Name)
Adding Custom Methods
In addition to getters, you can define methods to encapsulate more complex logic or operations involving the class’s fields.
Example (Adding a Method)
Leveraging Classes as Schema Definitions
When you define a class with Schema.Class, it serves both as a schema and as a class. This dual functionality allows the class to be used wherever a schema is required.
Example (Using a Class in an Array Schema)
Exposed Values
The class also includes a fields static property, which outlines the fields defined during the class creation.
Example (Accessing the fields Property)
Adding Annotations
Classes as Transformation Schemas
When a class is defined, it is equivalent to creating a transformation schema that maps a structured schema to the class type.
For instance, consider the following definition:
This class definition implicitly defines a transformation schema from the following struct schema:
to a schema that represents the Person class.
Annotations allow you to provide metadata for your schemas, such as identifiers or descriptions. Depending on your requirements, annotations can be added to either the structured schema (the “from” part of the transformation) or the class schema (the “to” part of the transformation).
Annotating Input (Struct) Schemas
You can annotate the struct schema component, which is transformed into the class.
Example (Annotating the Struct Schema)
Annotating Class Schemas
Alternatively, annotations can be added directly to the class schema, affecting how the class is represented as a schema.
Example (Annotating the Class Schema)
Recursive Schemas
The Schema.suspend combinator is useful when you need to define a schema that depends on itself, like in the case of recursive data structures.
In this example, the Category schema depends on itself because it has a field subcategories that is an array of Category objects.
Example (Self-Referencing Schema)
Example (Missing Type Annotation Error)
Mutually Recursive Schemas
Sometimes, schemas depend on each other in a mutually recursive way. For instance, an arithmetic expression tree might include Expression nodes that can either be numbers or Operation nodes, which in turn reference Expression nodes.
Example (Arithmetic Expression Tree)
Recursive Types with Different Encoded and Type
Defining recursive schemas where the Encoded type differs from the Type type introduces additional complexity. For instance, if a schema includes fields that transform data (e.g., NumberFromString), the Encoded and Type types may not align.
In such cases, we need to define an interface for the Encoded type.
Let’s consider an example: suppose we want to add an id field to the Category schema, where the schema for id is NumberFromString.
It’s important to note that NumberFromString is a schema that transforms a string into a number, so the Type and Encoded types of NumberFromString differ, being number and string respectively.
When we add this field to the Category schema, TypeScript raises an error:
This error occurs because the explicit annotation S.suspend((): S.Schema<Category> => Category is no longer sufficient and needs to be adjusted by explicitly adding the Encoded type:
Example (Adjusting the Schema with Explicit Encoded Type)
As we’ve observed, it’s necessary to define an interface for the Encoded of the schema to enable recursive schema definition, which can complicate things and be quite tedious.
One pattern to mitigate this is to separate the field responsible for recursion from all other fields.
Example (Separating Recursive Field)
Tagged Class variants
You can also create classes that extend TaggedClass and TaggedError from the effect/Data module.
Example (Creating Tagged Classes and Errors)
Extending existing Classes
The extend static utility allows you to enhance an existing schema class by adding additional fields and functionality. This approach helps in building on top of existing schemas without redefining them from scratch.
Example (Extending a Schema Class)
Transformations
You can enhance schema classes with effectful transformations to enrich or validate entities, particularly when working with data sourced from external systems like databases or APIs.
Example (Effectful Transformation)
The following example demonstrates adding an age field to a Person class. The age value is derived asynchronously based on the id field.
The decision of which API to use, either transformOrFail or transformOrFailFrom, depends on when you wish to execute the transformation:
Using transformOrFail:
The transformation occurs at the end of the process.
It expects you to provide a value of type { age: Option<number> }.
After processing the initial input, the new transformation comes into play, and you need to ensure the final output adheres to the specified structure.
Using transformOrFailFrom:
The new transformation starts as soon as the initial input is handled.
You should provide a value { age?: number }.
Based on this fresh input, the subsequent transformation Schema.optionalWith(Schema.Number, { exact: true, as: "Option" }) is executed.
This approach allows for immediate handling of the input, potentially influencing the subsequent transformations.