-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
Showing
7 changed files
with
134 additions
and
1 deletion.
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,19 @@ | ||
import { CommonModel } from 'models'; | ||
import { Schema } from 'models/Schema'; | ||
import { Interpreter } from './Interpreter'; | ||
import { isModelObject } from './Utils'; | ||
|
||
/** | ||
* Interpreter function for JSON Schema draft 7 additionalProperties keyword. | ||
* | ||
* @param schema | ||
* @param model | ||
* @param interpreter | ||
*/ | ||
export default function interpretAdditionalProperties(schema: Schema | boolean, model: CommonModel, interpreter : Interpreter) { | ||
if (typeof schema === 'boolean' || !isModelObject(model)) return; | ||
const newModels = interpreter.interpret(schema.additionalProperties || true); | ||
if (newModels.length > 0) { | ||
model.addAdditionalProperty(newModels[0], schema); | ||
} | ||
} |
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
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,61 @@ | ||
/* eslint-disable no-undef */ | ||
import { CommonModel } from '../../src/models/CommonModel'; | ||
import { Interpreter } from '../../src/interpreter/Interpreter'; | ||
import interpretAdditionalProperties from '../../src/interpreter/InterpretAdditionalProperties'; | ||
|
||
let mockedReturnModels = [new CommonModel()]; | ||
jest.mock('../../src/interpreter/Interpreter', () => { | ||
return { | ||
Interpreter: jest.fn().mockImplementation(() => { | ||
return { | ||
interpret: jest.fn().mockReturnValue(mockedReturnModels) | ||
}; | ||
}) | ||
}; | ||
}); | ||
jest.mock('../../src/models/CommonModel'); | ||
describe('Interpretation of additionalProperties', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockedReturnModels = [new CommonModel()]; | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('should try and interpret additionalProperties schema', () => { | ||
const schema: any = { additionalProperties: { type: 'string' } }; | ||
const model = new CommonModel(); | ||
model.type = "object"; | ||
const interpreter = new Interpreter(); | ||
interpretAdditionalProperties(schema, model, interpreter); | ||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, { type: 'string' }); | ||
expect(model.addAdditionalProperty).toHaveBeenNthCalledWith(1, mockedReturnModels[0], schema); | ||
}); | ||
test('should ignore model if interpreter cannot interpret additionalProperty schema', () => { | ||
const schema: any = { }; | ||
const model = new CommonModel(); | ||
model.type = "object"; | ||
const interpreter = new Interpreter(); | ||
mockedReturnModels.pop(); | ||
interpretAdditionalProperties(schema, model, interpreter); | ||
expect(model.addAdditionalProperty).not.toHaveBeenCalled(); | ||
}); | ||
test('should only work if model is object type', () => { | ||
const schema: any = { }; | ||
const model = new CommonModel(); | ||
model.type = "string"; | ||
const interpreter = new Interpreter(); | ||
interpretAdditionalProperties(schema, model, interpreter); | ||
expect(interpreter.interpret).not.toHaveBeenCalled(); | ||
expect(model.addAdditionalProperty).not.toHaveBeenCalled(); | ||
}); | ||
test('should default to true', () => { | ||
const schema: any = { }; | ||
const model = new CommonModel(); | ||
model.type = "object"; | ||
const interpreter = new Interpreter(); | ||
interpretAdditionalProperties(schema, model, interpreter); | ||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, true); | ||
expect(model.addAdditionalProperty).toHaveBeenNthCalledWith(1, mockedReturnModels[0], schema); | ||
}); | ||
}); |
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