Skip to content

Commit

Permalink
fix: add validation for '0x'-prefixed hex strings (#1147)
Browse files Browse the repository at this point in the history
* Added validation for '0x'-prefixed hex strings.

* Added 0h-prefix as an acceptable hexadecimal format.
  • Loading branch information
ayala-io authored and profnandaa committed Oct 13, 2019
1 parent 4162ae7 commit 1bf0094
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/isHexadecimal.js
Expand Up @@ -9,7 +9,7 @@ var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var hexadecimal = /^[0-9A-F]+$/i;
var hexadecimal = /^(0x)?[0-9A-F]+$/i;

function isHexadecimal(str) {
(0, _assertString.default)(str);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/isHexadecimal.js
@@ -1,6 +1,6 @@
import assertString from './util/assertString';

const hexadecimal = /^[0-9A-F]+$/i;
const hexadecimal = /^(0x|0h)?[0-9A-F]+$/i;

export default function isHexadecimal(str) {
assertString(str);
Expand Down
12 changes: 12 additions & 0 deletions test/validators.js
Expand Up @@ -2495,11 +2495,23 @@ describe('Validators', () => {
valid: [
'deadBEEF',
'ff0044',
'0xff0044',
'0XfF0044',
'0x0123456789abcDEF',
'0X0123456789abcDEF',
'0hfedCBA9876543210',
'0HfedCBA9876543210',
'0123456789abcDEF',
],
invalid: [
'abcdefg',
'',
'..',
'0xa2h',
'0xa20x',
'0x0123456789abcDEFq',
'0hfedCBA9876543210q',
'01234q56789abcDEF',
],
});
});
Expand Down

0 comments on commit 1bf0094

Please sign in to comment.