Refining Address Validation Pipeline in Typescript

can this validation pipeline be refined ?
is is an issue to encapsulate all validation in schema definition ?
am i missing some validation by relying on the below definition alone ?
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")
);
Was this page helpful?