-1

The value I am testing with is: prefixa:rxcj7sdek1n7mbybiibxr4w09w

hasValidFormat keeps returning false when I expect it to return true.

export function isValidValue(value: unknown): value is Value {
  if (typeof value !== 'string') {
    return false;
  }

  const validPrefixes = Object.values(ValidPrefix);
  
  const hasValidPrefix = validPrefixes.some(prefix => value.startsWith(prefix));
  const hasValidFormat = /^[\w]+:[0-9a-f]{26}$/.test(value);

  // Add console logs for debugging
  console.log('the value is:', value);
  console.log('hasValidPrefix:', hasValidPrefix);
  console.log('hasValidFormat:', hasValidFormat);

  return hasValidPrefix && hasValidFormat;
}

// Valid Prefix
export enum ValidPrefix {
  PREFIXA = 'prefixa',
  PREFIXB = 'prefixa'
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
mystride
  • 75
  • 6
  • 4
    `[0-9a-f]{26}` won't match `rxcj7sdek1n7mbybiibxr4w09w`. You need to include letters from `a-z`. – Luatic Jul 06 '23 at 15:39
  • 3
    You apparently copied a regexp that was designed to only match hexadecimal numbers, not any letter. Don't just copy blindly. – Barmar Jul 06 '23 at 15:48

1 Answers1

0

You are limiting the regex to allow only the characters a,b,c,d,e and f. The value you are evaluating contains letters greater than f, it should be:

/^[\w]+:[0-9a-z]{26}$/
Barmar
  • 741,623
  • 53
  • 500
  • 612