The Data module in the Effect ecosystem serves as a utility module that simplifies the process of comparing values for equality without the need for explicit implementations of the Equal and Hash traits.
It provides convenient APIs that automatically generate default implementations for equality checks, making it easier for developers to perform equality comparisons in their applications:
You can use the Schema.Data(schema) combinator to build a schema from an existing schema that can decode a value A to a value with Equal and Hash traits added:
Config
The Schema.Config function is specifically designed to enhance configuration validation in software applications.
This feature empowers developers to integrate structured schema validation with configuration settings, ensuring that the configuration data is consistent with predefined schemas and providing detailed feedback when discrepancies are found.
The Schema.Config function is defined as follows:
This function requires two parameters:
name: The identifier for the configuration setting.
schema: A schema object that describes 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
To test the configuration, execute the following commands:
Test with Missing Configuration Data:
Test with Invalid Data:
Test with Valid Data:
Option
Option
Decoding
{ _tag: "None" } is converted to Option.none().
{ _tag: "Some", value: i } is converted to Option.some(a), where i is decoded into a using the inner schema.
Encoding
Option.none() is converted to { _tag: "None" }.
Option.some(a) is converted to { _tag: "Some", value: i }, where a is encoded into i using the inner schema.
Example
OptionFromSelf
Decoding
Option.none() remains as Option.none().
Option.some(i) is converted to Option.some(a), where i is decoded into a using the inner schema.
Encoding
Option.none() remains as Option.none().
Option.some(a) is converted to Option.some(i), where a is encoded into i using the inner schema.
Example
OptionFromUndefinedOr
Decoding
undefined is converted to Option.none().
i is converted to Option.some(a), where i is decoded into a using the inner schema.
Encoding
Option.none() is converted to undefined.
Option.some(a) is converted to i, where a is encoded into i using the inner schema.
Example
OptionFromNullOr
Decoding
null is converted to Option.none().
i is converted to Option.some(a), where i is decoded into a using the inner schema.
Encoding
Option.none() is converted to null.
Option.some(a) is converted to i, where a is encoded into i using the inner schema.
Example
OptionFromNullishOr
Decoding
null is converted to Option.none().
undefined is converted to Option.none().
i is converted to Option.some(a), where i is decoded into a using the inner schema.
Encoding
Option.none() is converted to a specified value (undefined or null based on user choice).
Option.some(a) is converted to i, where a is encoded into i using the inner schema.
Example
OptionFromNonEmptyTrimmedString
Decoding
s is converted to Option.some(s), if s.trim().length > 0.
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 from the effect library.
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 functionis 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
It’s important to note that when successfully decoding a Redacted, the output is intentionally obscured ({}) to prevent the actual secret from being revealed in logs or console outputs.
Practical Example Showing Potential Data Exposure
In the example above, 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:
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 ({}) to prevent the actual secret from being revealed in logs or console outputs.