Skip to content

Commit

Permalink
Merge pull request #2 from qzb/merge-patterns
Browse files Browse the repository at this point in the history
Add support for merging patterns
  • Loading branch information
mokkabonna authored Apr 24, 2019
2 parents c494107 + f367dc2 commit a014ab3
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 37 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 2 additions & 10 deletions test/specs/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,11 +777,7 @@ describe('module', function() {
properties: {
name: {
type: 'string',
allOf: [{
pattern: 'bar'
}, {
pattern: 'foo'
}]
pattern: '(?=bar)(?=foo)'
}
}
}
Expand All @@ -798,11 +794,7 @@ describe('module', function() {
})

expect(result).to.eql({
allOf: [{
pattern: 'fdsaf'
}, {
pattern: 'abba'
}]
pattern: '(?=fdsaf)(?=abba)'
})

var result2 = merger({
Expand Down
6 changes: 1 addition & 5 deletions test/specs/items.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ describe('items', function() {
properties: {
name: {
type: 'string',
allOf: [{
pattern: 'bar'
}, {
pattern: 'foo'
}]
pattern: '(?=bar)(?=foo)'
}
}
}
Expand Down

0 comments on commit a014ab3

Please sign in to comment.