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

Prefix Validation on fromString

Open itizarsa opened this issue 11 months ago • 4 comments

In the fromString method, if the prefix is omitted, the typeid passed with a random prefix succeeds, but it should fail I think.

For it to fail, we need to change the below condition

Current

if (prefix && p !== prefix) {
    throw new PrefixMismatchError(prefix, p);
}

Suggested

if (p && p !== prefix) {
    throw new PrefixMismatchError(prefix, p);
}

itizarsa avatar Mar 25 '25 04:03 itizarsa

Reproduction

const tid = typeidUnboxed() // 01jq5r369mfezvr17bcf8ahztt

const fromTid =  typeidUnboxed("ran_01jq5r369mfezvr17bcf8ahztt")

itizarsa avatar Mar 25 '25 04:03 itizarsa

@itizarsa good find! Do you have a PR for this somewhere? It's ok if there isn't. I can apply the changes

LucilleH avatar Apr 10 '25 18:04 LucilleH

I'll raise one

itizarsa avatar Apr 11 '25 01:04 itizarsa

@LucilleH If we make that change, other tests are failing. Need to investigate more.

  1. We need to define the error message.
  2. In most places, the fromString is used without providing a prefix, so it shouldn't break the existing usages.

If you take a look at this, use the below test to reproduce

    it("should throw an error for TypeId string with prefix when there should be none", () => {
      const prefix = "prefix";
      const str = `${prefix}_00041061050r3gg28a1c60t3gf`;

      expect(() => {
        fromString(str);
      }).toThrowError(new PrefixMismatchError("NONE", prefix));
    });

itizarsa avatar Apr 11 '25 03:04 itizarsa