Returns a "Standard Schema" object conforming to the Standard Schema
v1 specification.
This function creates a schema whose validate method attempts to decode and
validate the provided input synchronously. If the underlying Schema
includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Any detected defects will be reported via a single issue containing no
path.
The Schema.standardSchemaV1 API creates a schema whose validate method attempts to decode and validate the provided input synchronously. If the underlying Schema includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Example (Handling Synchronous and Asynchronous Validation)
1
import {
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect,
import Schema
Schema } from"effect"
2
3
// Utility function to display sync and async results
4
const
constprint: <T>(t:T) =>void|Promise<void>
print= <
function (typeparameter) Tin <T>(t:T):void|Promise<void>
T>(
t: T
t:
function (typeparameter) Tin <T>(t:T):void|Promise<void>
T) =>
5
t: T
tinstanceof
var Promise:PromiseConstructor
Represents the completion of an asynchronous operation
Attaches callbacks for the resolution and/or rejection of the Promise.
@param ― onfulfilled The callback to execute when the Promise is resolved.
@param ― onrejected The callback to execute when the Promise is rejected.
@returns ― A Promise for the completion of which ever callback is executed.
then((
x: any
x) =>
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@param ― value A JavaScript value, usually an object or array, to be converted.
@param ― replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.
@param ― space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
stringify(
x: any
x, null, 2)))
7
:
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@param ― value A JavaScript value, usually an object or array, to be converted.
@param ― replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.
@param ― space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
stringify(
t: T
t, null, 2))
8
9
// Define a synchronous schema
10
const
constsync:Schema.Struct<{
name:typeof Schema.String;
}>
sync=
import Schema
Schema.
functionStruct<{
name:typeof Schema.String;
}>(fields: {
name:typeof Schema.String;
}):Schema.Struct<{
name:typeof Schema.String;
}> (+1overload)
@since ― 3.10.0
Struct({
11
name: typeof Schema.String
name:
import Schema
Schema.
classString
exportString
@since ― 3.10.0
String
12
})
13
14
// Generate a Standard Schema V1 object
15
const
constsyncStandardSchema:StandardSchemaV1<{
readonlyname:string;
}, {
readonlyname:string;
}>
syncStandardSchema=
import Schema
Schema.
conststandardSchemaV1: <{
readonlyname:string;
}, {
readonlyname:string;
}>(schema:Schema.Schema<{
readonlyname:string;
}, {
readonlyname:string;
}, never>) =>StandardSchemaV1<{
readonlyname:string;
}, {
readonlyname:string;
}>
Returns a "Standard Schema" object conforming to the Standard Schema
v1 specification.
This function creates a schema whose validate method attempts to decode and
validate the provided input synchronously. If the underlying Schema
includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Any detected defects will be reported via a single issue containing no
path.
Suspends the execution of an effect for a specified Duration.
Details
This function pauses the execution of an effect for a given duration. It is
asynchronous, meaning that it does not block the fiber executing the effect.
Instead, the fiber is suspended during the delay period and can resume once
the specified time has passed.
The duration can be specified using various formats supported by the
Duration module, such as a string ("2 seconds") or numeric value
representing milliseconds.
@example
import { Effect } from"effect"
constprogram= Effect.gen(function*() {
console.log("Starting task...")
yield* Effect.sleep("3 seconds") // Waits for 3 seconds
}) => <A, E, R>(self:Effect.Effect<A, E, R>) =>Effect.Effect<{
readonlyname:string;
}, E, R> (+1overload)
Replaces the value inside an effect with a constant value.
Details
This function allows you to ignore the original value inside an effect and
replace it with a constant value.
When to Use
It is useful when you no longer need the value produced by an effect but want
to ensure that the effect completes successfully with a specific constant
result instead. For instance, you can replace the value produced by a
computation with a predefined value, ignoring what was calculated before.
@example
// Title: Replacing a Value
import { pipe, Effect } from"effect"
// Replaces the value 5 with the constant "new value"
Creates an Effect that always succeeds with a given value.
When to Use
Use this function when you need an effect that completes successfully with a
specific value without any errors or external dependencies.
@see ― fail to create an effect that represents a failure.
@example
// Title: Creating a Successful Effect
import { Effect } from"effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
constsuccess= Effect.succeed(42)
@since ― 2.0.0
succeed
43
}
44
)
45
46
// Generate a Standard Schema V1 object
47
const
constasyncStandardSchema:StandardSchemaV1<{
readonlyname:string;
}, {
readonlyname:string;
}>
asyncStandardSchema=
import Schema
Schema.
conststandardSchemaV1: <{
readonlyname:string;
}, {
readonlyname:string;
}>(schema:Schema.Schema<{
readonlyname:string;
}, {
readonlyname:string;
}, never>) =>StandardSchemaV1<{
readonlyname:string;
}, {
readonlyname:string;
}>
Returns a "Standard Schema" object conforming to the Standard Schema
v1 specification.
This function creates a schema whose validate method attempts to decode and
validate the provided input synchronously. If the underlying Schema
includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Any detected defects will be reported via a single issue containing no
path.
"message": "Expected a non empty string, actual \"\""
60
}
61
]
62
}
63
*/
Defects
If an unexpected defect occurs during validation, it is reported as a single issue without a path. This ensures that unexpected errors do not disrupt schema validation but are still captured and reported.
Example (Handling Defects)
1
import {
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect,
import Schema
Schema } from"effect"
2
3
// Define a schema with a defect in the decode function
Create a new Schema by transforming the input and output of an existing Schema
using the provided decoding functions.
@since ― 3.10.0
transformOrFail(
import Schema
Schema.
classString
exportString
@since ― 3.10.0
String,
import Schema
Schema.
classString
exportString
@since ― 3.10.0
String, {
5
// Simulate an internal failure
6
decode: () => Effect.Effect<never, never, never>
decode: () =>
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
constdie: (defect:unknown) =>Effect.Effect<never>
Creates an effect that terminates a fiber with a specified error.
Details
This function is used to signal a defect, which represents a critical and
unexpected error in the code. When invoked, it produces an effect that does
not handle the error and instead terminates the fiber.
The error channel of the resulting effect is of type never, indicating that
it cannot recover from this failure.
When to Use
Use this function when encountering unexpected conditions in your code that
should not be handled as regular errors but instead represent unrecoverable
defects.
@see ― dieSync for a variant that throws a specified error, evaluated
lazily.
@see ― dieMessage for a variant that throws a RuntimeException with a
message.
@example
// Title: Terminating on Division by Zero with a Specified Error
Returns a "Standard Schema" object conforming to the Standard Schema
v1 specification.
This function creates a schema whose validate method attempts to decode and
validate the provided input synchronously. If the underlying Schema
includes any asynchronous components (e.g., asynchronous message resolutions
or checks), then validation will necessarily return a Promise instead.
Any detected defects will be reported via a single issue containing no
path.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).