-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7edaab5
commit 274bea4
Showing
2 changed files
with
335 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,291 @@ | ||
var chai = require('chai') | ||
var merger = require('../../src') | ||
var sinon = require('sinon') | ||
var _ = require('lodash') | ||
var expect = chai.expect | ||
var Ajv = require('ajv').default | ||
|
||
var ajv = new Ajv() | ||
describe.only('if then else', function() { | ||
it('moves the if then else to the base schema if none there', () => { | ||
const result = merger({ | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result).to.eql({ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}) | ||
}) | ||
|
||
it('does NOT move the if then else to the base schema if something already there', () => { | ||
const result = merger({ | ||
if: { | ||
minimum: 5 | ||
}, | ||
then: { | ||
maximum: 2 | ||
}, | ||
else: { | ||
maximum: 10 | ||
}, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result).to.eql({ | ||
if: { | ||
minimum: 5 | ||
}, | ||
then: { | ||
maximum: 2 | ||
}, | ||
else: { | ||
maximum: 10 | ||
}, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
}) | ||
|
||
it('moves the unaffected keywords to the base schema', () => { | ||
const result = merger({ | ||
properties: { | ||
name: { | ||
type: 'string', | ||
minLength: 3 | ||
} | ||
}, | ||
if: { | ||
minimum: 5 | ||
}, | ||
then: { | ||
maximum: 2 | ||
}, | ||
else: { | ||
maximum: 10 | ||
}, | ||
allOf: [{ | ||
properties: { | ||
name: { | ||
type: 'string', | ||
minLength: 5 | ||
} | ||
}, | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result).to.eql({ | ||
properties: { | ||
name: { | ||
type: 'string', | ||
minLength: 5 | ||
} | ||
}, | ||
if: { | ||
minimum: 5 | ||
}, | ||
then: { | ||
maximum: 2 | ||
}, | ||
else: { | ||
maximum: 10 | ||
}, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
}) | ||
|
||
it('should not move to base schema if only some keywords are not present', () => { | ||
const result = merger({ | ||
else: false, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result).to.eql({ | ||
else: false, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
const result2 = merger({ | ||
then: false, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result2).to.eql({ | ||
then: false, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
const result3 = merger({ | ||
if: false, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result3).to.eql({ | ||
if: false, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
}) | ||
|
||
it('works with undefined value, it is as if not there. NOT the same as empty schema', () => { | ||
const result = merger({ | ||
if: undefined, | ||
then: undefined, | ||
else: undefined, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
|
||
expect(result).to.eql({ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}) | ||
}) | ||
|
||
it('removes empty allOf', () => { | ||
const result = merger({ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {}, | ||
allOf: [{ | ||
properties: { | ||
name: { | ||
type: 'string' | ||
} | ||
} | ||
}] | ||
}) | ||
|
||
expect(result).to.eql({ | ||
properties: { | ||
name: { | ||
type: 'string' | ||
} | ||
}, | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}) | ||
}) | ||
|
||
it('works with resolver that does not manage to resolve it\'s schemas', () => { | ||
const result = merger({ | ||
required: ['123'], | ||
if: {}, | ||
then: {}, | ||
else: {}, | ||
allOf: [{ | ||
required: ['234'], | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
|
||
}, { | ||
resolvers: { | ||
foo(values, paths, mergeSchemas, options, reportUnresolved) { | ||
var key = paths.pop() | ||
reportUnresolved(values.map((val) => { | ||
return { | ||
[key]: val | ||
} | ||
})) | ||
} | ||
} | ||
}) | ||
|
||
expect(result).to.eql({ | ||
required: ['123', '234'], | ||
if: {}, | ||
then: {}, | ||
else: {}, | ||
allOf: [{ | ||
if: { | ||
required: ['prop1'] | ||
}, | ||
then: {}, | ||
else: {} | ||
}] | ||
}) | ||
}) | ||
}) |