The Data module in the Effect ecosystem simplifies value comparison by automatically implementing the Equal and Hash traits. This eliminates the need for manual implementations, making equality checks straightforward.
Example (Comparing Structs with Data)
By default, schemas like Schema.Struct do not implement the Equal and Hash traits. This means that two decoded objects with identical values will not be considered equal.
Example (Default Behavior Without Equal and Hash)
The Schema.Data function can be used to enhance a schema by including the Equal and Hash traits. This allows the resulting objects to support value-based equality.
Example (Using Schema.Data to Add Equality)
Config
The Schema.Config function allows you to validate and manage application configuration settings using structured schemas. It ensures consistency in configuration data and provides detailed feedback for validation errors.
Syntax
This function requires two parameters:
name: Identifier for the configuration setting.
schema: Schema describing the expected data type and structure.
The function returns a Config object that is directly integrated with your application’s configuration management system.
The Schema.Config function operates through the following steps:
Fetching Configuration: The configuration value is retrieved based on its name.
Validation: The value is then validated against the schema. If the value does not conform to the schema, the function formats and returns detailed validation errors.
Error Formatting: Errors are formatted using TreeFormatter.formatErrorSync to provide clear, actionable error messages.
Example (Validating Configuration Settings)
To test the configuration, execute the following commands:
Test (with Missing Configuration Data)
Test (with Invalid Data)
Test (with Valid Data)
Option
Option
The Schema.Option function is useful for converting an Option into a JSON-serializable format.
Syntax
Decoding
Input
Output
{ _tag: "None" }
Converted to Option.none()
{ _tag: "Some", value: I }
Converted to Option.some(a), where I is decoded into A using the inner schema
Encoding
Input
Output
Option.none()
Converted to { _tag: "None" }
Option.some(A)
Converted to { _tag: "Some", value: I }, where A is encoded into I using the inner schema
Example
OptionFromSelf
The Schema.OptionFromSelf function is designed for scenarios where Option values are already in the Option format and need to be decoded or encoded while transforming the inner value according to the schema.
Syntax
Decoding
Input
Output
Option.none()
Remains as Option.none()
Option.some(I)
Converted to Option.some(A), where I is decoded into A using the inner schema
Encoding
Input
Output
Option.none()
Remains as Option.none()
Option.some(A)
Converted to Option.some(I), where A is encoded into I using the inner schema
Example
OptionFromUndefinedOr
The Schema.OptionFromUndefinedOr function handles cases where undefined is treated as Option.none(), and all other values are interpreted as Option.some() based on the provided schema.
Syntax
Decoding
Input
Output
undefined
Converted to Option.none()
I
Converted to Option.some(A), where I is decoded into A using the inner schema
Encoding
Input
Output
Option.none()
Converted to undefined
Option.some(A)
Converted to I, where A is encoded into I using the inner schema
Example
OptionFromNullOr
The Schema.OptionFromUndefinedOr function handles cases where null is treated as Option.none(), and all other values are interpreted as Option.some() based on the provided schema.
Syntax
Decoding
Input
Output
null
Converted to Option.none()
I
Converted to Option.some(A), where I is decoded into A using the inner schema
Encoding
Input
Output
Option.none()
Converted to null
Option.some(A)
Converted to I, where A is encoded into I using the inner schema
Example
OptionFromNullishOr
The Schema.OptionFromNullishOr function handles cases where null or undefined are treated as Option.none(), and all other values are interpreted as Option.some() based on the provided schema. Additionally, it allows customization of how Option.none() is encoded (null or undefined).
Syntax
Decoding
Input
Output
undefined
Converted to Option.none()
null
Converted to Option.none()
I
Converted to Option.some(A), where I is decoded into A using the inner schema
Encoding
Input
Output
Option.none()
Converted to undefined or null based on user choice (onNoneEncoding)
Option.some(A)
Converted to I, where A is encoded into I using the inner schema
Example
OptionFromNonEmptyTrimmedString
The Schema.OptionFromNonEmptyTrimmedString schema is designed for handling strings where trimmed empty strings are treated as Option.none(), and all other strings are converted to Option.some().
Decoding
Input
Output
s: string
Converted to Option.some(s), if s.trim().length > 0
Converted to Option.none() otherwise
Encoding
Input
Output
Option.none()
Converted to ""
Option.some(s: string)
Converted to s
Example
Either
Either
The Schema.Either function is useful for converting an Either into a JSON-serializable format.
Syntax
Decoding
Input
Output
{ _tag: "Left", left: LI }
Converted to Either.left(LA), where LI is decoded into LA using the inner left schema
{ _tag: "Right", right: RI }
Converted to Either.right(RA), where RI is decoded into RA using the inner right schema
Encoding
Input
Output
Either.left(LA)
Converted to { _tag: "Left", left: LI }, where LA is encoded into LI using the inner left schema
Either.right(RA)
Converted to { _tag: "Right", right: RI }, where RA is encoded into RI using the inner right schema
Example
EitherFromSelf
The Schema.EitherFromSelf function is designed for scenarios where Either values are already in the Either format and need to be decoded or encoded while transforming the inner valued according to the schemas.
Syntax
Decoding
Input
Output
Either.left(LI)
Converted to Either.left(LA), where LI is decoded into LA using the inner left schema
Either.right(RI)
Converted to Either.right(RA), where RI is decoded into RA using the inner right schema
Encoding
Input
Output
Either.left(LA)
Converted to Either.left(LI), where LA is encoded into LI using the inner left schema
Either.right(RA)
Converted to Either.right(RI), where RA is encoded into RI using the inner right schema
Example
EitherFromUnion
The Schema.EitherFromUnion function is designed to decode and encode Either values where the left and right sides are represented as distinct types. This schema enables conversions between raw union types and structured Either types.
Syntax
Decoding
Input
Output
LI
Converted to Either.left(LA), where LI is decoded into LA using the inner left schema
RI
Converted to Either.right(RA), where RI is decoded into RA using the inner right schema
Encoding
Input
Output
Either.left(LA)
Converted to LI, where LA is encoded into LI using the inner left schema
Either.right(RA)
Converted to RI, where RA is encoded into RI using the inner right schema
Example
ReadonlySet
ReadonlySet
The Schema.ReadonlySet function is useful for converting a ReadonlySet into a JSON-serializable format.
Syntax
Decoding
Input
Output
ReadonlyArray<I>
Converted to ReadonlySet<A>, where I is decoded into A using the inner schema
Encoding
Input
Output
ReadonlySet<A>
ReadonlyArray<I>, where A is encoded into I using the inner schema
Example
ReadonlySetFromSelf
The Schema.ReadonlySetFromSelf function is designed for scenarios where ReadonlySet values are already in the ReadonlySet format and need to be decoded or encoded while transforming the inner values according to the schema.
Syntax
Decoding
Input
Output
ReadonlySet<I>
Converted to ReadonlySet<A>, where I is decoded into A using the inner schema
Encoding
Input
Output
ReadonlySet<A>
ReadonlySet<I>, where A is encoded into I using the inner schema
Example
ReadonlyMap
The Schema.ReadonlyMap function is useful for converting a ReadonlyMap into a JSON-serializable format.
ReadonlyMap
Syntax
Decoding
Input
Output
ReadonlyArray<readonly [KI, VI]>
Converted to ReadonlyMap<KA, VA>, where KI is decoded into KA using the inner key schema and VI is decoded into VA using the inner value schema
Encoding
Input
Output
ReadonlyMap<KA, VA>
Converted to ReadonlyArray<readonly [KI, VI]>, where KA is decoded into KI using the inner key schema and VA is decoded into VI using the inner value schema
Example
ReadonlyMapFromSelf
The Schema.ReadonlyMapFromSelf function is designed for scenarios where ReadonlyMap values are already in the ReadonlyMap format and need to be decoded or encoded while transforming the inner values according to the schemas.
Syntax
Decoding
Input
Output
ReadonlyMap<KI, VI>
Converted to ReadonlyMap<KA, VA>, where KI is decoded into KA using the inner key schema and VI is decoded into VA using the inner value schema
Encoding
Input
Output
ReadonlyMap<KA, VA>
Converted to ReadonlyMap<KI, VI>, where KA is decoded into KI using the inner key schema and VA is decoded into VI using the inner value schema
Example
ReadonlyMapFromRecord
The Schema.ReadonlyMapFromRecord function is a utility to transform a ReadonlyMap into an object format, where keys are strings and values are serializable, and vice versa.
Syntax
Decoding
Input
Output
{ readonly [x: string]: VI }
Converts to ReadonlyMap<KA, VA>, where x is decoded into KA using the key schema and VI into VA using the value schema
Encoding
Input
Output
ReadonlyMap<KA, VA>
Converts to { readonly [x: string]: VI }, where KA is encoded into x using the key schema and VA into VI using the value schema
Example
HashSet
HashSet
The Schema.HashSet function provides a way to map between HashSet and an array representation, allowing for JSON serialization and deserialization.
Syntax
Decoding
Input
Output
ReadonlyArray<I>
Converts to HashSet<A>, where each element in the array is decoded into type A using the schema
Encoding
Input
Output
HashSet<A>
Converts to ReadonlyArray<I>, where each element in the HashSet is encoded into type I using the schema
Example
HashSetFromSelf
The Schema.HashSetFromSelf function is designed for scenarios where HashSet values are already in the HashSet format and need to be decoded or encoded while transforming the inner values according to the schema.
Syntax
Decoding
Input
Output
HashSet<I>
Converts to HashSet<A>, decoding each element from type I to type A using the schema
Encoding
Input
Output
HashSet<A>
Converts to HashSet<I>, encoding each element from type A to type I using the schema
Example
HashMap
HashMap
The Schema.HashMap function is useful for converting a HashMap into a JSON-serializable format.
Syntax
Input
Output
ReadonlyArray<readonly [KI, VI]>
Converts to HashMap<KA, VA>, where KI is decoded into KA and VI is decoded into VA using the specified schemas
Encoding
Input
Output
HashMap<KA, VA>
Converts to ReadonlyArray<readonly [KI, VI]>, where KA is encoded into KI and VA is encoded into VI using the specified schemas
Example
HashMapFromSelf
The Schema.HashMapFromSelf function is designed for scenarios where HashMap values are already in the HashMap format and need to be decoded or encoded while transforming the inner values according to the schemas.
Syntax
Decoding
Input
Output
HashMap<KI, VI>
Converts to HashMap<KA, VA>, where KI is decoded into KA and VI is decoded into VA using the specified schemas
Encoding
Input
Output
HashMap<KA, VA>
Converts to HashMap<KI, VI>, where KA is encoded into KI and VA is encoded into VI using the specified schemas
Example
SortedSet
SortedSet
The Schema.SortedSet function provides a way to map between SortedSet and an array representation, allowing for JSON serialization and deserialization.
Syntax
Decoding
Input
Output
ReadonlyArray<I>
Converts to SortedSet<A>, where each element in the array is decoded into type A using the schema
Encoding
Input
Output
SortedSet<A>
Converts to ReadonlyArray<I>, where each element in the SortedSet is encoded into type I using the schema
Example
SortedSetFromSelf
The Schema.SortedSetFromSelf function is designed for scenarios where SortedSet values are already in the SortedSet format and need to be decoded or encoded while transforming the inner values according to the schema.
Syntax
Decoding
Input
Output
SortedSet<I>
Converts to SortedSet<A>, decoding each element from type I to type A using the schema
Encoding
Input
Output
SortedSet<A>
Converts to SortedSet<I>, encoding each element from type A to type I using the schema
Example
Duration
The Duration schema family enables the transformation and validation of duration values across various formats, including hrtime, milliseconds, and nanoseconds.
Duration
Converts an hrtime(i.e. [seconds: number, nanos: number]) into a Duration.
Example
DurationFromSelf
The DurationFromSelf schema is designed to validate that a given value conforms to the Duration type.
Example
DurationFromMillis
Converts a number into a Duration where the number represents the number of milliseconds.
Example
DurationFromNanos
Converts a BigInt into a Duration where the number represents the number of nanoseconds.
Example
clampDuration
Clamps a Duration between a minimum and a maximum value.
Example
Redacted
Redacted
The Schema.Redacted function is specifically designed to handle sensitive information by converting a string into a Redacted object.
This transformation ensures that the sensitive data is not exposed in the application’s output.
Example (Basic Redacted Schema)
It’s important to note that when successfully decoding a Redacted, the output is intentionally obscured (<redacted>) to prevent the actual secret from being revealed in logs or console outputs.
Example (Exposure Risks During Errors)
In the example below, if the input string does not meet the criteria (e.g., contains spaces), the error message generated might inadvertently expose sensitive information included in the input.
Mitigating Exposure Risks
To reduce the risk of sensitive information leakage in error messages, you can customize the error messages to obscure sensitive details:
Example (Customizing Error Messages)
RedactedFromSelf
The Schema.RedactedFromSelf schema is designed to validate that a given value conforms to the Redacted type from the effect library.
Example
It’s important to note that when successfully decoding a Redacted, the output is intentionally obscured (<redacted>) to prevent the actual secret from being revealed in logs or console outputs.