-
-
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.
refactor: const interpretation (#200)
- Loading branch information
1 parent
0db6ea0
commit 92185d7
Showing
5 changed files
with
90 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,25 @@ | ||
|
||
import { CommonModel } from '../models/CommonModel'; | ||
import { Schema } from '../models/Schema'; | ||
import { inferTypeFromValue } from './Utils'; | ||
|
||
/** | ||
* Interpreter function for JSON Schema draft 7 const keyword. | ||
* | ||
* @param schema | ||
* @param model | ||
*/ | ||
export default function interpretConst(schema: Schema | boolean, model: CommonModel) { | ||
if (typeof schema === 'boolean' || schema.const === undefined) return; | ||
|
||
const schemaConst = schema.const; | ||
model.enum = [schemaConst]; | ||
|
||
//If schema does not contain type interpret the schema | ||
if (schema.type === undefined) { | ||
const inferredType = inferTypeFromValue(schemaConst); | ||
if (inferredType !== undefined) { | ||
model.setType(inferredType); | ||
} | ||
} | ||
} |
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,52 @@ | ||
|
||
import { CommonModel } from '../../src/models/CommonModel'; | ||
import interpretConst from '../../src/interpreter/InterpretConst'; | ||
import {inferTypeFromValue} from '../../src/interpreter/Utils'; | ||
jest.mock('../../src/interpreter/Utils'); | ||
describe('Interpretation of const', function() { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('should not do anything for boolean schemas', function() { | ||
const model = new CommonModel(); | ||
const schema: any = true; | ||
interpretConst(schema, model); | ||
expect(model.type).toBeUndefined(); | ||
expect(model.enum).toBeUndefined(); | ||
}); | ||
test('should not do anything if schema does not contain const', function() { | ||
const model = new CommonModel(); | ||
const schema: any = { type: 'string'}; | ||
interpretConst(schema, model); | ||
expect(model.type).toBeUndefined(); | ||
expect(model.enum).toBeUndefined(); | ||
}); | ||
test('should not infer type from const if schema have type', function() { | ||
const model = new CommonModel(); | ||
const schema: any = { type: 'string', const: 'test'}; | ||
interpretConst(schema, model); | ||
expect(model.type).toBeUndefined(); | ||
expect(model.enum).toEqual([schema.const]); | ||
}); | ||
test('should infer type and enum', function() { | ||
(inferTypeFromValue as jest.Mock).mockReturnValue("string"); | ||
const schema: any = { const: 'test'}; | ||
const model = new CommonModel(); | ||
interpretConst(schema, model); | ||
expect(model.enum).toEqual([schema.const]); | ||
expect(inferTypeFromValue).toHaveBeenNthCalledWith(1, 'test'); | ||
expect(model.type).toEqual("string"); | ||
}); | ||
test('should not infer unknown type', function() { | ||
(inferTypeFromValue as jest.Mock).mockReturnValue(undefined); | ||
const schema: any = { const: 'test'}; | ||
const model = new CommonModel(); | ||
interpretConst(schema, model); | ||
expect(model.enum).toEqual([schema.const]); | ||
expect(model.type).toBeUndefined(); | ||
expect(inferTypeFromValue).toHaveBeenNthCalledWith(1, 'test'); | ||
}); | ||
}); |