Overview
The Mod utility provides powerful methods for transforming and manipulating existing schemas.
Mod.partial()
Make all fields optional
const UserSchema = Interface({
id: "number",
name: "string",
email: "email"
});
const PartialUser = Mod.partial(UserSchema);
// All fields now optional
Mod.pick()
Select specific fields
const UserSchema = Interface({
id: "number",
name: "string",
email: "email",
password: "string"
});
const PublicUser = Mod.pick(UserSchema, ["id", "name", "email"]);
// Only id, name, email
Mod.omit()
Exclude specific fields
const UserSchema = Interface({
id: "number",
name: "string",
email: "email",
password: "string"
});
const SafeUser = Mod.omit(UserSchema, ["password"]);
// All fields except password
Mod.merge()
Combine multiple schemas
const BaseSchema = Interface({ id: "number", name: "string" });
const ExtendedSchema = Interface({ email: "email", age: "number?" });
const MergedSchema = Mod.merge(BaseSchema, ExtendedSchema);
// Contains all fields from both schemas
Mod.strict()
Reject additional properties
const UserSchema = Interface({ id: "number", name: "string" });
const StrictUser = Mod.strict(UserSchema);
// This will fail
StrictUser.parse({ id: 1, name: "John", extra: "field" });
// Error: Unexpected properties: extra
Mod.defaults()
Set default values
const ConfigSchema = Interface({
host: "string",
port: "number?",
timeout: "number?"
});
const ConfigWithDefaults = Mod.defaults(ConfigSchema, {
port: 3000,
timeout: 5000
});
// Only need to provide host
const config = ConfigWithDefaults.parse({ host: "localhost" });
// { host: "localhost", port: 3000, timeout: 5000 }