Default Constructors
Schema constructors operate on decoded types: they apply constructor defaults and type-side filters without running encoded-to-decoded conversions. Plain builders follow the call rules.
Structs and records
const Label = Schema.Struct({ text: Schema.NonEmptyString })
const LabelsById = Schema.Record(Schema.String, Schema.NonEmptyString)
effect function makeLabels() {
const label = try Label.makeEffect({ text: "Keep upright" })
const byId = try LabelsById.makeEffect({ handling: label.text })
return { label, byId }
}
// Run in an effect function:
// {
// const jsonValue = try makeLabels()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }{"label":{"text":"Keep upright"},"byId":{"handling":"Keep upright"}}Every schema exposes make, makeOption, and makeEffect. Each expects the decoded constructor input (for instance, FiniteFromString.makeEffect(6) takes a number, not "6") and validates all fields or record entries.
Filters and brands
const CaseCount = Schema.FiniteFromString.check(
Schema.isInt(), Schema.isBetween({ minimum: 1, maximum: 48 })
).pipe(Schema.brand("CaseCount"))
effect function makeCaseCount(value: number) {
return try CaseCount.makeEffect(value)
}
// Run in an effect function:
// try Console.log(try makeCaseCount(6))
// try Effect.catch(makeCaseCount(0), (issue) => Console.log(issue._tag))6
CompositeType-side checks run during construction, and brands attach statically without changing the runtime value. Passing { disableChecks: true } skips validation without proving the value conforms.
Constructor failures
effect function constructOrExplain(value: number) {
return effect {
return String(try CaseCount.makeEffect(value))
} catch (issue) => SchemaIssue.makeFormatterDefault()(issue)
}
function constructAsOption(value: number) {
return CaseCount.makeOption(value)
}
effect function constructWithExpectedFailure(value: number) {
return try CaseCount.makeEffect(value)
}
// Run in an effect function:
// try Console.log(try constructOrExplain(6))
// try Console.log(constructAsOption(0).isNone())
// try Effect.catch(constructWithExpectedFailure(0), (error) => Console.log("Construction rejected"))6
true
Construction rejectedmakeEffect: fails in the typed error channel with a rawSchemaIssue.Issue(Effect 4.1.1—not wrapped inSchemaError). Format it directly or map it to a domain error.makeOption: returnsOption.none()on schema failure, discarding issue details.make: throws a validationErrorwith the issue oncause.
Unexpected non-schema exceptions or asynchronous defaults in make / makeOption throw as defects; prefer makeEffect for expected validation failures.
Nested and lazy defaults
const Packing = Schema.Struct({
handling: Schema.String.pipe(Schema.withConstructorDefault(effect { return "standard" })),
dimensions: Schema.Struct({
unit: Schema.String.pipe(Schema.withConstructorDefault(effect { return "cm" })),
width: Schema.Finite
}).pipe(Schema.withConstructorDefault(effect { return { width: 10 } })),
labels: Schema.Array(Schema.String).pipe(Schema.withConstructorDefault(effect { return [] }))
})
effect function makePacking() {
return try Packing.makeEffect({})
}
// { handling: "standard", dimensions: { unit: "cm", width: 10 }, labels: [] }
// Run in an effect function:
// const first = try makePacking()
// const second = try makePacking()
// try Console.log(try Effect.try(() => JSON.stringify(first)))
// try Console.log(first.labels === second.labels){"handling":"standard","dimensions":{"unit":"cm","width":10},"labels":[]}
falseSchema.withConstructorDefault runs its Effect lazily only when the field is omitted—each call allocates a fresh [] for labels, and nested defaults fill missing inner fields after an outer default runs. Default Effects may fail with schema issues but cannot require services; pass service-derived values explicitly. Use makeEffect if any default is asynchronous.
Reusing defaults
const ReturnPacking = Schema.Struct({
reason: Schema.String,
handling: Packing.fields.handling,
labels: Packing.fields.labels
})
effect function makeReturnPacking() {
return try ReturnPacking.makeEffect({ reason: "inspection" })
}
// Run in an effect function:
// {
// const jsonValue = try makeReturnPacking()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }{"reason":"inspection","handling":"standard","labels":[]}Constructor defaults travel with field schemas when reused via .fields. A default annotation is documentation only, and constructor defaults never apply during decoding—add a decoding default separately when needed.
Schema-backed classes
class PackingRequest extends Schema.Class<PackingRequest>("PackingRequest")({
reference: Schema.String,
handling: Packing.fields.handling,
labels: Packing.fields.labels
}) {}
effect function createPackingRequest(reference: string) {
return try PackingRequest.makeEffect({ reference })
}
// Run in an effect function:
// const value = try createPackingRequest("pkg-94")
// try Console.log(value instanceof PackingRequest)
// try Console.log(try Effect.try(() => JSON.stringify(value)))true
{"reference":"pkg-94","handling":"standard","labels":[]}Classes reuse the same field defaults in both makeEffect and new. Inside effect code, wrap throwing new calls with Effect.try if validation failure is expected; new never executes an Effect on its own.
The matching Effect page: effect.website/docs/v4/schema/default-constructors