-
-
Notifications
You must be signed in to change notification settings - Fork 165
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
179 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,38 @@ | ||
|
||
import { CommonModel } from '../models/CommonModel'; | ||
import { Schema } from '../models/Schema'; | ||
import { Interpreter } from './Interpreter'; | ||
|
||
/** | ||
* Interpreter function for JSON Schema draft 7 items keyword. | ||
* | ||
* @param schema | ||
* @param model | ||
* @param interpreter | ||
*/ | ||
export default function interpretItems(schema: Schema | boolean, model: CommonModel, interpreter : Interpreter) { | ||
if (typeof schema === 'boolean' || schema.items === undefined) return; | ||
model.addTypes('array'); | ||
interpretArrayItems(schema, schema.items, model, interpreter); | ||
} | ||
|
||
/** | ||
* Internal function to process all item schemas | ||
* | ||
* @param rootSchema | ||
* @param itemSchemas | ||
* @param model | ||
* @param interpreter | ||
*/ | ||
function interpretArrayItems(rootSchema: Schema, itemSchemas: (Schema | boolean)[] | (Schema | boolean), model: CommonModel, interpreter : Interpreter) { | ||
if (Array.isArray(itemSchemas)) { | ||
for (const itemSchema of itemSchemas) { | ||
interpretArrayItems(rootSchema, itemSchema, model, interpreter); | ||
} | ||
} else { | ||
const itemModels = interpreter.interpret(itemSchemas); | ||
if (itemModels.length > 0) { | ||
model.addItem(itemModels[0], rootSchema); | ||
} | ||
} | ||
} |
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,86 @@ | ||
/* eslint-disable no-undef */ | ||
import { CommonModel } from '../../src/models/CommonModel'; | ||
import { Interpreter } from '../../src/interpreter/Interpreter'; | ||
import interpretItems from '../../src/interpreter/InterpretItems'; | ||
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', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockedReturnModels = [new CommonModel()]; | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}) | ||
test('should not do anything if schema does not contain items', function() { | ||
const schema = {}; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretItems(schema, model, interpreter); | ||
expect(model.type).toBeUndefined(); | ||
expect(model.addItem).not.toHaveBeenCalled(); | ||
}); | ||
test('should not do anything if schema is boolean', function() { | ||
const schema = true; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretItems(schema, model, interpreter); | ||
expect(model.type).toBeUndefined(); | ||
expect(model.addItem).not.toHaveBeenCalled(); | ||
expect(JSON.stringify(model)).toEqual(JSON.stringify(new CommonModel())); | ||
}); | ||
|
||
test('should ignore model if interpreter cannot interpret property schema', () => { | ||
const schema: any = { items: { type: 'string' } }; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
mockedReturnModels.pop(); | ||
interpretItems(schema, model, interpreter); | ||
expect(model.type).toBeUndefined(); | ||
expect(model.addItem).not.toHaveBeenCalled(); | ||
}); | ||
describe('single item schemas', () => { | ||
test('should set items', () => { | ||
const schema: any = { items: { type: 'string' } }; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretItems(schema, model, interpreter); | ||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, { type: 'string' }); | ||
expect(model.addItem).toHaveBeenNthCalledWith(1, mockedReturnModels[0], schema); | ||
}); | ||
test('should infer type of model', () => { | ||
const schema: any = { items: { type: 'string' } }; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretItems(schema, model, interpreter); | ||
expect(model.addTypes).toHaveBeenNthCalledWith(1, 'array'); | ||
}); | ||
}); | ||
describe('multiple item schemas', () => { | ||
test('should set items', () => { | ||
const schema: any = { items: [{ type: 'string' }, { type: 'number' }] }; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretItems(schema, model, interpreter); | ||
expect(interpreter.interpret).toHaveBeenNthCalledWith(1, { type: 'string' }); | ||
expect(interpreter.interpret).toHaveBeenNthCalledWith(2, { type: 'number' }); | ||
expect(model.addItem).toHaveBeenNthCalledWith(1, mockedReturnModels[0], schema); | ||
}); | ||
test('should infer type of model', () => { | ||
const schema: any = { items: [{ type: 'string' }, { type: 'number' }] }; | ||
const model = new CommonModel(); | ||
const interpreter = new Interpreter(); | ||
interpretItems(schema, model, interpreter); | ||
expect(model.addTypes).toHaveBeenNthCalledWith(1, 'array'); | ||
}); | ||
}); | ||
}); |
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