caip-js icon indicating copy to clipboard operation
caip-js copied to clipboard

implement 'validation' specs like CAIP-3, 22, 29

Open shrugs opened this issue 4 years ago • 0 comments

How should 'validation' specs be implemented, ala CAIP-3, CAIP-22, CAIP-29?

Proposal

I'm imagining that we introduce more specific *Param types, plus a functional validation function that looks something like:

export interface CAIP3ChainIdParams extends ChainIDParams {
  namespace: "eip155",
}

export const isCAIP3ChainId = (params: ChainIdParams): params is CAIP3ChainIdParams => {
  if (params.namespace !== "eip155") return false;

  try {
    // must parse as decimal number
    parseInt(params.reference, 10);
  } catch {
    return false;
  }

  return true;
};

we could bring in a more extensible validation library like ajv, or write our own based on some spec we define (a ValidatorSpec?), but imo may not be worth it unless validation logic gets out of hand.

shrugs avatar May 06 '21 21:05 shrugs