typeid-js
typeid-js copied to clipboard
Prefix Validation on fromString
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);
}
Reproduction
const tid = typeidUnboxed() // 01jq5r369mfezvr17bcf8ahztt
const fromTid = typeidUnboxed("ran_01jq5r369mfezvr17bcf8ahztt")
@itizarsa good find! Do you have a PR for this somewhere? It's ok if there isn't. I can apply the changes
I'll raise one
@LucilleH If we make that change, other tests are failing. Need to investigate more.
- We need to define the error message.
- 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));
});