@@ -23,7 +23,10 @@ const internals = {
2323 }
2424 } ,
2525 dataUriRegex : / ^ d a t a : [ \w + . - ] + \/ [ \w + . - ] + ; ( ( c h a r s e t = [ \w - ] + | b a s e 6 4 ) , ) ? ( .* ) $ / ,
26- hexRegex : / ^ ( 0 x ) ? [ 0 - 9 a - f ] + $ / i,
26+ hexRegex : {
27+ withPrefix : / ^ ( 0 x ) ? [ 0 - 9 a - f ] + $ / i,
28+ withoutPrefix : / ^ [ 0 - 9 a - f ] + $ / i
29+ } ,
2730 ipRegex : ipRegex ( { cidr : 'forbidden' } ) . regex ,
2831 isoDurationRegex : / ^ P (? ! $ ) ( \d + Y ) ? ( \d + M ) ? ( \d + W ) ? ( \d + D ) ? ( T (? = \d ) ( \d + H ) ? ( \d + M ) ? ( \d + S ) ? ) ? $ / ,
2932
@@ -364,16 +367,18 @@ module.exports = Any.extend({
364367 hex : {
365368 method ( options = { } ) {
366369
367- Common . assertOptions ( options , [ 'byteAligned' ] ) ;
370+ Common . assertOptions ( options , [ 'byteAligned' , 'withPrefix' ] ) ;
368371
369- options = { byteAligned : false , ...options } ;
372+ options = { byteAligned : false , withPrefix : false , ...options } ;
370373 assert ( typeof options . byteAligned === 'boolean' , 'byteAligned must be boolean' ) ;
374+ assert ( typeof options . withPrefix === 'boolean' , 'withPrefix must be boolean' ) ;
371375
372376 return this . $_addRule ( { name : 'hex' , args : { options } } ) ;
373377 } ,
374378 validate ( value , helpers , { options } ) {
375379
376- if ( ! internals . hexRegex . test ( value ) ) {
380+ const re = options . withPrefix ? internals . hexRegex . withPrefix : internals . hexRegex . withoutPrefix ;
381+ if ( ! re . test ( value ) ) {
377382 return helpers . error ( 'string.hex' ) ;
378383 }
379384
0 commit comments