From 223217aea62183d90b1d6f9538cfa83c1744170d Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Wed, 17 Jun 2026 18:50:53 +0200 Subject: [PATCH] fix: expose multiple string patterns in json schemas --- lib/types/string.js | 14 +++++++++++++- test/json-schema.js | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/types/string.js b/lib/types/string.js index 56dfe620..48303763 100755 --- a/lib/types/string.js +++ b/lib/types/string.js @@ -693,7 +693,19 @@ module.exports = Any.extend({ }, jsonSchema(rule, res) { - res.pattern = rule.args.regex.source; + const pattern = rule.args.regex.source; + + if (res.allOf) { + res.allOf.push({ pattern }); + } + else if (res.pattern !== undefined) { + res.allOf = [{ pattern: res.pattern }, { pattern }]; + delete res.pattern; + } + else { + res.pattern = pattern; + } + return res; }, args: ['regex', 'options'], diff --git a/test/json-schema.js b/test/json-schema.js index fe070c68..a4fdd1c2 100644 --- a/test/json-schema.js +++ b/test/json-schema.js @@ -1623,6 +1623,28 @@ describe('jsonSchema', () => { }); }); + it('composes multiple patterns via allOf', () => { + + Helper.validateJsonSchema(Joi.string().pattern(/^\d+$/).pattern(/.{4,}/), { + type: 'string', + minLength: 1, + allOf: [ + { pattern: '^\\d+$' }, + { pattern: '.{4,}' } + ] + }); + + Helper.validateJsonSchema(Joi.string().pattern(/^a/).pattern(/b/).pattern(/c$/), { + type: 'string', + minLength: 1, + allOf: [ + { pattern: '^a' }, + { pattern: 'b' }, + { pattern: 'c$' } + ] + }); + }); + it('represents string with valids', () => { Helper.validateJsonSchema(Joi.string().valid('a', 'b'), {