export const AddressSchema = Schema.String.pipe(
// Transform input to ensure 0x prefix
Schema.transform(Schema.String, {
strict: true,
decode: (s) => (s.startsWith("0x") ? s : `0x${s}`),
encode: (s) => s, // reverse transform (identity)
}),
// Validate hex pattern
Schema.pattern(/^0x[0-9a-fA-F]{40}$/),
// ensures future calls of `getAddress(s)` will certainly pass
// provided `s` has type `AddressSchema`
Schema.filter((s) => {
try {
ethers.getAddress(s); // throws if invalid
return true;
} catch {
return false;
}
}),
// Brand for nominal typing
Schema.brand("Address")
);
export const AddressSchema = Schema.String.pipe(
// Transform input to ensure 0x prefix
Schema.transform(Schema.String, {
strict: true,
decode: (s) => (s.startsWith("0x") ? s : `0x${s}`),
encode: (s) => s, // reverse transform (identity)
}),
// Validate hex pattern
Schema.pattern(/^0x[0-9a-fA-F]{40}$/),
// ensures future calls of `getAddress(s)` will certainly pass
// provided `s` has type `AddressSchema`
Schema.filter((s) => {
try {
ethers.getAddress(s); // throws if invalid
return true;
} catch {
return false;
}
}),
// Brand for nominal typing
Schema.brand("Address")
);