Patch Changes
-
#3166
15967cf
Thanks @gcanti! - AddfilterEffect
API, closes #3165The
filterEffect
function enhances thefilter
functionality by allowing the integration of effects, thus enabling asynchronous or dynamic validation scenarios. This is particularly useful when validations need to perform operations that require side effects, such as network requests or database queries.Example: Validating Usernames Asynchronously
import { Schema } from "@effect/schema"; import { Effect } from "effect"; async function validateUsername(username: string) { return Promise.resolve(username === "gcanti"); } const ValidUsername = Schema.String.pipe( Schema.filterEffect((username) => Effect.promise(() => validateUsername(username).then((valid) => valid || "Invalid username"), ), ), ).annotations({ identifier: "ValidUsername" }); Effect.runPromise(Schema.decodeUnknown(ValidUsername)("xxx")).then( console.log, ); /* ParseError: ValidUsername └─ Transformation process failure └─ Invalid username */
-
#3163
2328e17
Thanks @gcanti! - Addpick
andomit
static functions toStruct
interface, closes #3152.pick
The
pick
static function available in each struct schema can be used to create a newStruct
by selecting particular properties from an existingStruct
.import { Schema } from "@effect/schema"; const MyStruct = Schema.Struct({ a: Schema.String, b: Schema.Number, c: Schema.Boolean, }); // Schema.Struct<{ a: typeof Schema.String; c: typeof Schema.Boolean; }> const PickedSchema = MyStruct.pick("a", "c");
omit
The
omit
static function available in each struct schema can be used to create a newStruct
by excluding particular properties from an existingStruct
.import { Schema } from "@effect/schema"; const MyStruct = Schema.Struct({ a: Schema.String, b: Schema.Number, c: Schema.Boolean, }); // Schema.Struct<{ a: typeof Schema.String; c: typeof Schema.Boolean; }> const PickedSchema = MyStruct.omit("b");
-
Updated dependencies [
a5737d6
]:- effect@3.4.7