-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: combiners inside of arrays (#53)
- Loading branch information
Showing
14 changed files
with
335 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"title": "Test", | ||
"type": "object", | ||
"properties": { | ||
"array-all-objects": { | ||
"type": "array", | ||
"items": { | ||
"allOf": [ | ||
{ | ||
"properties": { | ||
"foo": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
{ | ||
"properties": { | ||
"bar": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
], | ||
"type": "object" | ||
} | ||
} | ||
} | ||
} |
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
229 changes: 229 additions & 0 deletions
229
src/utils/__tests__/__snapshots__/renderSchema.spec.ts.snap
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { isCombiner } from '../isCombiner'; | ||
import { isCombinerNode } from '../nodes'; | ||
|
||
describe('isCombiner function', () => { | ||
describe('isCombinerNode function', () => { | ||
test('should return false if object without combiner is given', () => { | ||
expect(isCombiner({} as any)).toBe(false); | ||
expect(isCombiner({ properties: [] } as any)).toBe(false); | ||
expect(isCombinerNode({} as any)).toBe(false); | ||
expect(isCombinerNode({ properties: [] } as any)).toBe(false); | ||
}); | ||
|
||
test.each(['allOf', 'anyOf', 'oneOf'])('should return true if object with %s is given', combiner => { | ||
expect(isCombiner({ combiner } as any)).toBe(true); | ||
expect(isCombinerNode({ combiner } as any)).toBe(true); | ||
}); | ||
}); |
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,21 @@ | ||
import { JSONSchema4TypeName } from 'json-schema'; | ||
import { IArrayNode, JSONSchema4CombinerName } from '../types'; | ||
import { getCombiner } from './getCombiner'; | ||
import { inferType } from './inferType'; | ||
|
||
export function getArraySubtype( | ||
node: IArrayNode, | ||
): JSONSchema4TypeName | JSONSchema4TypeName[] | JSONSchema4CombinerName | string | undefined { | ||
if (!node.items || Array.isArray(node.items)) return; | ||
if ('$ref' in node.items) { | ||
return `$ref( ${node.items.$ref} )`; | ||
} | ||
|
||
const combiner = getCombiner(node.items); | ||
|
||
if (combiner !== undefined) { | ||
return combiner; | ||
} | ||
|
||
return inferType(node.items); | ||
} |
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,8 @@ | ||
import { JSONSchema4 } from 'json-schema'; | ||
import { JSONSchema4CombinerName } from '../types'; | ||
|
||
export const getCombiner = (node: JSONSchema4): JSONSchema4CombinerName | void => { | ||
if ('allOf' in node) return 'allOf'; | ||
if ('anyOf' in node) return 'anyOf'; | ||
if ('oneOf' in node) return 'oneOf'; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { ICombinerNode, SchemaNode } from '../types'; | ||
import { JSONSchema4CombinerName } from '../types'; | ||
|
||
export const isCombiner = (node: SchemaNode): node is ICombinerNode => 'combiner' in node; | ||
const combinerTypes = ['allOf', 'oneOf', 'anyOf']; | ||
|
||
export const isCombiner = (type: string): type is JSONSchema4CombinerName => combinerTypes.includes(type); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.