@@ -25,40 +25,48 @@ const buildKeyRegex = (keyPattern) => {
2525 }
2626} ;
2727
28+ const normalizeKeyPattern = ( keyPattern ) => {
29+ if ( keyPattern instanceof RegExp ) {
30+ return keyPattern . source ;
31+ }
32+ return keyPattern ;
33+ } ;
34+
2835/**
2936 * Creates a Git trailer schema bundle with customizable validation rules.
3037 * @param {Object } options - Configuration options
31- * @param {string } options.keyPattern - Regex pattern string for key validation (will be anchored)
38+ * @param {string|RegExp } options.keyPattern - Regex pattern string or RegExp for key validation (will be anchored)
3239 * @param {number } options.keyMaxLength - Maximum length for trailer keys
3340 * @returns {{ schema: z.ZodObject, keyPattern: string, keyRegex: RegExp } }
3441 */
3542export function createGitTrailerSchemaBundle ( { keyPattern = DEFAULT_KEY_PATTERN , keyMaxLength = 100 } = { } ) {
36- if ( typeof keyPattern !== 'string' || keyPattern . length === 0 ) {
37- throw new TypeError ( 'keyPattern must be a non-empty string' ) ;
43+ const normalizedKeyPattern = normalizeKeyPattern ( keyPattern ) ;
44+ if ( typeof normalizedKeyPattern !== 'string' || normalizedKeyPattern . length === 0 ) {
45+ throw new TypeError ( 'keyPattern must be a non-empty string or RegExp' ) ;
3846 }
3947 if ( ! Number . isInteger ( keyMaxLength ) || keyMaxLength <= 0 ) {
4048 throw new TypeError ( 'keyMaxLength must be a positive integer' ) ;
4149 }
4250
43- const cacheKey = `${ keyPattern } ::${ keyMaxLength } ` ;
51+ const cacheKey = `${ normalizedKeyPattern } ::${ keyMaxLength } ` ;
4452 if ( bundleCache . has ( cacheKey ) ) {
4553 return bundleCache . get ( cacheKey ) ;
4654 }
4755
48- const keyRegex = buildKeyRegex ( keyPattern ) ;
56+ const keyRegex = buildKeyRegex ( normalizedKeyPattern ) ;
4957 const bundle = {
5058 schema : z . object ( {
5159 key : z
5260 . string ( )
5361 . min ( 1 )
5462 . max ( keyMaxLength , 'Trailer key must not exceed character limit' )
55- . regex ( keyRegex , `Trailer key must match the required pattern ${ keyPattern } ` ) ,
63+ . regex ( keyRegex , `Trailer key must match the required pattern ${ normalizedKeyPattern } ` ) ,
5664 value : z
5765 . string ( )
5866 . min ( 1 )
5967 . regex ( / ^ [ ^ \r \n ] + $ / , 'Trailer values cannot contain line breaks' ) ,
6068 } ) ,
61- keyPattern,
69+ keyPattern : normalizedKeyPattern ,
6270 keyRegex,
6371 } ;
6472
0 commit comments