-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: interpret additionalItems (#257)
- Loading branch information
1 parent
f98e456
commit 21c8ca4
Showing
11 changed files
with
246 additions
and
11 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,19 @@ | ||
import { CommonModel } from 'models'; | ||
import { Schema } from 'models/Schema'; | ||
import { Interpreter, InterpreterOptions } from './Interpreter'; | ||
|
||
/** | ||
* Interpreter function for JSON Schema draft 7 additionalProperties keyword. | ||
* | ||
* @param schema | ||
* @param model | ||
* @param interpreter | ||
* @param interpreterOptions to control the interpret process | ||
*/ | ||
export default function interpretAdditionalItems(schema: Schema, model: CommonModel, interpreter : Interpreter, interpreterOptions: InterpreterOptions = Interpreter.defaultInterpreterOptions): void { | ||
if (model.type?.includes('array') === false) {return;} | ||
const additionalItemsModel = interpreter.interpret(schema.additionalItems === undefined ? true : schema.additionalItems, interpreterOptions); | ||
if (additionalItemsModel !== undefined) { | ||
model.addAdditionalItems(additionalItemsModel, 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
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,76 @@ | ||
/* eslint-disable no-undef */ | ||
import { CommonModel } from '../../../src/models/CommonModel'; | ||
import { Interpreter } from '../../../src/interpreter/Interpreter'; | ||
import interpretAdditionalItems from '../../../src/interpreter/InterpretAdditionalItems'; | ||
jest.mock('../../../src/interpreter/Interpreter'); | ||
jest.mock('../../../src/models/CommonModel'); | ||
describe('Interpretation of additionalItems', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('should try and interpret additionalItems schema', () => { | ||
const schema: any = { additionalItems: { type: 'string' } }; | ||
const model = new CommonModel(); | ||
model.type = 'array'; | ||
const interpreter = new Interpreter(); | ||
const mockedReturnModel = new CommonModel(); | ||
(interpreter.interpret as jest.Mock).mockReturnValue(mockedReturnModel); | ||
|
||
interpretAdditionalItems(schema, model, interpreter); | ||
|
||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, { type: 'string' }, Interpreter.defaultInterpreterOptions); | ||
expect(model.addAdditionalItems).toHaveBeenNthCalledWith(1, mockedReturnModel, schema); | ||
}); | ||
test('should ignore model if interpreter cannot interpret additionalItems schema', () => { | ||
const schema: any = { }; | ||
const model = new CommonModel(); | ||
model.type = 'array'; | ||
const interpreter = new Interpreter(); | ||
(interpreter.interpret as jest.Mock).mockReturnValue(undefined); | ||
|
||
interpretAdditionalItems(schema, model, interpreter); | ||
|
||
expect(model.addAdditionalItems).not.toHaveBeenCalled(); | ||
}); | ||
test('should be able to define additionalItems as false', () => { | ||
const schema: any = { additionalItems: false }; | ||
const model = new CommonModel(); | ||
model.type = 'array'; | ||
const interpreter = new Interpreter(); | ||
(interpreter.interpret as jest.Mock).mockReturnValue(undefined); | ||
|
||
interpretAdditionalItems(schema, model, interpreter); | ||
|
||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, false, Interpreter.defaultInterpreterOptions); | ||
expect(model.addAdditionalItems).not.toHaveBeenCalled(); | ||
}); | ||
test('should only work if model is array type', () => { | ||
const schema: any = { }; | ||
const model = new CommonModel(); | ||
model.type = 'string'; | ||
const interpreter = new Interpreter(); | ||
const mockedReturnModel = new CommonModel(); | ||
(interpreter.interpret as jest.Mock).mockReturnValue(mockedReturnModel); | ||
|
||
interpretAdditionalItems(schema, model, interpreter); | ||
|
||
expect(interpreter.interpret).not.toHaveBeenCalled(); | ||
expect(model.addAdditionalItems).not.toHaveBeenCalled(); | ||
}); | ||
test('should default to true', () => { | ||
const schema: any = { }; | ||
const model = new CommonModel(); | ||
model.type = 'array'; | ||
const interpreter = new Interpreter(); | ||
const mockedReturnModel = new CommonModel(); | ||
(interpreter.interpret as jest.Mock).mockReturnValue(mockedReturnModel); | ||
|
||
interpretAdditionalItems(schema, model, interpreter); | ||
|
||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, true, Interpreter.defaultInterpreterOptions); | ||
expect(model.addAdditionalItems).toHaveBeenNthCalledWith(1, mockedReturnModel, 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
Oops, something went wrong.