From f367dc27b7b4e47fa679cf781706b178f9019e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Soko=C5=82owski?= Date: Mon, 25 Mar 2019 23:02:30 +0100 Subject: [PATCH] Add support for merging patterns --- README.md | 17 ----------------- src/index.js | 7 ++----- test/specs/index.spec.js | 12 ++---------- test/specs/items.spec.js | 6 +----- 4 files changed, 5 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 7bb68de..9f60609 100644 --- a/README.md +++ b/README.md @@ -69,23 +69,6 @@ As you can see above the strategy is to choose the **most** restrictive of the s What you are left with is a schema completely free of allOf. Except for in a couple of values that are impossible to properly intersect/combine: -### pattern - -If a schema have the pattern keyword and we have a conflict, then we need to leave that expressed like this: - -```js -{ - type: 'string', - allOf: [{ - pattern: '\\w+\\s\\w+' - }, { - pattern: '123$' - }] -} -``` - -Regular expressions does not have an AND operator, only OR. - ### not When multiple conflicting **not** values are found, we also use the approach that pattern use, but instead of allOf we use anyOf. When extraction of common rules from anyOf is in place this can be further simplified. diff --git a/src/index.js b/src/index.js index fc8cb82..fb1b82c 100644 --- a/src/index.js +++ b/src/index.js @@ -393,11 +393,8 @@ var defaultResolvers = { not(compacted) { return {anyOf: compacted} }, - pattern(compacted, paths, mergeSchemas, options, reportUnresolved) { - var key = paths.pop() - reportUnresolved(compacted.map(function(regexp) { - return {[key]: regexp} - })) + pattern(compacted) { + return compacted.map(r => '(?=' + r + ')').join('') }, multipleOf(compacted) { var integers = compacted.slice(0) diff --git a/test/specs/index.spec.js b/test/specs/index.spec.js index 4f0da89..1a67dc8 100644 --- a/test/specs/index.spec.js +++ b/test/specs/index.spec.js @@ -777,11 +777,7 @@ describe('module', function() { properties: { name: { type: 'string', - allOf: [{ - pattern: 'bar' - }, { - pattern: 'foo' - }] + pattern: '(?=bar)(?=foo)' } } } @@ -798,11 +794,7 @@ describe('module', function() { }) expect(result).to.eql({ - allOf: [{ - pattern: 'fdsaf' - }, { - pattern: 'abba' - }] + pattern: '(?=fdsaf)(?=abba)' }) var result2 = merger({ diff --git a/test/specs/items.spec.js b/test/specs/items.spec.js index 4cfa8d3..81c9f01 100644 --- a/test/specs/items.spec.js +++ b/test/specs/items.spec.js @@ -40,11 +40,7 @@ describe('items', function() { properties: { name: { type: 'string', - allOf: [{ - pattern: 'bar' - }, { - pattern: 'foo' - }] + pattern: '(?=bar)(?=foo)' } } }