From b14f4a90b0aca31c3a9bd68c954226a6e5ea68d6 Mon Sep 17 00:00:00 2001 From: Shishir Date: Tue, 16 Feb 2021 01:55:51 -0800 Subject: [PATCH 1/9] wip: store srcFileName for exportable types --- package.json | 1 + src/NodeParser/EnumNodeParser.ts | 3 ++- src/NodeParser/InterfaceAndClassNodeParser.ts | 2 +- src/NodeParser/MappedTypeNodeParser.ts | 9 +++++---- src/NodeParser/ObjectLiteralExpressionNodeParser.ts | 2 +- src/NodeParser/ObjectTypeNodeParser.ts | 2 +- src/NodeParser/TypeAliasNodeParser.ts | 2 +- src/NodeParser/TypeLiteralNodeParser.ts | 8 +++++++- src/NodeParser/TypeofNodeParser.ts | 2 +- src/SchemaGenerator.ts | 1 + src/Type/AliasType.ts | 6 +++++- src/Type/EnumType.ts | 6 +++++- src/Type/ObjectType.ts | 6 +++++- src/Type/ReferenceType.ts | 9 +++++++++ src/Utils/isAssignableTo.ts | 2 +- test/valid-data/duplicates-composition/componentA.ts | 3 +++ test/valid-data/duplicates-composition/componentB.ts | 3 +++ test/valid-data/duplicates-composition/main.ts | 7 +++++++ 18 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 test/valid-data/duplicates-composition/componentA.ts create mode 100644 test/valid-data/duplicates-composition/componentB.ts create mode 100644 test/valid-data/duplicates-composition/main.ts diff --git a/package.json b/package.json index 3859092cc..6bab34ecc 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "test": "jest test/ --verbose", "test:fast": "FAST_TEST=1 jest test/ --verbose", "debug": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts", + "dbg": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts -p test/valid-data/duplicates-composition/main.ts -t MyObject", "run": "ts-node ts-json-schema-generator.ts" }, "release": { diff --git a/src/NodeParser/EnumNodeParser.ts b/src/NodeParser/EnumNodeParser.ts index 3e9d2dac5..622950a78 100644 --- a/src/NodeParser/EnumNodeParser.ts +++ b/src/NodeParser/EnumNodeParser.ts @@ -19,7 +19,8 @@ export class EnumNodeParser implements SubNodeParser { `enum-${getKey(node, context)}`, members .filter((member: ts.EnumMember) => !isNodeHidden(member)) - .map((member, index) => this.getMemberValue(member, index)) + .map((member, index) => this.getMemberValue(member, index)), + node.getSourceFile().fileName ); } diff --git a/src/NodeParser/InterfaceAndClassNodeParser.ts b/src/NodeParser/InterfaceAndClassNodeParser.ts index 0da128469..50a9bebc1 100644 --- a/src/NodeParser/InterfaceAndClassNodeParser.ts +++ b/src/NodeParser/InterfaceAndClassNodeParser.ts @@ -64,7 +64,7 @@ export class InterfaceAndClassNodeParser implements SubNodeParser { } } - return new ObjectType(id, this.getBaseTypes(node, context), properties, additionalProperties); + return new ObjectType(id, this.getBaseTypes(node, context), properties, additionalProperties, node.getSourceFile().fileName); } /** diff --git a/src/NodeParser/MappedTypeNodeParser.ts b/src/NodeParser/MappedTypeNodeParser.ts index 1251e0fdb..e99038fce 100644 --- a/src/NodeParser/MappedTypeNodeParser.ts +++ b/src/NodeParser/MappedTypeNodeParser.ts @@ -35,20 +35,21 @@ export class MappedTypeNodeParser implements SubNodeParser { id, [], this.getProperties(node, keyListType, context), - this.getAdditionalProperties(node, keyListType, context) + this.getAdditionalProperties(node, keyListType, context), + node.getSourceFile().fileName ); } else if (keyListType instanceof LiteralType) { // Key type resolves to single known property - return new ObjectType(id, [], this.getProperties(node, new UnionType([keyListType]), context), false); + return new ObjectType(id, [], this.getProperties(node, new UnionType([keyListType]), context), false, node.getSourceFile().fileName); } else if (keyListType instanceof StringType || keyListType instanceof SymbolType) { // Key type widens to `string` const type = this.childNodeParser.createType(node.type!, context); - return type === undefined ? undefined : new ObjectType(id, [], [], type); + return type === undefined ? undefined : new ObjectType(id, [], [], type, node.getSourceFile().fileName); } else if (keyListType instanceof NumberType) { const type = this.childNodeParser.createType(node.type!, this.createSubContext(node, keyListType, context)); return type === undefined ? undefined : new ArrayType(type); } else if (keyListType instanceof EnumType) { - return new ObjectType(id, [], this.getValues(node, keyListType, context), false); + return new ObjectType(id, [], this.getValues(node, keyListType, context), false, node.getSourceFile().fileName); } else { throw new LogicError( // eslint-disable-next-line max-len diff --git a/src/NodeParser/ObjectLiteralExpressionNodeParser.ts b/src/NodeParser/ObjectLiteralExpressionNodeParser.ts index b62d9951c..df54726ea 100644 --- a/src/NodeParser/ObjectLiteralExpressionNodeParser.ts +++ b/src/NodeParser/ObjectLiteralExpressionNodeParser.ts @@ -24,7 +24,7 @@ export class ObjectLiteralExpressionNodeParser implements SubNodeParser { ) ); - return new ObjectType(`object-${getKey(node, context)}`, [], properties, false); + return new ObjectType(`object-${getKey(node, context)}`, [], properties, false, node.getSourceFile().fileName); } // TODO: implement this? diff --git a/src/NodeParser/ObjectTypeNodeParser.ts b/src/NodeParser/ObjectTypeNodeParser.ts index 1621043d0..b774ee257 100644 --- a/src/NodeParser/ObjectTypeNodeParser.ts +++ b/src/NodeParser/ObjectTypeNodeParser.ts @@ -11,6 +11,6 @@ export class ObjectTypeNodeParser implements SubNodeParser { } public createType(node: ts.KeywordTypeNode, context: Context): BaseType { - return new ObjectType(`object-${getKey(node, context)}`, [], [], true); + return new ObjectType(`object-${getKey(node, context)}`, [], [], true, node.getSourceFile().fileName); } } diff --git a/src/NodeParser/TypeAliasNodeParser.ts b/src/NodeParser/TypeAliasNodeParser.ts index b99e4a788..4821d6a9d 100644 --- a/src/NodeParser/TypeAliasNodeParser.ts +++ b/src/NodeParser/TypeAliasNodeParser.ts @@ -41,7 +41,7 @@ export class TypeAliasNodeParser implements SubNodeParser { if (type === undefined) { return undefined; } - return new AliasType(id, type); + return new AliasType(id, type, node.getSourceFile().fileName); } private getTypeId(node: ts.TypeAliasDeclaration, context: Context): string { diff --git a/src/NodeParser/TypeLiteralNodeParser.ts b/src/NodeParser/TypeLiteralNodeParser.ts index 1bfcd8f9c..ac0e75fd5 100644 --- a/src/NodeParser/TypeLiteralNodeParser.ts +++ b/src/NodeParser/TypeLiteralNodeParser.ts @@ -26,7 +26,13 @@ export class TypeLiteralNodeParser implements SubNodeParser { return undefined; } - return new ObjectType(id, [], properties, this.getAdditionalProperties(node, context)); + return new ObjectType( + id, + [], + properties, + this.getAdditionalProperties(node, context), + node.getSourceFile().fileName + ); } private getProperties(node: ts.TypeLiteralNode, context: Context): ObjectProperty[] | undefined { diff --git a/src/NodeParser/TypeofNodeParser.ts b/src/NodeParser/TypeofNodeParser.ts index f341257e8..b9730b339 100644 --- a/src/NodeParser/TypeofNodeParser.ts +++ b/src/NodeParser/TypeofNodeParser.ts @@ -61,6 +61,6 @@ export class TypeofNodeParser implements SubNodeParser { return new ObjectProperty(name, type, true); }); - return new ObjectType(id, [], properties, false); + return new ObjectType(id, [], properties, false, node.getSourceFile().fileName); } } diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index 91c01d176..9a80b2e51 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -104,6 +104,7 @@ export class SchemaGenerator { const childId = child.getId().replace(/def-/g, ""); if (previousId && childId !== previousId) { + throw new Error(`Type "${name}" has multiple definitions.`); } ids.set(name, childId); diff --git a/src/Type/AliasType.ts b/src/Type/AliasType.ts index 50f62bd12..a27c1d198 100644 --- a/src/Type/AliasType.ts +++ b/src/Type/AliasType.ts @@ -1,7 +1,7 @@ import { BaseType } from "./BaseType"; export class AliasType extends BaseType { - public constructor(private id: string, private type: BaseType) { + public constructor(private id: string, private type: BaseType, private srcFileName: string) { super(); } @@ -12,4 +12,8 @@ export class AliasType extends BaseType { public getType(): BaseType { return this.type; } + + public getSrcFileName(): string { + return this.srcFileName; + } } diff --git a/src/Type/EnumType.ts b/src/Type/EnumType.ts index 242d7fb05..912d34270 100644 --- a/src/Type/EnumType.ts +++ b/src/Type/EnumType.ts @@ -7,7 +7,7 @@ export type EnumValue = string | boolean | number | null; export class EnumType extends BaseType { private types: BaseType[]; - public constructor(private id: string, private values: readonly EnumValue[]) { + public constructor(private id: string, private values: readonly EnumValue[], private srcFileName: string) { super(); this.types = values.map((value) => (value == null ? new NullType() : new LiteralType(value))); } @@ -23,4 +23,8 @@ export class EnumType extends BaseType { public getTypes(): BaseType[] { return this.types; } + + public getSrcFileName(): string { + return this.srcFileName; + } } diff --git a/src/Type/ObjectType.ts b/src/Type/ObjectType.ts index 4c581885f..636ecba0f 100644 --- a/src/Type/ObjectType.ts +++ b/src/Type/ObjectType.ts @@ -20,7 +20,8 @@ export class ObjectType extends BaseType { private id: string, private baseTypes: readonly BaseType[], private properties: readonly ObjectProperty[], - private additionalProperties: BaseType | boolean + private additionalProperties: BaseType | boolean, + private srcFileName: string ) { super(); } @@ -38,4 +39,7 @@ export class ObjectType extends BaseType { public getAdditionalProperties(): BaseType | boolean { return this.additionalProperties; } + public getSrcFileName(): string { + return this.srcFileName; + } } diff --git a/src/Type/ReferenceType.ts b/src/Type/ReferenceType.ts index 4d0cc5496..67f1e181e 100644 --- a/src/Type/ReferenceType.ts +++ b/src/Type/ReferenceType.ts @@ -7,6 +7,8 @@ export class ReferenceType extends BaseType { private name: string | null = null; + private srcFileName: string | null = null; + public getId(): string { if (this.id == null) { throw new Error("Reference type ID not set yet"); @@ -41,4 +43,11 @@ export class ReferenceType extends BaseType { this.setId(type.getId()); this.setName(type.getName()); } + + public getSrcFileName(): string { + if (!this.srcFileName) { + throw new Error("Reference srcFileName not set yet"); + } + return this.srcFileName; + } } diff --git a/src/Utils/isAssignableTo.ts b/src/Utils/isAssignableTo.ts index a8d7fa949..3a8fd5c1a 100644 --- a/src/Utils/isAssignableTo.ts +++ b/src/Utils/isAssignableTo.ts @@ -37,7 +37,7 @@ function combineIntersectingTypes(intersection: IntersectionType): BaseType[] { if (objectTypes.length === 1) { combined.push(objectTypes[0]); } else if (objectTypes.length > 1) { - combined.push(new ObjectType(`combined-objects-${intersection.getId()}`, objectTypes, [], false)); + combined.push(new ObjectType(`combined-objects-${intersection.getId()}`, objectTypes, [], false, `non-terminal-intersection`)); } return combined; } diff --git a/test/valid-data/duplicates-composition/componentA.ts b/test/valid-data/duplicates-composition/componentA.ts new file mode 100644 index 000000000..bd5ff59fb --- /dev/null +++ b/test/valid-data/duplicates-composition/componentA.ts @@ -0,0 +1,3 @@ +export interface MyObject { + a: string; +} diff --git a/test/valid-data/duplicates-composition/componentB.ts b/test/valid-data/duplicates-composition/componentB.ts new file mode 100644 index 000000000..5837991cc --- /dev/null +++ b/test/valid-data/duplicates-composition/componentB.ts @@ -0,0 +1,3 @@ +export interface MyObject { + b: string; +} diff --git a/test/valid-data/duplicates-composition/main.ts b/test/valid-data/duplicates-composition/main.ts new file mode 100644 index 000000000..241cd330e --- /dev/null +++ b/test/valid-data/duplicates-composition/main.ts @@ -0,0 +1,7 @@ +import * as A from "./componentA"; +import * as B from "./componentB"; + +export interface MyObject { + a: A.MyObject; + b: B.MyObject; +} From 9ccb230e4657028c814d5c26d4c10ac3b10e623d Mon Sep 17 00:00:00 2001 From: Shishir Date: Wed, 17 Feb 2021 03:24:24 -0800 Subject: [PATCH 2/9] wip: base cases working. recursive definition test case still fails --- src/SchemaGenerator.ts | 151 +++++++-- src/Type/BaseType.ts | 8 + src/Type/DefinitionType.ts | 4 + src/TypeFormatter/DefinitionTypeFormatter.ts | 2 +- src/TypeFormatter/ReferenceTypeFormatter.ts | 2 +- src/Utils/removeUnreachable.ts | 2 +- test/invalid-data.test.ts | 1 - test/utils.ts | 12 +- test/valid-data-other.test.ts | 3 + test/valid-data/annotation-custom/schema.json | 34 +- test/valid-data/any-unknown/schema.json | 12 +- .../array-max-items-optional/schema.json | 10 +- test/valid-data/array-min-items-1/schema.json | 8 +- test/valid-data/array-min-items-2/schema.json | 8 +- .../array-min-max-items-optional/schema.json | 10 +- .../array-min-max-items/schema.json | 10 +- test/valid-data/class-extra-props/schema.json | 10 +- test/valid-data/class-generics/schema.json | 96 +++--- test/valid-data/class-inheritance/schema.json | 16 +- test/valid-data/class-jsdoc/schema.json | 42 +-- test/valid-data/class-multi/schema.json | 20 +- test/valid-data/class-recursion/schema.json | 12 +- test/valid-data/class-single/schema.json | 24 +- .../duplicates-composition/schema.json | 46 +++ .../valid-data/duplicates-inheritance/base.ts | 3 + .../duplicates-inheritance/intermediate.ts | 5 + .../valid-data/duplicates-inheritance/main.ts | 5 + .../duplicates-inheritance/schema.json | 26 ++ .../duplicates/import1.ts | 2 +- .../duplicates/import2.ts | 2 +- .../duplicates/main.ts | 8 +- test/valid-data/duplicates/schema.json | 22 ++ test/valid-data/enums-compute/schema.json | 4 +- test/valid-data/enums-initialized/schema.json | 4 +- test/valid-data/enums-member/schema.json | 10 +- test/valid-data/enums-mixed/schema.json | 4 +- test/valid-data/enums-number/schema.json | 4 +- test/valid-data/enums-string/schema.json | 4 +- test/valid-data/generic-anonymous/schema.json | 28 +- test/valid-data/generic-arrays/schema.json | 12 +- test/valid-data/generic-default/schema.json | 10 +- test/valid-data/generic-hell/schema.json | 320 +++++++++--------- test/valid-data/generic-multiargs/schema.json | 96 +++--- test/valid-data/generic-multiple/schema.json | 82 ++--- test/valid-data/generic-nested/schema.json | 26 +- .../generic-prefixed-number/schema.json | 34 +- test/valid-data/generic-recursive/schema.json | 76 ++--- test/valid-data/generic-simple/schema.json | 52 +-- test/valid-data/generic-void/schema.json | 52 +-- test/valid-data/import-anonymous/schema.json | 22 +- test/valid-data/import-exposed/schema.json | 22 +- test/valid-data/import-internal/schema.json | 28 +- test/valid-data/import-simple/schema.json | 16 +- test/valid-data/interface-array/schema.json | 14 +- .../schema.json | 10 +- .../interface-extra-props/schema.json | 10 +- test/valid-data/interface-multi/schema.json | 20 +- .../interface-property-dash/schema.json | 14 +- .../interface-recursion/schema.json | 12 +- test/valid-data/interface-single/schema.json | 14 +- test/valid-data/keyof-typeof-enum/schema.json | 24 +- .../valid-data/literal-array-type/schema.json | 34 +- .../valid-data/literal-index-type/schema.json | 17 +- .../literal-object-type/schema.json | 28 +- test/valid-data/multiple-roots1/schema.json | 12 +- .../multiple-roots2/schema/schema.json | 30 +- test/valid-data/namespace-deep-1/schema.json | 32 +- test/valid-data/namespace-deep-2/schema.json | 32 +- test/valid-data/namespace-deep-3/schema.json | 32 +- test/valid-data/nullable-null/schema.json | 4 +- .../object-literal-expression/schema.json | 17 +- test/valid-data/simple-object/schema.json | 10 +- .../string-literals-inline/schema.json | 12 +- .../string-literals-null/schema.json | 12 +- test/valid-data/string-literals/schema.json | 12 +- .../structure-anonymous/schema.json | 26 +- .../structure-extra-props/schema.json | 16 +- test/valid-data/structure-private/schema.json | 26 +- .../structure-recursion/schema.json | 18 +- test/valid-data/symbol/schema.json | 24 +- .../type-aliases-anonymous/schema.json | 12 +- .../type-aliases-local-namespace/schema.json | 26 +- .../valid-data/type-aliases-mixed/schema.json | 20 +- .../type-aliases-object/schema.json | 12 +- .../schema.json | 4 +- .../type-aliases-primitive/schema.json | 4 +- .../schema.json | 18 +- .../type-aliases-recursive-export/schema.json | 12 +- .../schema.json | 16 +- .../schema.json | 38 ++- .../type-aliases-tuple-empty/schema.json | 4 +- .../type-aliases-tuple-only-rest/schema.json | 4 +- .../schema.json | 8 +- .../type-aliases-tuple-rest/schema.json | 12 +- .../valid-data/type-aliases-tuple/schema.json | 8 +- .../valid-data/type-aliases-union/schema.json | 10 +- .../type-conditional-enum/schema.json | 16 +- .../schema.json | 6 +- .../schema.json | 100 +++--- .../type-conditional-exclude/schema.json | 24 +- .../type-conditional-inheritance/schema.json | 106 +++--- .../type-conditional-intersection/schema.json | 46 +-- .../type-conditional-jsdoc/schema.json | 68 ++-- .../type-conditional-narrowing/schema.json | 54 +-- .../type-conditional-omit/schema.json | 14 +- .../type-conditional-simple/schema.json | 28 +- .../type-conditional-union/schema.json | 34 +- test/valid-data/type-date/schema.json | 12 +- .../type-extend-circular/schema.json | 53 +-- test/valid-data/type-extend/schema.json | 14 +- .../type-indexed-access-keyof/schema.json | 14 +- .../type-indexed-access-object-1/schema.json | 4 +- .../type-indexed-access-object-2/schema.json | 6 +- .../type-indexed-access-tuple-1/schema.json | 4 +- .../type-indexed-access-tuple-2/schema.json | 4 +- .../schema.json | 6 +- .../schema.json | 25 +- .../schema.json | 34 +- .../type-intersection-conflict/schema.json | 10 +- .../schema.json | 12 +- .../type-intersection-union/schema.json | 70 ++-- test/valid-data/type-intersection/schema.json | 28 +- .../type-keyof-object-function/schema.json | 10 +- test/valid-data/type-keyof-object/schema.json | 4 +- test/valid-data/type-keyof-tuple/schema.json | 4 +- .../type-mapped-additional-props/schema.json | 30 +- test/valid-data/type-mapped-array/schema.json | 6 +- .../type-mapped-double-exclude/schema.json | 62 ++-- .../type-mapped-enum-null/schema.json | 4 +- .../type-mapped-enum-optional/schema.json | 4 +- test/valid-data/type-mapped-enum/schema.json | 14 +- .../type-mapped-exclude/schema.json | 36 +- .../type-mapped-generic/schema.json | 60 ++-- test/valid-data/type-mapped-index/schema.json | 32 +- .../type-mapped-literal/schema.json | 4 +- .../schema.json | 10 +- .../valid-data/type-mapped-native/schema.json | 4 +- .../type-mapped-optional/schema.json | 24 +- .../valid-data/type-mapped-simple/schema.json | 4 +- .../valid-data/type-mapped-symbol/schema.json | 18 +- .../schema.json | 56 +-- .../type-mapped-widened/schema.json | 4 +- test/valid-data/type-maps/schema.json | 14 +- test/valid-data/type-primitives/schema.json | 22 +- test/valid-data/type-regexp/schema.json | 12 +- test/valid-data/type-typeof-class/schema.json | 42 +-- test/valid-data/type-typeof-enum/schema.json | 122 +++---- test/valid-data/type-typeof-keys/schema.json | 17 +- .../type-typeof-object-property/schema.json | 4 +- test/valid-data/type-typeof-value/schema.json | 4 +- test/valid-data/type-typeof/schema.json | 4 +- test/valid-data/type-union-tagged/schema.json | 30 +- test/valid-data/type-union/schema.json | 20 +- test/valid-data/undefined-alias/schema.json | 4 +- .../valid-data/undefined-property/schema.json | 10 +- test/valid-data/undefined-union/schema.json | 4 +- 156 files changed, 1998 insertions(+), 1735 deletions(-) create mode 100644 test/valid-data/duplicates-composition/schema.json create mode 100644 test/valid-data/duplicates-inheritance/base.ts create mode 100644 test/valid-data/duplicates-inheritance/intermediate.ts create mode 100644 test/valid-data/duplicates-inheritance/main.ts create mode 100644 test/valid-data/duplicates-inheritance/schema.json rename test/{invalid-data => valid-data}/duplicates/import1.ts (96%) rename test/{invalid-data => valid-data}/duplicates/import2.ts (96%) rename test/{invalid-data => valid-data}/duplicates/main.ts (96%) create mode 100644 test/valid-data/duplicates/schema.json diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index 9a80b2e51..8c163c6ca 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -12,6 +12,7 @@ import { notUndefined } from "./Utils/notUndefined"; import { removeUnreachable } from "./Utils/removeUnreachable"; import { Config } from "./Config"; import { hasJsDocTag } from "./Utils/hasJsDocTag"; +import { JSONSchema7, JSONSchema7Definition } from "json-schema"; export class SchemaGenerator { public constructor( @@ -34,16 +35,31 @@ export class SchemaGenerator { .filter(notUndefined); const rootTypeDefinition = rootTypes.length === 1 ? this.getRootTypeDefinition(rootTypes[0]) : undefined; const definitions: StringMap = {}; - rootTypes.forEach((rootType) => this.appendRootChildDefinitions(rootType, definitions)); + const idNameMap = new Map(); + rootTypes.forEach((rootType) => this.appendRootChildDefinitions(rootType, definitions, idNameMap)); const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions); - return { + if (false) { + // TODO: remove this. + console.log( + JSON.stringify({ + rootTypeDefinition, + definitions, + reachableDefinitions, + }, null, 2) + ); + console.log(idNameMap); + } + // create schema - all $ref's use getId(). + const schema: JSONSchema7Definition = { ...(this.config?.schemaId ? { $id: this.config.schemaId } : {}), $schema: "http://json-schema.org/draft-07/schema#", ...(rootTypeDefinition ?? {}), definitions: reachableDefinitions, }; + // Finally, replace all getId() by their equivalent names. + return resolveIdRefs(schema, idNameMap, this.config?.encodeRefs ?? true) as JSONSchema7; } private getRootNodes(fullName: string | undefined) { @@ -81,9 +97,12 @@ export class SchemaGenerator { private getRootTypeDefinition(rootType: BaseType): Definition { return this.typeFormatter.getDefinition(rootType); } - private appendRootChildDefinitions(rootType: BaseType, childDefinitions: StringMap): void { + private appendRootChildDefinitions( + rootType: BaseType, + childDefinitions: StringMap, + idNameMap: Map + ): void { const seen = new Set(); - const children = this.typeFormatter .getChildren(rootType) .filter((child): child is DefinitionType => child instanceof DefinitionType) @@ -95,25 +114,22 @@ export class SchemaGenerator { return false; }); - const ids = new Map(); + const duplicates: StringMap> = {}; for (const child of children) { - const name = child.getName(); - const previousId = ids.get(name); - // remove def prefix from ids to avoid false alarms - // FIXME: we probably shouldn't be doing this as there is probably something wrong with the deduplication - const childId = child.getId().replace(/def-/g, ""); - - if (previousId && childId !== previousId) { - - throw new Error(`Type "${name}" has multiple definitions.`); - } - ids.set(name, childId); + const name = child.getName(); // .replace(/^def-interface/, "interface"); + duplicates[name] = duplicates[name] ?? new Set(); + duplicates[name].add(child); } children.reduce((definitions, child) => { - const name = child.getName(); - if (!(name in definitions)) { - definitions[name] = this.typeFormatter.getDefinition(child.getType()); + const id = child.getId(); // .replace(/^def-interface/, "interface"); + if (!(id in definitions)) { + // Record the schema against the ID, allowing steps like removeUnreachable to work + definitions[id] = this.typeFormatter.getDefinition(child.getType()); + // Create a record of id->name mapping. This is used in the final step + // to resolve id -> name before delivering the schema to caller. + const name = unambiguousName(child, child === rootType, [...duplicates[child.getName()]]); + idNameMap.set(id, name); } return definitions; }, childDefinitions); @@ -172,3 +188,100 @@ export class SchemaGenerator { return typeChecker.getFullyQualifiedName(symbol).replace(/".*"\./, ""); } } + +/** + * Given a set of paths, will strip the common-prefix, and return an array of remaining substrings. + * Also removes any file extensions, and replaces any '/' with '-' in the path. + * Each return value can be used as a name prefix for disambiguation. + * The returned prefixes array maintains input order. + */ +function getCommonPrefixes(paths: string[]) { + // clone before sorting to maintain input order. + const sorted = [...paths].sort(); + const shortest = sorted[0]; + const second = sorted[sorted.length - 1]; + const maxPrefix = shortest.length; + let pos = 0; + let path_pos = 0; + while (pos < maxPrefix && shortest.charAt(pos) === second.charAt(pos)) { + if (shortest.charAt(pos) === "/") { + path_pos = pos; + } + pos++; + } + return paths.map((p) => + p + .substr(path_pos + 1) + .replace(/\//g, "__") + .replace(/\.[^\.]+$/, "") + ); +} + +function unambiguousName(child: DefinitionType, isRoot: boolean, peers: DefinitionType[]) { + if (peers.length === 1 || isRoot) { + return child.getName(); + } else { + let index = -1; + const srcPaths = peers.map((peer: DefinitionType, count) => { + index = child === peer ? count : index; + return peer.getType().getSrcFileName(); + }); + const prefixes = getCommonPrefixes(srcPaths); + return `${prefixes[index]}-${child.getName()}`; + } +} + +/** + * Resolve all `#/definitions/...` and `$ref` in schema with appropriate disambiguated names + */ +function resolveIdRefs( + schema: JSONSchema7Definition, + idNameMap: Map, + encodeRefs: boolean +): JSONSchema7Definition { + if (!schema || typeof schema === "boolean") { + return schema; + } + let { $ref, allOf, oneOf, anyOf, not, properties, items, definitions, additionalProperties, ...rest } = schema; + const result: JSONSchema7Definition = { ...rest }; + if ($ref) { + // THE Money Shot. + const id = encodeRefs ? decodeURIComponent($ref.slice(14)) : $ref.slice(14); + const name = idNameMap.get(id); + result.$ref = `#/definitions/${encodeRefs ? encodeURIComponent(name!) : name}`; + } + if (definitions) { + result.definitions = Object.entries(definitions).reduce((acc, [prop, value]) => { + const name = idNameMap.get(prop)!; + acc[name] = resolveIdRefs(value, idNameMap, encodeRefs); + return acc; + }, {} as StringMap); + } + if (properties) { + result.properties = Object.entries(properties).reduce((acc, [prop, value]) => { + acc[prop] = resolveIdRefs(value, idNameMap, encodeRefs); + return acc; + }, {} as StringMap); + } + if (additionalProperties || additionalProperties === false) { + result.additionalProperties = resolveIdRefs(additionalProperties, idNameMap, encodeRefs); + } + if (items) { + result.items = Array.isArray(items) + ? items.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)) + : resolveIdRefs(items, idNameMap, encodeRefs); + } + if (allOf) { + result.allOf = allOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); + } + if (anyOf) { + result.anyOf = anyOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); + } + if (oneOf) { + result.oneOf = oneOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); + } + if (not) { + result.not = resolveIdRefs(not, idNameMap, encodeRefs); + } + return result; +} diff --git a/src/Type/BaseType.ts b/src/Type/BaseType.ts index 7b2fd8cbe..864519c5f 100644 --- a/src/Type/BaseType.ts +++ b/src/Type/BaseType.ts @@ -7,4 +7,12 @@ export abstract class BaseType { public getName(): string { return this.getId(); } + + /** + * Provide a base class implementation. Will only be exported for entities + * exposed in a schema - Alias|Enum|Class|Interface. + */ + public getSrcFileName(): string { + throw new Error(`Missing sourceFileName '${this.getId()}'`); + } } diff --git a/src/Type/DefinitionType.ts b/src/Type/DefinitionType.ts index 89b25940f..2d0c8cbef 100644 --- a/src/Type/DefinitionType.ts +++ b/src/Type/DefinitionType.ts @@ -16,4 +16,8 @@ export class DefinitionType extends BaseType { public getType(): BaseType { return this.type; } + + public getSrcFileName(): string { + return this.type.getSrcFileName(); + } } diff --git a/src/TypeFormatter/DefinitionTypeFormatter.ts b/src/TypeFormatter/DefinitionTypeFormatter.ts index 8fe8151dd..1ff913558 100644 --- a/src/TypeFormatter/DefinitionTypeFormatter.ts +++ b/src/TypeFormatter/DefinitionTypeFormatter.ts @@ -12,7 +12,7 @@ export class DefinitionTypeFormatter implements SubTypeFormatter { return type instanceof DefinitionType; } public getDefinition(type: DefinitionType): Definition { - const ref = type.getName(); + const ref = type.getId(); return { $ref: `#/definitions/${this.encodeRefs ? encodeURIComponent(ref) : ref}` }; } public getChildren(type: DefinitionType): BaseType[] { diff --git a/src/TypeFormatter/ReferenceTypeFormatter.ts b/src/TypeFormatter/ReferenceTypeFormatter.ts index 42871b63d..5ffe4d508 100644 --- a/src/TypeFormatter/ReferenceTypeFormatter.ts +++ b/src/TypeFormatter/ReferenceTypeFormatter.ts @@ -12,7 +12,7 @@ export class ReferenceTypeFormatter implements SubTypeFormatter { return type instanceof ReferenceType; } public getDefinition(type: ReferenceType): Definition { - const ref = type.getName(); + const ref = type.getId(); return { $ref: `#/definitions/${this.encodeRefs ? encodeURIComponent(ref) : ref}` }; } public getChildren(type: ReferenceType): BaseType[] { diff --git a/src/Utils/removeUnreachable.ts b/src/Utils/removeUnreachable.ts index 1619fc3f9..be29b9ad8 100644 --- a/src/Utils/removeUnreachable.ts +++ b/src/Utils/removeUnreachable.ts @@ -8,7 +8,7 @@ function addReachable( definitions: StringMap, reachable: Set ) { - if (isBoolean(definition)) { + if (!definition || isBoolean(definition)) { return; } diff --git a/test/invalid-data.test.ts b/test/invalid-data.test.ts index 8abcf3f53..5e775067b 100644 --- a/test/invalid-data.test.ts +++ b/test/invalid-data.test.ts @@ -32,5 +32,4 @@ describe("invalid-data", () => { // TODO: template recursive it("script-empty", assertSchema("script-empty", "MyType", `No root type "MyType" found`)); - it("duplicates", assertSchema("duplicates", "MyType", `Type "A" has multiple definitions.`)); }); diff --git a/test/utils.ts b/test/utils.ts index 4426f341c..c33f5382f 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,6 +1,6 @@ import Ajv from "ajv"; import addFormats from "ajv-formats"; -import { readFileSync } from "fs"; +import { readFileSync, writeFileSync } from "fs"; import { resolve } from "path"; import ts from "typescript"; import { createFormatter } from "../factory/formatter"; @@ -47,11 +47,11 @@ export function assertValidSchema( const actual: any = JSON.parse(JSON.stringify(schema)); // uncomment to write test files - // writeFileSync( - // resolve(`${basePath}/${relativePath}/schema.json`), - // JSON.stringify(schema, null, 4) + "\n", - // "utf8" - // ); + writeFileSync( + resolve(`${basePath}/${relativePath}/schema.json`), + JSON.stringify(schema, null, 4) + "\n", + "utf8" + ); expect(typeof actual).toBe("object"); expect(actual).toEqual(expected); diff --git a/test/valid-data-other.test.ts b/test/valid-data-other.test.ts index 70de6cc49..db19f8461 100644 --- a/test/valid-data-other.test.ts +++ b/test/valid-data-other.test.ts @@ -64,4 +64,7 @@ describe("valid-data-other", () => { it("array-min-max-items", assertValidSchema("array-min-max-items", "MyType")); it("array-min-max-items-optional", assertValidSchema("array-min-max-items-optional", "MyType")); it("array-max-items-optional", assertValidSchema("array-max-items-optional", "MyType")); + it("duplicates", assertValidSchema("duplicates", "MyType")); + it("duplicates-composition", assertValidSchema("duplicates-composition", "MyObject")); + it("duplicates-inheritance", assertValidSchema("duplicates-inheritance", "MyObject")); }); diff --git a/test/valid-data/annotation-custom/schema.json b/test/valid-data/annotation-custom/schema.json index 79bad120a..b8bc53895 100644 --- a/test/valid-data/annotation-custom/schema.json +++ b/test/valid-data/annotation-custom/schema.json @@ -1,18 +1,22 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "customComplexProperty": { - "a": 1, - "b": 2 - }, - "customMultilineProperty": ["value1", "value2", "value3"], - "customNumberProperty": 14, - "customStringProperty": "string-value", - "customUnquotedProperty": "not-a-valid-json", - "type": "object" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "customNumberProperty": 14, + "customStringProperty": "string-value", + "customComplexProperty": { + "a": 1, + "b": 2 + }, + "customMultilineProperty": [ + "value1", + "value2", + "value3" + ], + "customUnquotedProperty": "not-a-valid-json", + "additionalProperties": false + } } - } } diff --git a/test/valid-data/any-unknown/schema.json b/test/valid-data/any-unknown/schema.json index 00a47b050..839bfe587 100644 --- a/test/valid-data/any-unknown/schema.json +++ b/test/valid-data/any-unknown/schema.json @@ -1,18 +1,18 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", - "properties": { - "value1": {}, - "value2": {} - }, "required": [ "value1", "value2" ], + "properties": { + "value1": {}, + "value2": {} + }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/array-max-items-optional/schema.json b/test/valid-data/array-max-items-optional/schema.json index 2e60c3b1e..d491deecc 100644 --- a/test/valid-data/array-max-items-optional/schema.json +++ b/test/valid-data/array-max-items-optional/schema.json @@ -1,14 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "array", + "minItems": 0, + "maxItems": 2, "items": { "type": "string" - }, - "minItems": 0, - "maxItems": 2 + } } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/array-min-items-1/schema.json b/test/valid-data/array-min-items-1/schema.json index f4fc5ded1..51e34141b 100644 --- a/test/valid-data/array-min-items-1/schema.json +++ b/test/valid-data/array-min-items-1/schema.json @@ -1,13 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "array", + "minItems": 1, "items": { "type": "string" - }, - "minItems": 1 + } } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/array-min-items-2/schema.json b/test/valid-data/array-min-items-2/schema.json index 06fd0b447..73ef790e6 100644 --- a/test/valid-data/array-min-items-2/schema.json +++ b/test/valid-data/array-min-items-2/schema.json @@ -1,13 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "array", + "minItems": 2, "items": { "type": "string" - }, - "minItems": 2 + } } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/array-min-max-items-optional/schema.json b/test/valid-data/array-min-max-items-optional/schema.json index 88182e985..f9b3daf52 100644 --- a/test/valid-data/array-min-max-items-optional/schema.json +++ b/test/valid-data/array-min-max-items-optional/schema.json @@ -1,14 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "array", + "minItems": 2, + "maxItems": 3, "items": { "type": "string" - }, - "minItems": 2, - "maxItems": 3 + } } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/array-min-max-items/schema.json b/test/valid-data/array-min-max-items/schema.json index 281f6e3c0..4d67d7ef6 100644 --- a/test/valid-data/array-min-max-items/schema.json +++ b/test/valid-data/array-min-max-items/schema.json @@ -1,14 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "array", + "minItems": 3, + "maxItems": 3, "items": { "type": "string" - }, - "minItems": 3, - "maxItems": 3 + } } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/class-extra-props/schema.json b/test/valid-data/class-extra-props/schema.json index 4dfbcb5a3..c45150d4a 100644 --- a/test/valid-data/class-extra-props/schema.json +++ b/test/valid-data/class-extra-props/schema.json @@ -1,8 +1,12 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "required" + ], "properties": { "required": { "type": "string" @@ -11,9 +15,6 @@ "type": "number" } }, - "required": [ - "required" - ], "additionalProperties": { "type": [ "string", @@ -21,6 +22,5 @@ ] } } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/class-generics/schema.json b/test/valid-data/class-generics/schema.json index 4484957dc..56eea589a 100644 --- a/test/valid-data/class-generics/schema.json +++ b/test/valid-data/class-generics/schema.json @@ -1,54 +1,54 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "Base": { - "additionalProperties": false, - "properties": { - "a": { - "type": "boolean" - } - }, - "required": [ - "a" - ], - "type": "object" - }, - "Base": { - "additionalProperties": false, - "properties": { - "a": { - "type": "string" - } - }, - "required": [ - "a" - ], - "type": "object" - }, - "MyObject": { - "additionalProperties": false, - "properties": { - "a": { - "type": "number" - }, - "b": { - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "a", + "b", + "c", + "d" + ], + "properties": { + "a": { + "type": "number" + }, + "b": { + "type": "string" + }, + "c": { + "$ref": "#/definitions/Base%3Cstring%3E" + }, + "d": { + "$ref": "#/definitions/Base%3Cboolean%3E" + } + }, + "additionalProperties": false }, - "c": { - "$ref": "#/definitions/Base%3Cstring%3E" + "Base": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "type": "string" + } + }, + "additionalProperties": false }, - "d": { - "$ref": "#/definitions/Base%3Cboolean%3E" + "Base": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "type": "boolean" + } + }, + "additionalProperties": false } - }, - "required": [ - "a", - "b", - "c", - "d" - ], - "type": "object" } - } } diff --git a/test/valid-data/class-inheritance/schema.json b/test/valid-data/class-inheritance/schema.json index cebc75839..4a3b850df 100644 --- a/test/valid-data/class-inheritance/schema.json +++ b/test/valid-data/class-inheritance/schema.json @@ -1,9 +1,14 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "b", + "c" + ], "properties": { "a": { "type": "number" @@ -15,12 +20,7 @@ "type": "boolean" } }, - "required": [ - "a", - "b", - "c" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/class-jsdoc/schema.json b/test/valid-data/class-jsdoc/schema.json index fba8d2fd1..29b720c06 100644 --- a/test/valid-data/class-jsdoc/schema.json +++ b/test/valid-data/class-jsdoc/schema.json @@ -1,36 +1,36 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "x", + "y", + "a", + "b" + ], "description": "Class Description", "properties": { - "a": { - "description": "Parameter a description", - "type": "string" - }, - "b": { - "description": "Parameter b description", - "type": "number" - }, "x": { - "description": "Property x description", - "type": "string" + "type": "string", + "description": "Property x description" }, "y": { + "type": "string", "description": "Property y description", - "pattern": "/abc/", - "type": "string" + "pattern": "/abc/" + }, + "a": { + "type": "string", + "description": "Parameter a description" + }, + "b": { + "type": "number", + "description": "Parameter b description" } }, - "required": [ - "x", - "y", - "a", - "b" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/class-multi/schema.json b/test/valid-data/class-multi/schema.json index 6d583bebb..f8c67c1a1 100644 --- a/test/valid-data/class-multi/schema.json +++ b/test/valid-data/class-multi/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "subA", + "subB" + ], "properties": { "subA": { "$ref": "#/definitions/MySubObject" @@ -11,14 +16,14 @@ "$ref": "#/definitions/MySubObject" } }, - "required": [ - "subA", - "subB" - ], "additionalProperties": false }, "MySubObject": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -27,12 +32,7 @@ "type": "number" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/class-recursion/schema.json b/test/valid-data/class-recursion/schema.json index ac19cdf1a..6eef63e57 100644 --- a/test/valid-data/class-recursion/schema.json +++ b/test/valid-data/class-recursion/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -11,12 +16,7 @@ "$ref": "#/definitions/MyObject" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/class-single/schema.json b/test/valid-data/class-single/schema.json index 523543f2c..d5441ff72 100644 --- a/test/valid-data/class-single/schema.json +++ b/test/valid-data/class-single/schema.json @@ -1,9 +1,15 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "propA", + "propB", + "readonlyProp", + "propC" + ], "properties": { "propA": { "type": "number" @@ -11,23 +17,17 @@ "propB": { "type": "number" }, + "readonlyProp": { + "type": "string" + }, "propC": { "type": "number" }, "propD": { "type": "string" - }, - "readonlyProp": { - "type": "string" } }, - "required": [ - "propA", - "propB", - "readonlyProp", - "propC" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/duplicates-composition/schema.json b/test/valid-data/duplicates-composition/schema.json new file mode 100644 index 000000000..31a28440a --- /dev/null +++ b/test/valid-data/duplicates-composition/schema.json @@ -0,0 +1,46 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "a", + "b" + ], + "properties": { + "a": { + "$ref": "#/definitions/componentA-MyObject" + }, + "b": { + "$ref": "#/definitions/componentB-MyObject" + } + }, + "additionalProperties": false + }, + "componentA-MyObject": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "type": "string" + } + }, + "additionalProperties": false + }, + "componentB-MyObject": { + "type": "object", + "required": [ + "b" + ], + "properties": { + "b": { + "type": "string" + } + }, + "additionalProperties": false + } + } +} diff --git a/test/valid-data/duplicates-inheritance/base.ts b/test/valid-data/duplicates-inheritance/base.ts new file mode 100644 index 000000000..bd5ff59fb --- /dev/null +++ b/test/valid-data/duplicates-inheritance/base.ts @@ -0,0 +1,3 @@ +export interface MyObject { + a: string; +} diff --git a/test/valid-data/duplicates-inheritance/intermediate.ts b/test/valid-data/duplicates-inheritance/intermediate.ts new file mode 100644 index 000000000..3ae21b2ec --- /dev/null +++ b/test/valid-data/duplicates-inheritance/intermediate.ts @@ -0,0 +1,5 @@ +import * as Base from "./Base"; + +export interface MyObject extends Base.MyObject { + b: string; +} diff --git a/test/valid-data/duplicates-inheritance/main.ts b/test/valid-data/duplicates-inheritance/main.ts new file mode 100644 index 000000000..95e357b5a --- /dev/null +++ b/test/valid-data/duplicates-inheritance/main.ts @@ -0,0 +1,5 @@ +import * as Intermediate from "./Intermediate"; + +export interface MyObject extends Intermediate.MyObject { + c: string; +} diff --git a/test/valid-data/duplicates-inheritance/schema.json b/test/valid-data/duplicates-inheritance/schema.json new file mode 100644 index 000000000..127983313 --- /dev/null +++ b/test/valid-data/duplicates-inheritance/schema.json @@ -0,0 +1,26 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "a", + "b", + "c" + ], + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "string" + }, + "c": { + "type": "string" + } + }, + "additionalProperties": false + } + } +} diff --git a/test/invalid-data/duplicates/import1.ts b/test/valid-data/duplicates/import1.ts similarity index 96% rename from test/invalid-data/duplicates/import1.ts rename to test/valid-data/duplicates/import1.ts index 3035d5d5e..2ded7dc83 100644 --- a/test/invalid-data/duplicates/import1.ts +++ b/test/valid-data/duplicates/import1.ts @@ -1 +1 @@ -export type A = number; +export type A = number; diff --git a/test/invalid-data/duplicates/import2.ts b/test/valid-data/duplicates/import2.ts similarity index 96% rename from test/invalid-data/duplicates/import2.ts rename to test/valid-data/duplicates/import2.ts index 3a4ed407b..0cc829ffa 100644 --- a/test/invalid-data/duplicates/import2.ts +++ b/test/valid-data/duplicates/import2.ts @@ -1 +1 @@ -export type A = string; +export type A = string; diff --git a/test/invalid-data/duplicates/main.ts b/test/valid-data/duplicates/main.ts similarity index 96% rename from test/invalid-data/duplicates/main.ts rename to test/valid-data/duplicates/main.ts index ad3ed7270..b7f2d7ff2 100644 --- a/test/invalid-data/duplicates/main.ts +++ b/test/valid-data/duplicates/main.ts @@ -1,4 +1,4 @@ -import {A as A1} from "./import1"; -import {A as A2} from "./import2"; - -export type MyType = A1 | A2; +import {A as A1} from "./import1"; +import {A as A2} from "./import2"; + +export type MyType = A1 | A2; diff --git a/test/valid-data/duplicates/schema.json b/test/valid-data/duplicates/schema.json new file mode 100644 index 000000000..94b7ed2cd --- /dev/null +++ b/test/valid-data/duplicates/schema.json @@ -0,0 +1,22 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "anyOf": [ + { + "$ref": "#/definitions/import1-A" + }, + { + "$ref": "#/definitions/import2-A" + } + ] + }, + "import1-A": { + "type": "number" + }, + "import2-A": { + "type": "string" + } + } +} diff --git a/test/valid-data/enums-compute/schema.json b/test/valid-data/enums-compute/schema.json index ac025740e..802b2b0a7 100644 --- a/test/valid-data/enums-compute/schema.json +++ b/test/valid-data/enums-compute/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Enum", "definitions": { "Enum": { "type": "number", @@ -10,6 +11,5 @@ 1 ] } - }, - "$ref": "#/definitions/Enum" + } } diff --git a/test/valid-data/enums-initialized/schema.json b/test/valid-data/enums-initialized/schema.json index bda329c35..365c0222a 100644 --- a/test/valid-data/enums-initialized/schema.json +++ b/test/valid-data/enums-initialized/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Enum", "definitions": { "Enum": { "type": "number", @@ -10,6 +11,5 @@ 1 ] } - }, - "$ref": "#/definitions/Enum" + } } diff --git a/test/valid-data/enums-member/schema.json b/test/valid-data/enums-member/schema.json index 64d69979a..e26d67009 100644 --- a/test/valid-data/enums-member/schema.json +++ b/test/valid-data/enums-member/schema.json @@ -1,19 +1,19 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "code" + ], "properties": { "code": { "type": "number", "const": 0 } }, - "required": [ - "code" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/enums-mixed/schema.json b/test/valid-data/enums-mixed/schema.json index 905f01f55..01147f9f5 100644 --- a/test/valid-data/enums-mixed/schema.json +++ b/test/valid-data/enums-mixed/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Enum", "definitions": { "Enum": { "type": [ @@ -16,6 +17,5 @@ null ] } - }, - "$ref": "#/definitions/Enum" + } } diff --git a/test/valid-data/enums-number/schema.json b/test/valid-data/enums-number/schema.json index d48ff3d13..f136c1620 100644 --- a/test/valid-data/enums-number/schema.json +++ b/test/valid-data/enums-number/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Enum", "definitions": { "Enum": { "type": "number", @@ -8,6 +9,5 @@ 2 ] } - }, - "$ref": "#/definitions/Enum" + } } diff --git a/test/valid-data/enums-string/schema.json b/test/valid-data/enums-string/schema.json index d31bb3f6d..4b91336f8 100644 --- a/test/valid-data/enums-string/schema.json +++ b/test/valid-data/enums-string/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Enum", "definitions": { "Enum": { "type": "string", @@ -8,6 +9,5 @@ "y" ] } - }, - "$ref": "#/definitions/Enum" + } } diff --git a/test/valid-data/generic-anonymous/schema.json b/test/valid-data/generic-anonymous/schema.json index 5b200ed13..7903eea49 100644 --- a/test/valid-data/generic-anonymous/schema.json +++ b/test/valid-data/generic-anonymous/schema.json @@ -1,11 +1,20 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "value1", + "value2" + ], "properties": { "value1": { "type": "object", + "required": [ + "a", + "b" + ], "properties": { "a": { "type": "string" @@ -14,14 +23,14 @@ "type": "number" } }, - "required": [ - "a", - "b" - ], "additionalProperties": false }, "value2": { "type": "object", + "required": [ + "a", + "b" + ], "properties": { "a": { "type": "number" @@ -30,19 +39,10 @@ "type": "string" } }, - "required": [ - "a", - "b" - ], "additionalProperties": false } }, - "required": [ - "value1", - "value2" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/generic-arrays/schema.json b/test/valid-data/generic-arrays/schema.json index 5280d4b10..479a7d08e 100644 --- a/test/valid-data/generic-arrays/schema.json +++ b/test/valid-data/generic-arrays/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "numberArray", + "stringArray" + ], "properties": { "numberArray": { "type": "array", @@ -17,12 +22,7 @@ } } }, - "required": [ - "numberArray", - "stringArray" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/generic-default/schema.json b/test/valid-data/generic-default/schema.json index 36cb98da5..e9e6feddb 100644 --- a/test/valid-data/generic-default/schema.json +++ b/test/valid-data/generic-default/schema.json @@ -1,21 +1,21 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "$ref": "#/definitions/Generic" }, "Generic": { "type": "object", + "required": [ + "foo" + ], "properties": { "foo": { "type": "number" } }, - "required": [ - "foo" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/generic-hell/schema.json b/test/valid-data/generic-hell/schema.json index e8ae9f425..60b3e570d 100644 --- a/test/valid-data/generic-hell/schema.json +++ b/test/valid-data/generic-hell/schema.json @@ -1,173 +1,173 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "b": { - "type": "number" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "b", + "c", + "someAlias", + "someGeneric" + ], + "properties": { + "b": { + "type": "number" + }, + "c": { + "$ref": "#/definitions/GenericC%3CGenericC%3CGenericA%3Cstring%3E%3E%3E" + }, + "someGeneric": { + "$ref": "#/definitions/SomeGeneric%3C1%2C2%3E" + }, + "someAlias": { + "$ref": "#/definitions/SomeAlias%3C%22alias%22%3E" + } + }, + "additionalProperties": false }, - "c": { - "$ref": "#/definitions/GenericC%3CGenericC%3CGenericA%3Cstring%3E%3E%3E" + "GenericC>>": { + "type": "object", + "required": [ + "c" + ], + "properties": { + "c": { + "$ref": "#/definitions/GenericC%3CGenericA%3Cstring%3E%3E" + } + }, + "additionalProperties": false }, - "someGeneric": { - "$ref": "#/definitions/SomeGeneric%3C1%2C2%3E" + "GenericC>": { + "type": "object", + "required": [ + "c" + ], + "properties": { + "c": { + "$ref": "#/definitions/GenericA%3Cstring%3E" + } + }, + "additionalProperties": false }, - "someAlias": { - "$ref": "#/definitions/SomeAlias%3C%22alias%22%3E" - } - }, - "required": [ - "b", - "c", - "someAlias", - "someGeneric" - ], - "additionalProperties": false - }, - "GenericC>>": { - "type": "object", - "properties": { - "c": { - "$ref": "#/definitions/GenericC%3CGenericA%3Cstring%3E%3E" - } - }, - "required": [ - "c" - ], - "additionalProperties": false - }, - "GenericC>": { - "type": "object", - "properties": { - "c": { - "$ref": "#/definitions/GenericA%3Cstring%3E" - } - }, - "required": [ - "c" - ], - "additionalProperties": false - }, - "GenericA": { - "type": "object", - "properties": { - "a": { - "type": "string" - } - }, - "required": [ - "a" - ], - "additionalProperties": false - }, - "SomeGeneric<1,2>": { - "type": "object", - "properties": { - "a": { - "type": "number", - "const": 1 + "GenericA": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "type": "string" + } + }, + "additionalProperties": false }, - "b": { - "type": "number", - "const": 2 + "SomeGeneric<1,2>": { + "type": "object", + "required": [ + "a", + "b", + "c", + "d" + ], + "properties": { + "a": { + "type": "number", + "const": 1 + }, + "b": { + "type": "number", + "const": 2 + }, + "c": { + "$ref": "#/definitions/GenericA%3C1%3E" + }, + "d": { + "$ref": "#/definitions/GenericC%3C2%3E" + } + }, + "additionalProperties": false }, - "c": { - "$ref": "#/definitions/GenericA%3C1%3E" + "GenericA<1>": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "type": "number", + "const": 1 + } + }, + "additionalProperties": false }, - "d": { - "$ref": "#/definitions/GenericC%3C2%3E" - } - }, - "required": [ - "a", - "b", - "c", - "d" - ], - "additionalProperties": false - }, - "GenericA<1>": { - "type": "object", - "properties": { - "a": { - "type": "number", - "const": 1 - } - }, - "required": [ - "a" - ], - "additionalProperties": false - }, - "GenericC<2>": { - "type": "object", - "properties": { - "c": { - "type": "number", - "const": 2 - } - }, - "required": [ - "c" - ], - "additionalProperties": false - }, - "SomeAlias<\"alias\">": { - "$ref": "#/definitions/SomeGeneric%3C%22alias%22%2C%22alias%22%3E" - }, - "SomeGeneric<\"alias\",\"alias\">": { - "type": "object", - "properties": { - "a": { - "type": "string", - "const": "alias" + "GenericC<2>": { + "type": "object", + "required": [ + "c" + ], + "properties": { + "c": { + "type": "number", + "const": 2 + } + }, + "additionalProperties": false }, - "b": { - "type": "string", - "const": "alias" + "SomeAlias<\"alias\">": { + "$ref": "#/definitions/SomeGeneric%3C%22alias%22%2C%22alias%22%3E" }, - "c": { - "$ref": "#/definitions/GenericA%3C%22alias%22%3E" + "SomeGeneric<\"alias\",\"alias\">": { + "type": "object", + "required": [ + "a", + "b", + "c", + "d" + ], + "properties": { + "a": { + "type": "string", + "const": "alias" + }, + "b": { + "type": "string", + "const": "alias" + }, + "c": { + "$ref": "#/definitions/GenericA%3C%22alias%22%3E" + }, + "d": { + "$ref": "#/definitions/GenericC%3C%22alias%22%3E" + } + }, + "additionalProperties": false }, - "d": { - "$ref": "#/definitions/GenericC%3C%22alias%22%3E" - } - }, - "required": [ - "a", - "b", - "c", - "d" - ], - "additionalProperties": false - }, - "GenericA<\"alias\">": { - "type": "object", - "properties": { - "a": { - "type": "string", - "const": "alias" - } - }, - "required": [ - "a" - ], - "additionalProperties": false - }, - "GenericC<\"alias\">": { - "type": "object", - "properties": { - "c": { - "type": "string", - "const": "alias" + "GenericA<\"alias\">": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "type": "string", + "const": "alias" + } + }, + "additionalProperties": false + }, + "GenericC<\"alias\">": { + "type": "object", + "required": [ + "c" + ], + "properties": { + "c": { + "type": "string", + "const": "alias" + } + }, + "additionalProperties": false } - }, - "required": [ - "c" - ], - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/generic-multiargs/schema.json b/test/valid-data/generic-multiargs/schema.json index 2c1bb62c7..52c91b758 100644 --- a/test/valid-data/generic-multiargs/schema.json +++ b/test/valid-data/generic-multiargs/schema.json @@ -1,54 +1,54 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "value1": { - "$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "value1", + "value2" + ], + "properties": { + "value1": { + "$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E" + }, + "value2": { + "$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E" + } + }, + "additionalProperties": false }, - "value2": { - "$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E" - } - }, - "required": [ - "value1", - "value2" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "a": { - "type": "string" - }, - "b": { - "type": "number" - } - }, - "required": [ - "a", - "b" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "a": { - "type": "number" + "MyGeneric": { + "type": "object", + "required": [ + "a", + "b" + ], + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "number" + } + }, + "additionalProperties": false }, - "b": { - "type": "string" + "MyGeneric": { + "type": "object", + "required": [ + "a", + "b" + ], + "properties": { + "a": { + "type": "number" + }, + "b": { + "type": "string" + } + }, + "additionalProperties": false } - }, - "required": [ - "a", - "b" - ], - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/generic-multiple/schema.json b/test/valid-data/generic-multiple/schema.json index e02bbc4ca..692136f2e 100644 --- a/test/valid-data/generic-multiple/schema.json +++ b/test/valid-data/generic-multiple/schema.json @@ -1,46 +1,46 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "value1": { - "$ref": "#/definitions/MyGeneric%3Cnumber%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "value1", + "value2" + ], + "properties": { + "value1": { + "$ref": "#/definitions/MyGeneric%3Cnumber%3E" + }, + "value2": { + "$ref": "#/definitions/MyGeneric%3Cstring%3E" + } + }, + "additionalProperties": false }, - "value2": { - "$ref": "#/definitions/MyGeneric%3Cstring%3E" - } - }, - "required": [ - "value1", - "value2" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "field": { - "type": "number" - } - }, - "required": [ - "field" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "field": { - "type": "string" + "MyGeneric": { + "type": "object", + "required": [ + "field" + ], + "properties": { + "field": { + "type": "number" + } + }, + "additionalProperties": false + }, + "MyGeneric": { + "type": "object", + "required": [ + "field" + ], + "properties": { + "field": { + "type": "string" + } + }, + "additionalProperties": false } - }, - "required": [ - "field" - ], - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/generic-nested/schema.json b/test/valid-data/generic-nested/schema.json index 9cb0fada2..3198731bb 100644 --- a/test/valid-data/generic-nested/schema.json +++ b/test/valid-data/generic-nested/schema.json @@ -4,27 +4,30 @@ "definitions": { "MyObject": { "type": "object", - "additionalProperties": false, "properties": { "responseType": { "$ref": "#/definitions/IRes" } - } + }, + "additionalProperties": false }, "IRes": { "type": "object", + "required": [ + "foo" + ], "properties": { "foo": { "$ref": "#/definitions/IListsable" } }, - "required": [ - "foo" - ], "additionalProperties": false }, "IListsable": { "type": "object", + "required": [ + "lists" + ], "properties": { "lists": { "type": "array", @@ -33,13 +36,15 @@ } } }, - "required": [ - "lists" - ], "additionalProperties": false }, "MyArrayObjectType": { "type": "object", + "required": [ + "a", + "b", + "c" + ], "properties": { "a": { "type": "string" @@ -51,11 +56,6 @@ "type": "string" } }, - "required": [ - "a", - "b", - "c" - ], "additionalProperties": false } } diff --git a/test/valid-data/generic-prefixed-number/schema.json b/test/valid-data/generic-prefixed-number/schema.json index 6542118b6..7e244c739 100644 --- a/test/valid-data/generic-prefixed-number/schema.json +++ b/test/valid-data/generic-prefixed-number/schema.json @@ -1,21 +1,21 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "properties": { - "angle": { - "$ref": "#/definitions/Range%3C-180%2C180%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "angle" + ], + "properties": { + "angle": { + "$ref": "#/definitions/Range%3C-180%2C180%3E" + } + }, + "additionalProperties": false + }, + "Range<-180,180>": { + "type": "number" } - }, - "required": [ - "angle" - ], - "type": "object" - }, - "Range<-180,180>": { - "type": "number" } - } } diff --git a/test/valid-data/generic-recursive/schema.json b/test/valid-data/generic-recursive/schema.json index 7d116e4a0..b699524a5 100644 --- a/test/valid-data/generic-recursive/schema.json +++ b/test/valid-data/generic-recursive/schema.json @@ -1,42 +1,42 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "value": { - "$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "value" + ], + "properties": { + "value": { + "$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E" + } + }, + "additionalProperties": false + }, + "MyGeneric": { + "type": "object", + "required": [ + "field" + ], + "properties": { + "field": { + "$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E" + } + }, + "additionalProperties": false + }, + "MyGeneric": { + "type": "object", + "required": [ + "field" + ], + "properties": { + "field": { + "$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E" + } + }, + "additionalProperties": false } - }, - "required": [ - "value" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "field": { - "$ref": "#/definitions/MyGeneric%3Cnumber%2Cstring%3E" - } - }, - "required": [ - "field" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "field": { - "$ref": "#/definitions/MyGeneric%3Cstring%2Cnumber%3E" - } - }, - "required": [ - "field" - ], - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/generic-simple/schema.json b/test/valid-data/generic-simple/schema.json index c3cdf2d3e..5c25c95db 100644 --- a/test/valid-data/generic-simple/schema.json +++ b/test/valid-data/generic-simple/schema.json @@ -1,30 +1,30 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "value": { - "$ref": "#/definitions/MyGeneric%3Cnumber%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "value" + ], + "properties": { + "value": { + "$ref": "#/definitions/MyGeneric%3Cnumber%3E" + } + }, + "additionalProperties": false + }, + "MyGeneric": { + "type": "object", + "required": [ + "field" + ], + "properties": { + "field": { + "type": "number" + } + }, + "additionalProperties": false } - }, - "required": [ - "value" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "field": { - "type": "number" - } - }, - "required": [ - "field" - ], - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/generic-void/schema.json b/test/valid-data/generic-void/schema.json index 8ad475b68..8fd76d9de 100644 --- a/test/valid-data/generic-void/schema.json +++ b/test/valid-data/generic-void/schema.json @@ -1,30 +1,30 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "value": { - "$ref": "#/definitions/MyGeneric%3Cvoid%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "value" + ], + "properties": { + "value": { + "$ref": "#/definitions/MyGeneric%3Cvoid%3E" + } + }, + "additionalProperties": false + }, + "MyGeneric": { + "type": "object", + "required": [ + "field" + ], + "properties": { + "field": { + "type": "null" + } + }, + "additionalProperties": false } - }, - "required": [ - "value" - ], - "additionalProperties": false - }, - "MyGeneric": { - "type": "object", - "properties": { - "field": { - "type": "null" - } - }, - "required": [ - "field" - ], - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/import-anonymous/schema.json b/test/valid-data/import-anonymous/schema.json index 21fecf0ad..191b03d92 100644 --- a/test/valid-data/import-anonymous/schema.json +++ b/test/valid-data/import-anonymous/schema.json @@ -1,39 +1,39 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "field" + ], "properties": { "field": { "$ref": "#/definitions/MySubObject" } }, - "required": [ - "field" - ], "additionalProperties": false }, "MySubObject": { "type": "object", + "required": [ + "value" + ], "properties": { "value": { "type": "object", + "required": [ + "subvalue" + ], "properties": { "subvalue": { "type": "number" } }, - "required": [ - "subvalue" - ], "additionalProperties": false } }, - "required": [ - "value" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/import-exposed/schema.json b/test/valid-data/import-exposed/schema.json index 751962379..abc29fdcf 100644 --- a/test/valid-data/import-exposed/schema.json +++ b/test/valid-data/import-exposed/schema.json @@ -1,42 +1,42 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "field" + ], "properties": { "field": { "$ref": "#/definitions/MySubObject" } }, - "required": [ - "field" - ], "additionalProperties": false }, "MySubObject": { "type": "object", + "required": [ + "value" + ], "properties": { "value": { "$ref": "#/definitions/ExposedSubType" } }, - "required": [ - "value" - ], "additionalProperties": false }, "ExposedSubType": { "type": "object", + "required": [ + "subvalue" + ], "properties": { "subvalue": { "type": "number" } }, - "required": [ - "subvalue" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/import-internal/schema.json b/test/valid-data/import-internal/schema.json index 9bce659c7..06ecb8032 100644 --- a/test/valid-data/import-internal/schema.json +++ b/test/valid-data/import-internal/schema.json @@ -1,11 +1,21 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "internalSubType", + "internalAlias", + "exposedSubType" + ], "properties": { "internalSubType": { "type": "object", + "required": [ + "text", + "exposedSubType" + ], "properties": { "text": { "type": "string" @@ -18,10 +28,6 @@ "format": "date-time" } }, - "required": [ - "text", - "exposedSubType" - ], "additionalProperties": false }, "internalAlias": { @@ -32,26 +38,20 @@ "$ref": "#/definitions/ExposedSubType" } }, - "required": [ - "internalSubType", - "internalAlias", - "exposedSubType" - ], "additionalProperties": false }, "ExposedSubType": { "type": "object", + "required": [ + "internalAlias" + ], "properties": { "internalAlias": { "type": "string", "format": "date-time" } }, - "required": [ - "internalAlias" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/import-simple/schema.json b/test/valid-data/import-simple/schema.json index 81ee3324a..653770b70 100644 --- a/test/valid-data/import-simple/schema.json +++ b/test/valid-data/import-simple/schema.json @@ -1,30 +1,30 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "field" + ], "properties": { "field": { "$ref": "#/definitions/MySubObject" } }, - "required": [ - "field" - ], "additionalProperties": false }, "MySubObject": { "type": "object", + "required": [ + "value" + ], "properties": { "value": { "type": "number" } }, - "required": [ - "value" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/interface-array/schema.json b/test/valid-data/interface-array/schema.json index d5600e227..a30ecb783 100644 --- a/test/valid-data/interface-array/schema.json +++ b/test/valid-data/interface-array/schema.json @@ -1,7 +1,13 @@ { - "$ref": "#/definitions/TagArray", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/TagArray", "definitions": { + "TagArray": { + "type": "array", + "items": { + "$ref": "#/definitions/Tag" + } + }, "Tag": { "anyOf": [ { @@ -12,12 +18,6 @@ } ] }, - "TagArray": { - "items": { - "$ref": "#/definitions/Tag" - }, - "type": "array" - }, "TagPrimitive": { "type": [ "string", diff --git a/test/valid-data/interface-extended-extra-props/schema.json b/test/valid-data/interface-extended-extra-props/schema.json index f18713801..705cd28bc 100644 --- a/test/valid-data/interface-extended-extra-props/schema.json +++ b/test/valid-data/interface-extended-extra-props/schema.json @@ -3,15 +3,15 @@ "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { + "type": "object", + "required": [ + "param" + ], "properties": { "param": { "type": "string" } - }, - "required": [ - "param" - ], - "type": "object" + } } } } diff --git a/test/valid-data/interface-extra-props/schema.json b/test/valid-data/interface-extra-props/schema.json index 4dfbcb5a3..c45150d4a 100644 --- a/test/valid-data/interface-extra-props/schema.json +++ b/test/valid-data/interface-extra-props/schema.json @@ -1,8 +1,12 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "required" + ], "properties": { "required": { "type": "string" @@ -11,9 +15,6 @@ "type": "number" } }, - "required": [ - "required" - ], "additionalProperties": { "type": [ "string", @@ -21,6 +22,5 @@ ] } } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/interface-multi/schema.json b/test/valid-data/interface-multi/schema.json index 6d583bebb..f8c67c1a1 100644 --- a/test/valid-data/interface-multi/schema.json +++ b/test/valid-data/interface-multi/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "subA", + "subB" + ], "properties": { "subA": { "$ref": "#/definitions/MySubObject" @@ -11,14 +16,14 @@ "$ref": "#/definitions/MySubObject" } }, - "required": [ - "subA", - "subB" - ], "additionalProperties": false }, "MySubObject": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -27,12 +32,7 @@ "type": "number" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/interface-property-dash/schema.json b/test/valid-data/interface-property-dash/schema.json index 8c7864402..8159effbe 100644 --- a/test/valid-data/interface-property-dash/schema.json +++ b/test/valid-data/interface-property-dash/schema.json @@ -1,8 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "with-dash", + "without", + "nodash" + ], "properties": { "with-dash": { "type": "string" @@ -14,13 +20,7 @@ "type": "string" } }, - "required": [ - "with-dash", - "without", - "nodash" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/interface-recursion/schema.json b/test/valid-data/interface-recursion/schema.json index ac19cdf1a..6eef63e57 100644 --- a/test/valid-data/interface-recursion/schema.json +++ b/test/valid-data/interface-recursion/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -11,12 +16,7 @@ "$ref": "#/definitions/MyObject" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/interface-single/schema.json b/test/valid-data/interface-single/schema.json index fdc0b58a0..d180d21c3 100644 --- a/test/valid-data/interface-single/schema.json +++ b/test/valid-data/interface-single/schema.json @@ -1,8 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "propA", + "propB", + "propC" + ], "properties": { "propA": { "type": "number" @@ -14,13 +20,7 @@ "type": "string" } }, - "required": [ - "propA", - "propB", - "propC" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/keyof-typeof-enum/schema.json b/test/valid-data/keyof-typeof-enum/schema.json index 3df47bb28..920c368fa 100644 --- a/test/valid-data/keyof-typeof-enum/schema.json +++ b/test/valid-data/keyof-typeof-enum/schema.json @@ -1,27 +1,27 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", "properties": { - "orientationName": { - "enum": [ - "Horizontal", - "Vertical" - ], - "type": "string" - }, "sizeName": { + "type": "string", "enum": [ "Small", "Medium", "Large" - ], - "type": "string" + ] + }, + "orientationName": { + "type": "string", + "enum": [ + "Horizontal", + "Vertical" + ] } }, - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/literal-array-type/schema.json b/test/valid-data/literal-array-type/schema.json index 7f97fdd8e..5fa4391b7 100644 --- a/test/valid-data/literal-array-type/schema.json +++ b/test/valid-data/literal-array-type/schema.json @@ -1,21 +1,21 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyType": { - "items": [ - { - "type": "string", - "const": "abc" - }, - { - "type": "string", - "const": "def" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "array", + "minItems": 2, + "maxItems": 2, + "items": [ + { + "type": "string", + "const": "abc" + }, + { + "type": "string", + "const": "def" + } + ] } - ], - "minItems": 2, - "maxItems": 2, - "type": "array" } - } } diff --git a/test/valid-data/literal-index-type/schema.json b/test/valid-data/literal-index-type/schema.json index 663b80fce..7e83bcf71 100644 --- a/test/valid-data/literal-index-type/schema.json +++ b/test/valid-data/literal-index-type/schema.json @@ -1,10 +1,13 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyType": { - "enum": ["abc", "def"], - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "string", + "enum": [ + "abc", + "def" + ] + } } - } } diff --git a/test/valid-data/literal-object-type/schema.json b/test/valid-data/literal-object-type/schema.json index 5047cfb67..b30cd29d7 100644 --- a/test/valid-data/literal-object-type/schema.json +++ b/test/valid-data/literal-object-type/schema.json @@ -1,17 +1,19 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyType": { - "additionalProperties": false, - "properties": { - "abc": { - "const": "def", - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "object", + "required": [ + "abc" + ], + "properties": { + "abc": { + "type": "string", + "const": "def" + } + }, + "additionalProperties": false } - }, - "required": ["abc"], - "type": "object" } - } } diff --git a/test/valid-data/multiple-roots1/schema.json b/test/valid-data/multiple-roots1/schema.json index 604f0aa43..7f1d03325 100644 --- a/test/valid-data/multiple-roots1/schema.json +++ b/test/valid-data/multiple-roots1/schema.json @@ -3,32 +3,38 @@ "definitions": { "MyObject1": { "type": "object", + "required": [ + "propA" + ], "properties": { "propA": { "$ref": "#/definitions/MyObject2" } }, - "required": ["propA"], "additionalProperties": false }, "MyObject2": { "type": "object", + "required": [ + "propB" + ], "properties": { "propB": { "type": "string" } }, - "required": ["propB"], "additionalProperties": false }, "MyObject3": { "type": "object", + "required": [ + "propC" + ], "properties": { "propC": { "type": "boolean" } }, - "required": ["propC"], "additionalProperties": false } } diff --git a/test/valid-data/multiple-roots2/schema/schema.json b/test/valid-data/multiple-roots2/schema/schema.json index 230e3eaed..04abfa5d1 100644 --- a/test/valid-data/multiple-roots2/schema/schema.json +++ b/test/valid-data/multiple-roots2/schema/schema.json @@ -1,31 +1,31 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { + "AssetType": { + "type": "string", + "enum": [ + "One", + "Two" + ] + }, + "MyAsset": { + "$ref": "#/definitions/Asset" + }, "Asset": { "type": "object", + "required": [ + "type" + ], "properties": { "type": { + "type": "string", "enum": [ "One", "Two" - ], - "type": "string" + ] } }, - "required": [ - "type" - ], "additionalProperties": false - }, - "AssetType": { - "enum": [ - "One", - "Two" - ], - "type": "string" - }, - "MyAsset": { - "$ref": "#/definitions/Asset" } } } diff --git a/test/valid-data/namespace-deep-1/schema.json b/test/valid-data/namespace-deep-1/schema.json index e2ac80cfa..df006e6c4 100644 --- a/test/valid-data/namespace-deep-1/schema.json +++ b/test/valid-data/namespace-deep-1/schema.json @@ -1,8 +1,15 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/RootNamespace.Def", "definitions": { "RootNamespace.Def": { "type": "object", + "required": [ + "nest", + "prev", + "propA", + "propB" + ], "properties": { "nest": { "$ref": "#/definitions/RootNamespace.Def" @@ -17,16 +24,14 @@ "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" } }, - "required": [ - "nest", - "prev", - "propA", - "propB" - ], "additionalProperties": false }, "RootNamespace.SubNamespace.HelperA": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -35,14 +40,14 @@ "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false }, "RootNamespace.SubNamespace.HelperB": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" @@ -51,12 +56,7 @@ "$ref": "#/definitions/RootNamespace.Def" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/RootNamespace.Def" + } } diff --git a/test/valid-data/namespace-deep-2/schema.json b/test/valid-data/namespace-deep-2/schema.json index b69ccf290..262f71b9f 100644 --- a/test/valid-data/namespace-deep-2/schema.json +++ b/test/valid-data/namespace-deep-2/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA", "definitions": { "RootNamespace.SubNamespace.HelperA": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -11,14 +16,14 @@ "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false }, "RootNamespace.SubNamespace.HelperB": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" @@ -27,14 +32,16 @@ "$ref": "#/definitions/RootNamespace.Def" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false }, "RootNamespace.Def": { "type": "object", + "required": [ + "nest", + "prev", + "propA", + "propB" + ], "properties": { "nest": { "$ref": "#/definitions/RootNamespace.Def" @@ -49,14 +56,7 @@ "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" } }, - "required": [ - "nest", - "prev", - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" + } } diff --git a/test/valid-data/namespace-deep-3/schema.json b/test/valid-data/namespace-deep-3/schema.json index 1af0aa481..4d40bc1b8 100644 --- a/test/valid-data/namespace-deep-3/schema.json +++ b/test/valid-data/namespace-deep-3/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB", "definitions": { "RootNamespace.SubNamespace.HelperB": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" @@ -11,14 +16,14 @@ "$ref": "#/definitions/RootNamespace.Def" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false }, "RootNamespace.SubNamespace.HelperA": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -27,14 +32,16 @@ "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false }, "RootNamespace.Def": { "type": "object", + "required": [ + "nest", + "prev", + "propA", + "propB" + ], "properties": { "nest": { "$ref": "#/definitions/RootNamespace.Def" @@ -49,14 +56,7 @@ "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" } }, - "required": [ - "nest", - "prev", - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" + } } diff --git a/test/valid-data/nullable-null/schema.json b/test/valid-data/nullable-null/schema.json index 449bc7927..4737af737 100644 --- a/test/valid-data/nullable-null/schema.json +++ b/test/valid-data/nullable-null/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", @@ -17,6 +18,5 @@ }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/object-literal-expression/schema.json b/test/valid-data/object-literal-expression/schema.json index 010f03e78..a6443b793 100644 --- a/test/valid-data/object-literal-expression/schema.json +++ b/test/valid-data/object-literal-expression/schema.json @@ -1,10 +1,13 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyType": { - "enum": ["x", "y"], - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "string", + "enum": [ + "x", + "y" + ] + } } - } } diff --git a/test/valid-data/simple-object/schema.json b/test/valid-data/simple-object/schema.json index 8c040562d..fbb74a5ed 100644 --- a/test/valid-data/simple-object/schema.json +++ b/test/valid-data/simple-object/schema.json @@ -1,8 +1,12 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/SimpleObject", "definitions": { "SimpleObject": { "type": "object", + "required": [ + "required" + ], "properties": { "required": { "type": "string" @@ -11,11 +15,7 @@ "type": "number" } }, - "required": [ - "required" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/SimpleObject" + } } diff --git a/test/valid-data/string-literals-inline/schema.json b/test/valid-data/string-literals-inline/schema.json index 40ed395ed..cf68d6c46 100644 --- a/test/valid-data/string-literals-inline/schema.json +++ b/test/valid-data/string-literals-inline/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "foo", + "bar" + ], "properties": { "foo": { "type": "string", @@ -16,12 +21,7 @@ "type": "string" } }, - "required": [ - "foo", - "bar" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/string-literals-null/schema.json b/test/valid-data/string-literals-null/schema.json index 58f3de93e..c802eb07d 100644 --- a/test/valid-data/string-literals-null/schema.json +++ b/test/valid-data/string-literals-null/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "enum1", + "enum2" + ], "properties": { "enum1": { "type": [ @@ -23,12 +28,7 @@ ] } }, - "required": [ - "enum1", - "enum2" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/string-literals/schema.json b/test/valid-data/string-literals/schema.json index 40ed395ed..cf68d6c46 100644 --- a/test/valid-data/string-literals/schema.json +++ b/test/valid-data/string-literals/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "foo", + "bar" + ], "properties": { "foo": { "type": "string", @@ -16,12 +21,7 @@ "type": "string" } }, - "required": [ - "foo", - "bar" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/structure-anonymous/schema.json b/test/valid-data/structure-anonymous/schema.json index afef27bde..76a83c273 100644 --- a/test/valid-data/structure-anonymous/schema.json +++ b/test/valid-data/structure-anonymous/schema.json @@ -1,11 +1,20 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "field" + ], "properties": { "field": { "type": "object", + "required": [ + "subfieldA", + "subfieldB", + "subfieldC" + ], "properties": { "subfieldA": { "type": "number" @@ -18,6 +27,9 @@ }, "subfieldC": { "type": "object", + "required": [ + "subsubfieldA" + ], "properties": { "subsubfieldA": { "type": "array", @@ -26,25 +38,13 @@ } } }, - "required": [ - "subsubfieldA" - ], "additionalProperties": false } }, - "required": [ - "subfieldA", - "subfieldB", - "subfieldC" - ], "additionalProperties": false } }, - "required": [ - "field" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/structure-extra-props/schema.json b/test/valid-data/structure-extra-props/schema.json index ab25a2a7c..b12a5eb5a 100644 --- a/test/valid-data/structure-extra-props/schema.json +++ b/test/valid-data/structure-extra-props/schema.json @@ -1,11 +1,18 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "structure" + ], "properties": { "structure": { "type": "object", + "required": [ + "required" + ], "properties": { "required": { "type": "string" @@ -14,9 +21,6 @@ "type": "number" } }, - "required": [ - "required" - ], "additionalProperties": { "type": [ "string", @@ -25,11 +29,7 @@ } } }, - "required": [ - "structure" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/structure-private/schema.json b/test/valid-data/structure-private/schema.json index afef27bde..76a83c273 100644 --- a/test/valid-data/structure-private/schema.json +++ b/test/valid-data/structure-private/schema.json @@ -1,11 +1,20 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "field" + ], "properties": { "field": { "type": "object", + "required": [ + "subfieldA", + "subfieldB", + "subfieldC" + ], "properties": { "subfieldA": { "type": "number" @@ -18,6 +27,9 @@ }, "subfieldC": { "type": "object", + "required": [ + "subsubfieldA" + ], "properties": { "subsubfieldA": { "type": "array", @@ -26,25 +38,13 @@ } } }, - "required": [ - "subsubfieldA" - ], "additionalProperties": false } }, - "required": [ - "subfieldA", - "subfieldB", - "subfieldC" - ], "additionalProperties": false } }, - "required": [ - "field" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/structure-recursion/schema.json b/test/valid-data/structure-recursion/schema.json index 5991e4f83..81ded7356 100644 --- a/test/valid-data/structure-recursion/schema.json +++ b/test/valid-data/structure-recursion/schema.json @@ -1,20 +1,25 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "sub" + ], "properties": { "sub": { "$ref": "#/definitions/MySubObject" } }, - "required": [ - "sub" - ], "additionalProperties": false }, "MySubObject": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -23,12 +28,7 @@ "$ref": "#/definitions/MySubObject" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/symbol/schema.json b/test/valid-data/symbol/schema.json index 2d02aa333..3e69782af 100644 --- a/test/valid-data/symbol/schema.json +++ b/test/valid-data/symbol/schema.json @@ -1,14 +1,16 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "properties": { - "foo": {} - }, - "required": ["foo"], - "type": "object" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "foo" + ], + "properties": { + "foo": {} + }, + "additionalProperties": false + } } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/type-aliases-anonymous/schema.json b/test/valid-data/type-aliases-anonymous/schema.json index 135a2c470..9fbb78046 100644 --- a/test/valid-data/type-aliases-anonymous/schema.json +++ b/test/valid-data/type-aliases-anonymous/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "export", + "private" + ], "properties": { "export": { "$ref": "#/definitions/MyExportString" @@ -11,15 +16,10 @@ "type": "string" } }, - "required": [ - "export", - "private" - ], "additionalProperties": false }, "MyExportString": { "type": "string" } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-aliases-local-namespace/schema.json b/test/valid-data/type-aliases-local-namespace/schema.json index 23c28338c..ec6ead6d2 100644 --- a/test/valid-data/type-aliases-local-namespace/schema.json +++ b/test/valid-data/type-aliases-local-namespace/schema.json @@ -1,40 +1,40 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", - "additionalProperties": false, + "required": [ + "d" + ], "properties": { "d": { "$ref": "#/definitions/C.C" } }, - "required": [ - "d" - ] + "additionalProperties": false }, "C.C": { "type": "object", - "additionalProperties": false, + "required": [ + "c" + ], "properties": { "c": { "$ref": "#/definitions/B.B" } }, - "required": [ - "c" - ] + "additionalProperties": false }, "B.B": { "type": "object", - "properties": { - "b": {} - }, "required": [ "b" ], + "properties": { + "b": {} + }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-aliases-mixed/schema.json b/test/valid-data/type-aliases-mixed/schema.json index 46ff014d5..784e3a70d 100644 --- a/test/valid-data/type-aliases-mixed/schema.json +++ b/test/valid-data/type-aliases-mixed/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "primitive", + "object" + ], "properties": { "primitive": { "$ref": "#/definitions/MyString" @@ -11,10 +16,6 @@ "$ref": "#/definitions/MySubObject" } }, - "required": [ - "primitive", - "object" - ], "additionalProperties": false }, "MyString": { @@ -22,6 +23,10 @@ }, "MySubObject": { "type": "object", + "required": [ + "propA", + "propB" + ], "properties": { "propA": { "type": "number" @@ -30,12 +35,7 @@ "type": "number" } }, - "required": [ - "propA", - "propB" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-aliases-object/schema.json b/test/valid-data/type-aliases-object/schema.json index fe93ff593..35668b3bd 100644 --- a/test/valid-data/type-aliases-object/schema.json +++ b/test/valid-data/type-aliases-object/schema.json @@ -1,11 +1,16 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyAlias", "definitions": { "MyAlias": { "$ref": "#/definitions/MyObject" }, "MyObject": { "type": "object", + "required": [ + "number", + "string" + ], "properties": { "number": { "type": "number" @@ -14,12 +19,7 @@ "type": "string" } }, - "required": [ - "number", - "string" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyAlias" + } } diff --git a/test/valid-data/type-aliases-primitive-with-id/schema.json b/test/valid-data/type-aliases-primitive-with-id/schema.json index 1cc5c111a..718a42016 100644 --- a/test/valid-data/type-aliases-primitive-with-id/schema.json +++ b/test/valid-data/type-aliases-primitive-with-id/schema.json @@ -1,10 +1,10 @@ { "$id": "testId", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyString", "definitions": { "MyString": { "type": "string" } - }, - "$ref": "#/definitions/MyString" + } } diff --git a/test/valid-data/type-aliases-primitive/schema.json b/test/valid-data/type-aliases-primitive/schema.json index d38e7834f..b7e5cfd7d 100644 --- a/test/valid-data/type-aliases-primitive/schema.json +++ b/test/valid-data/type-aliases-primitive/schema.json @@ -1,9 +1,9 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyString", "definitions": { "MyString": { "type": "string" } - }, - "$ref": "#/definitions/MyString" + } } diff --git a/test/valid-data/type-aliases-recursive-anonymous/schema.json b/test/valid-data/type-aliases-recursive-anonymous/schema.json index f6750bb29..9b755300f 100644 --- a/test/valid-data/type-aliases-recursive-anonymous/schema.json +++ b/test/valid-data/type-aliases-recursive-anonymous/schema.json @@ -4,34 +4,18 @@ "definitions": { "MyAlias": { "type": "object", - "properties": { - "alias": { - "$ref": "#/definitions/MyAlias" - }, - "self": { - "$ref": "#/definitions/interface-425636228-0-62-425636228-0-96" - } - }, "required": [ "alias", "self" ], - "additionalProperties": false - }, - "interface-425636228-0-62-425636228-0-96": { - "type": "object", "properties": { "alias": { "$ref": "#/definitions/MyAlias" }, "self": { - "$ref": "#/definitions/interface-425636228-0-62-425636228-0-96" + "$ref": "#/definitions/undefined" } }, - "required": [ - "alias", - "self" - ], "additionalProperties": false } } diff --git a/test/valid-data/type-aliases-recursive-export/schema.json b/test/valid-data/type-aliases-recursive-export/schema.json index 5c3c22011..6f7fac1e4 100644 --- a/test/valid-data/type-aliases-recursive-export/schema.json +++ b/test/valid-data/type-aliases-recursive-export/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "alias", + "self" + ], "properties": { "alias": { "$ref": "#/definitions/MyAlias" @@ -11,15 +16,10 @@ "$ref": "#/definitions/MyObject" } }, - "required": [ - "alias", - "self" - ], "additionalProperties": false }, "MyAlias": { "$ref": "#/definitions/MyObject" } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-aliases-recursive-generics-anonymous/schema.json b/test/valid-data/type-aliases-recursive-generics-anonymous/schema.json index 4da6c447c..8a5b0c1a4 100644 --- a/test/valid-data/type-aliases-recursive-generics-anonymous/schema.json +++ b/test/valid-data/type-aliases-recursive-generics-anonymous/schema.json @@ -1,21 +1,21 @@ { - "$ref": "#/definitions/MyAlias", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyAlias", "definitions": { "MyAlias": { - "additionalProperties": false, + "type": "object", + "required": [ + "a" + ], "properties": { "a": { + "type": "object", "additionalProperties": { "$ref": "#/definitions/MyAlias" - }, - "type": "object" + } } }, - "required": [ - "a" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-aliases-recursive-generics-export/schema.json b/test/valid-data/type-aliases-recursive-generics-export/schema.json index 85658e494..99a47dacd 100644 --- a/test/valid-data/type-aliases-recursive-generics-export/schema.json +++ b/test/valid-data/type-aliases-recursive-generics-export/schema.json @@ -1,22 +1,24 @@ { - "$ref": "#/definitions/MyAlias", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "Map": { - "additionalProperties": { - "$ref": "#/definitions/MyAlias" - }, - "type": "object" - }, - "MyAlias": { - "additionalProperties": false, - "properties": { - "a": { - "$ref": "#/definitions/Map%3CMyAlias%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyAlias", + "definitions": { + "MyAlias": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { + "$ref": "#/definitions/Map%3CMyAlias%3E" + } + }, + "additionalProperties": false + }, + "Map": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/MyAlias" + } } - }, - "required": ["a"], - "type": "object" } - } } diff --git a/test/valid-data/type-aliases-tuple-empty/schema.json b/test/valid-data/type-aliases-tuple-empty/schema.json index d2e55d7d7..dc1116706 100644 --- a/test/valid-data/type-aliases-tuple-empty/schema.json +++ b/test/valid-data/type-aliases-tuple-empty/schema.json @@ -1,11 +1,11 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyTuple", "definitions": { "MyTuple": { "type": "array", "minItems": 0, "maxItems": 0 } - }, - "$ref": "#/definitions/MyTuple" + } } diff --git a/test/valid-data/type-aliases-tuple-only-rest/schema.json b/test/valid-data/type-aliases-tuple-only-rest/schema.json index 8646c1008..02d87e31c 100644 --- a/test/valid-data/type-aliases-tuple-only-rest/schema.json +++ b/test/valid-data/type-aliases-tuple-only-rest/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyTuple", "definitions": { "MyTuple": { "type": "array", @@ -8,6 +9,5 @@ "type": "string" } } - }, - "$ref": "#/definitions/MyTuple" + } } diff --git a/test/valid-data/type-aliases-tuple-optional-items/schema.json b/test/valid-data/type-aliases-tuple-optional-items/schema.json index 9b4e18f77..44887cd65 100644 --- a/test/valid-data/type-aliases-tuple-optional-items/schema.json +++ b/test/valid-data/type-aliases-tuple-optional-items/schema.json @@ -1,9 +1,11 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyTuple", "definitions": { "MyTuple": { "type": "array", "minItems": 2, + "maxItems": 4, "items": [ { "type": "string" @@ -17,9 +19,7 @@ { "type": "number" } - ], - "maxItems": 4 + ] } - }, - "$ref": "#/definitions/MyTuple" + } } diff --git a/test/valid-data/type-aliases-tuple-rest/schema.json b/test/valid-data/type-aliases-tuple-rest/schema.json index 266013828..b95e27d27 100644 --- a/test/valid-data/type-aliases-tuple-rest/schema.json +++ b/test/valid-data/type-aliases-tuple-rest/schema.json @@ -1,9 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyTuple", "definitions": { "MyTuple": { "type": "array", "minItems": 2, + "additionalItems": { + "type": "number" + }, "items": [ { "type": "string" @@ -14,11 +18,7 @@ { "type": "string" } - ], - "additionalItems": { - "type": "number" - } + ] } - }, - "$ref": "#/definitions/MyTuple" + } } diff --git a/test/valid-data/type-aliases-tuple/schema.json b/test/valid-data/type-aliases-tuple/schema.json index aac933fff..1204715cf 100644 --- a/test/valid-data/type-aliases-tuple/schema.json +++ b/test/valid-data/type-aliases-tuple/schema.json @@ -1,9 +1,11 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyTuple", "definitions": { "MyTuple": { "type": "array", "minItems": 2, + "maxItems": 2, "items": [ { "type": "string" @@ -11,9 +13,7 @@ { "type": "number" } - ], - "maxItems": 2 + ] } - }, - "$ref": "#/definitions/MyTuple" + } } diff --git a/test/valid-data/type-aliases-union/schema.json b/test/valid-data/type-aliases-union/schema.json index 5dda0cfb8..3f9e10a60 100644 --- a/test/valid-data/type-aliases-union/schema.json +++ b/test/valid-data/type-aliases-union/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyUnion", "definitions": { "MyUnion": { "type": "array", @@ -16,6 +17,9 @@ }, "MyObject": { "type": "object", + "required": [ + "array" + ], "properties": { "array": { "type": "array", @@ -27,11 +31,7 @@ } } }, - "required": [ - "array" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyUnion" + } } diff --git a/test/valid-data/type-conditional-enum/schema.json b/test/valid-data/type-conditional-enum/schema.json index ace0a4a98..2f77f484b 100644 --- a/test/valid-data/type-conditional-enum/schema.json +++ b/test/valid-data/type-conditional-enum/schema.json @@ -1,22 +1,22 @@ { - "$ref": "#/definitions/IParameter", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/IParameter", "definitions": { "IParameter": { - "additionalProperties": false, + "type": "object", + "required": [ + "type" + ], "properties": { "type": { + "type": "string", "enum": [ "string", "date" - ], - "type": "string" + ] } }, - "required": [ - "type" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-exclude-complex/schema.json b/test/valid-data/type-conditional-exclude-complex/schema.json index ff049f392..7fb9f8c8a 100644 --- a/test/valid-data/type-conditional-exclude-complex/schema.json +++ b/test/valid-data/type-conditional-exclude-complex/schema.json @@ -1,9 +1,9 @@ { - "$ref": "#/definitions/BaseAxisNoSignals", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/BaseAxisNoSignals", "definitions": { "BaseAxisNoSignals": { - "additionalProperties": false, + "type": "object", "properties": { "minExtent": { "type": "number" @@ -12,7 +12,7 @@ "type": "string" } }, - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-exclude-narrowing/schema.json b/test/valid-data/type-conditional-exclude-narrowing/schema.json index aaaff37cf..c2d3fa25a 100644 --- a/test/valid-data/type-conditional-exclude-narrowing/schema.json +++ b/test/valid-data/type-conditional-exclude-narrowing/schema.json @@ -1,73 +1,61 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { - "Align": { - "enum": [ - "left", - "right", - "center" - ], - "type": "string" - }, - "BadPrimitives": { - "type": [ - "null", - "boolean" - ] - }, - "GoodPrimitives": { - "type": [ - "string", - "number" - ] - }, "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "textWithoutAlign", + "textWithoutNumbers", + "allPrims", + "goodPrims", + "badPrims", + "stringOrNull" + ], "properties": { - "allPrims": { - "$ref": "#/definitions/Primitives" - }, - "badPrims": { - "$ref": "#/definitions/BadPrimitives" - }, - "goodPrims": { - "$ref": "#/definitions/GoodPrimitives" - }, - "stringOrNull": { - "type": [ - "string", - "null" - ] - }, "textWithoutAlign": { - "additionalProperties": false, + "type": "object", "properties": { "align": { "type": "number" } }, - "type": "object" + "additionalProperties": false }, "textWithoutNumbers": { - "additionalProperties": false, + "type": "object", "properties": { "align": { "$ref": "#/definitions/Align" } }, - "type": "object" + "additionalProperties": false + }, + "allPrims": { + "$ref": "#/definitions/Primitives" + }, + "goodPrims": { + "$ref": "#/definitions/GoodPrimitives" + }, + "badPrims": { + "$ref": "#/definitions/BadPrimitives" + }, + "stringOrNull": { + "type": [ + "string", + "null" + ] } }, - "required": [ - "textWithoutAlign", - "textWithoutNumbers", - "allPrims", - "goodPrims", - "badPrims", - "stringOrNull" - ], - "type": "object" + "additionalProperties": false + }, + "Align": { + "type": "string", + "enum": [ + "left", + "right", + "center" + ] }, "Primitives": { "anyOf": [ @@ -78,6 +66,18 @@ "$ref": "#/definitions/BadPrimitives" } ] + }, + "GoodPrimitives": { + "type": [ + "string", + "number" + ] + }, + "BadPrimitives": { + "type": [ + "null", + "boolean" + ] } } } diff --git a/test/valid-data/type-conditional-exclude/schema.json b/test/valid-data/type-conditional-exclude/schema.json index 9516d8c39..b577a74bb 100644 --- a/test/valid-data/type-conditional-exclude/schema.json +++ b/test/valid-data/type-conditional-exclude/schema.json @@ -1,10 +1,19 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "primitives", + "noNumber", + "noNumberAndBoolean", + "noStringAndNumber" + ], "properties": { + "primitives": { + "$ref": "#/definitions/Primitives" + }, "noNumber": { "type": [ "string", @@ -16,18 +25,9 @@ }, "noStringAndNumber": { "type": "boolean" - }, - "primitives": { - "$ref": "#/definitions/Primitives" } }, - "required": [ - "primitives", - "noNumber", - "noNumberAndBoolean", - "noStringAndNumber" - ], - "type": "object" + "additionalProperties": false }, "Primitives": { "type": [ diff --git a/test/valid-data/type-conditional-inheritance/schema.json b/test/valid-data/type-conditional-inheritance/schema.json index edb8256d5..d02d266c7 100644 --- a/test/valid-data/type-conditional-inheritance/schema.json +++ b/test/valid-data/type-conditional-inheritance/schema.json @@ -1,62 +1,62 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "Map": { - "const": "a", - "type": "string" - }, - "Map": { - "const": "a", - "type": "string" - }, - "Map": { - "const": "a", - "type": "string" - }, - "Map": { - "const": "d", - "type": "string" - }, - "Map": { - "const": "d", - "type": "string" - }, - "Map": { - "const": "f", - "type": "string" - }, - "MyObject": { - "additionalProperties": false, - "properties": { - "a": { - "$ref": "#/definitions/Map%3CA%3E" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "a", + "b", + "c", + "d", + "e", + "f" + ], + "properties": { + "a": { + "$ref": "#/definitions/Map%3CA%3E" + }, + "b": { + "$ref": "#/definitions/Map%3CB%3E" + }, + "c": { + "$ref": "#/definitions/Map%3CC%3E" + }, + "d": { + "$ref": "#/definitions/Map%3CD%3E" + }, + "e": { + "$ref": "#/definitions/Map%3CE%3E" + }, + "f": { + "$ref": "#/definitions/Map%3CF%3E" + } + }, + "additionalProperties": false }, - "b": { - "$ref": "#/definitions/Map%3CB%3E" + "Map": { + "type": "string", + "const": "a" }, - "c": { - "$ref": "#/definitions/Map%3CC%3E" + "Map": { + "type": "string", + "const": "a" }, - "d": { - "$ref": "#/definitions/Map%3CD%3E" + "Map": { + "type": "string", + "const": "a" }, - "e": { - "$ref": "#/definitions/Map%3CE%3E" + "Map": { + "type": "string", + "const": "d" }, - "f": { - "$ref": "#/definitions/Map%3CF%3E" + "Map": { + "type": "string", + "const": "d" + }, + "Map": { + "type": "string", + "const": "f" } - }, - "required": [ - "a", - "b", - "c", - "d", - "e", - "f" - ], - "type": "object" } - } } diff --git a/test/valid-data/type-conditional-intersection/schema.json b/test/valid-data/type-conditional-intersection/schema.json index e78c3dda2..0399fff84 100644 --- a/test/valid-data/type-conditional-intersection/schema.json +++ b/test/valid-data/type-conditional-intersection/schema.json @@ -1,44 +1,44 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "b", + "c", + "d", + "e", + "f" + ], "properties": { "a": { - "const": "a", - "type": "string" + "type": "string", + "const": "a" }, "b": { - "const": "b", - "type": "string" + "type": "string", + "const": "b" }, "c": { - "const": "a and b", - "type": "string" + "type": "string", + "const": "a and b" }, "d": { - "const": "D", - "type": "string" + "type": "string", + "const": "D" }, "e": { - "const": "a and b", - "type": "string" + "type": "string", + "const": "a and b" }, "f": { - "const": "D", - "type": "string" + "type": "string", + "const": "D" } }, - "required": [ - "a", - "b", - "c", - "d", - "e", - "f" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-jsdoc/schema.json b/test/valid-data/type-conditional-jsdoc/schema.json index e3efec326..8aaf4a33d 100644 --- a/test/valid-data/type-conditional-jsdoc/schema.json +++ b/test/valid-data/type-conditional-jsdoc/schema.json @@ -1,77 +1,77 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h", + "i" + ], "properties": { "a": { - "description": "Number or string", - "pattern": "foo", "type": [ "number", "string" - ] + ], + "description": "Number or string", + "pattern": "foo" }, "b": { - "description": "Description of b", - "pattern": "foo", "type": [ "number", "string" - ] + ], + "description": "Description of b", + "pattern": "foo" }, "c": { "type": "number" }, "d": { + "type": "number", "description": "No string", - "pattern": "bar", - "type": "number" + "pattern": "bar" }, "e": { - "description": "Description of e", - "type": "number" + "type": "number", + "description": "Description of e" }, "f": { + "type": "number", "description": "Description of f", - "pattern": "bar", - "type": "number" + "pattern": "bar" }, "g": { - "description": "Number or string", - "pattern": "foo", "type": [ "number", "string" - ] + ], + "description": "Number or string", + "pattern": "foo" }, "h": { + "type": "string", "description": "Number or string", - "pattern": "foo", - "type": "string" + "pattern": "foo" }, "i": { - "description": "Number or string", - "pattern": "foo", "type": [ "number", "string" - ] + ], + "description": "Number or string", + "pattern": "foo" } }, - "required": [ - "a", - "b", - "c", - "d", - "e", - "f", - "g", - "h", - "i" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-narrowing/schema.json b/test/valid-data/type-conditional-narrowing/schema.json index 46fa6f6b1..ca2533529 100644 --- a/test/valid-data/type-conditional-narrowing/schema.json +++ b/test/valid-data/type-conditional-narrowing/schema.json @@ -1,49 +1,49 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "string", + "stringOrNumber" + ], "properties": { - "number": { - "const": "number", - "type": "string" - }, - "numberOrUnknown": { - "enum": [ - "number", - "unknown" - ], - "type": "string" - }, "string": { - "const": "string", - "type": "string" + "type": "string", + "const": "string" + }, + "number": { + "type": "string", + "const": "number" }, "stringOrNumber": { + "type": "string", "enum": [ "string", "number" - ], - "type": "string" + ] + }, + "unknown": { + "type": "string", + "const": "unknown" }, "stringOrUnknown": { + "type": "string", "enum": [ "string", "unknown" - ], - "type": "string" + ] }, - "unknown": { - "const": "unknown", - "type": "string" + "numberOrUnknown": { + "type": "string", + "enum": [ + "number", + "unknown" + ] } }, - "required": [ - "string", - "stringOrNumber" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-omit/schema.json b/test/valid-data/type-conditional-omit/schema.json index 719f8c4db..5ba04ab42 100644 --- a/test/valid-data/type-conditional-omit/schema.json +++ b/test/valid-data/type-conditional-omit/schema.json @@ -1,9 +1,13 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "c" + ], "properties": { "a": { "type": "string" @@ -12,11 +16,7 @@ "type": "boolean" } }, - "required": [ - "a", - "c" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-simple/schema.json b/test/valid-data/type-conditional-simple/schema.json index 902f6aa43..c1cf189d4 100644 --- a/test/valid-data/type-conditional-simple/schema.json +++ b/test/valid-data/type-conditional-simple/schema.json @@ -1,29 +1,29 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "b", + "c" + ], "properties": { "a": { - "const": "string", - "type": "string" + "type": "string", + "const": "string" }, "b": { - "const": "number", - "type": "string" + "type": "string", + "const": "number" }, "c": { - "const": "unknown", - "type": "string" + "type": "string", + "const": "unknown" } }, - "required": [ - "a", - "b", - "c" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-conditional-union/schema.json b/test/valid-data/type-conditional-union/schema.json index b03f9a4d1..b795b7c85 100644 --- a/test/valid-data/type-conditional-union/schema.json +++ b/test/valid-data/type-conditional-union/schema.json @@ -1,34 +1,34 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "b", + "c", + "d" + ], "properties": { "a": { - "const": "a or b", - "type": "string" + "type": "string", + "const": "a or b" }, "b": { - "const": "a or b", - "type": "string" + "type": "string", + "const": "a or b" }, "c": { - "const": "unknown", - "type": "string" + "type": "string", + "const": "unknown" }, "d": { - "const": "a or b", - "type": "string" + "type": "string", + "const": "a or b" } }, - "required": [ - "a", - "b", - "c", - "d" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-date/schema.json b/test/valid-data/type-date/schema.json index 0900b05af..7801efb10 100644 --- a/test/valid-data/type-date/schema.json +++ b/test/valid-data/type-date/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "date", + "dateAlias" + ], "properties": { "date": { "type": "string", @@ -13,12 +18,7 @@ "format": "date-time" } }, - "required": [ - "date", - "dateAlias" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-extend-circular/schema.json b/test/valid-data/type-extend-circular/schema.json index b5acdc296..9fe1f75ca 100644 --- a/test/valid-data/type-extend-circular/schema.json +++ b/test/valid-data/type-extend-circular/schema.json @@ -1,29 +1,34 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "properties": { - "foo": { - "$ref": "#/definitions/MyObject" - } - }, - "required": ["foo"], - "type": "object" - }, - "MyType": { - "additionalProperties": false, - "properties": { - "bar": { - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "object", + "required": [ + "bar", + "foo" + ], + "properties": { + "foo": { + "$ref": "#/definitions/MyObject" + }, + "bar": { + "type": "string" + } + }, + "additionalProperties": false }, - "foo": { - "$ref": "#/definitions/MyObject" + "MyObject": { + "type": "object", + "required": [ + "foo" + ], + "properties": { + "foo": { + "$ref": "#/definitions/MyObject" + } + }, + "additionalProperties": false } - }, - "required": ["bar", "foo"], - "type": "object" } - } } diff --git a/test/valid-data/type-extend/schema.json b/test/valid-data/type-extend/schema.json index bdfcbfa9e..70f259ae2 100644 --- a/test/valid-data/type-extend/schema.json +++ b/test/valid-data/type-extend/schema.json @@ -1,17 +1,19 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "value" + ], "properties": { "value": { - "const": "foo", - "type": "string" + "type": "string", + "const": "foo" } }, - "required": ["value"], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-indexed-access-keyof/schema.json b/test/valid-data/type-indexed-access-keyof/schema.json index 8440a00af..5fb5608cf 100644 --- a/test/valid-data/type-indexed-access-keyof/schema.json +++ b/test/valid-data/type-indexed-access-keyof/schema.json @@ -1,9 +1,13 @@ { - "$ref": "#/definitions/MyType", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { - "additionalProperties": false, + "type": "object", + "required": [ + "types1", + "types2" + ], "properties": { "types1": { "type": "string" @@ -15,11 +19,7 @@ ] } }, - "required": [ - "types1", - "types2" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-indexed-access-object-1/schema.json b/test/valid-data/type-indexed-access-object-1/schema.json index 7bfe91e0c..4b2a54905 100644 --- a/test/valid-data/type-indexed-access-object-1/schema.json +++ b/test/valid-data/type-indexed-access-object-1/schema.json @@ -1,10 +1,10 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "string", "const": "foo" } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-indexed-access-object-2/schema.json b/test/valid-data/type-indexed-access-object-2/schema.json index b05db29b7..8dedff8de 100644 --- a/test/valid-data/type-indexed-access-object-2/schema.json +++ b/test/valid-data/type-indexed-access-object-2/schema.json @@ -1,12 +1,12 @@ { - "$ref": "#/definitions/MyType", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "anyOf": [ { - "const": "bar", - "type": "string" + "type": "string", + "const": "bar" }, { "not": {} diff --git a/test/valid-data/type-indexed-access-tuple-1/schema.json b/test/valid-data/type-indexed-access-tuple-1/schema.json index 7bfe91e0c..4b2a54905 100644 --- a/test/valid-data/type-indexed-access-tuple-1/schema.json +++ b/test/valid-data/type-indexed-access-tuple-1/schema.json @@ -1,10 +1,10 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "string", "const": "foo" } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-indexed-access-tuple-2/schema.json b/test/valid-data/type-indexed-access-tuple-2/schema.json index 5ce5203e0..330de0448 100644 --- a/test/valid-data/type-indexed-access-tuple-2/schema.json +++ b/test/valid-data/type-indexed-access-tuple-2/schema.json @@ -1,10 +1,10 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "string", "const": "bar" } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-indexed-access-tuple-union/schema.json b/test/valid-data/type-indexed-access-tuple-union/schema.json index 7767a04ba..27e1a1363 100644 --- a/test/valid-data/type-indexed-access-tuple-union/schema.json +++ b/test/valid-data/type-indexed-access-tuple-union/schema.json @@ -1,14 +1,14 @@ { - "$ref": "#/definitions/FormLayout", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/FormLayout", "definitions": { "FormLayout": { + "type": "string", "enum": [ "horizontal", "inline", "vertical" - ], - "type": "string" + ] } } } diff --git a/test/valid-data/type-indexed-access-type-union/schema.json b/test/valid-data/type-indexed-access-type-union/schema.json index 523197eeb..8055edcde 100644 --- a/test/valid-data/type-indexed-access-type-union/schema.json +++ b/test/valid-data/type-indexed-access-type-union/schema.json @@ -1,15 +1,18 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyType": { - "additionalProperties": false, - "properties": { - "foo": { - "type": ["number", "boolean"] + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "object", + "properties": { + "foo": { + "type": [ + "number", + "boolean" + ] + } + }, + "additionalProperties": false } - }, - "type": "object" } - } } diff --git a/test/valid-data/type-intersection-additional-props/schema.json b/test/valid-data/type-intersection-additional-props/schema.json index c1896e2dc..b3dd748e0 100644 --- a/test/valid-data/type-intersection-additional-props/schema.json +++ b/test/valid-data/type-intersection-additional-props/schema.json @@ -1,11 +1,23 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "value" + ], "properties": { "value": { "type": "object", + "required": [ + "bar" + ], + "properties": { + "bar": { + "type": "number" + } + }, "additionalProperties": { "anyOf": [ { @@ -18,34 +30,22 @@ ] } ] - }, - "properties": { - "bar": { - "type": "number" - } - }, - "required": [ - "bar" - ] + } } }, - "required": [ - "value" - ], "additionalProperties": false }, "A": { "type": "object", + "required": [ + "bar" + ], "properties": { "bar": { "type": "number" } }, - "required": [ - "bar" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-intersection-conflict/schema.json b/test/valid-data/type-intersection-conflict/schema.json index 9a974bbf1..2ce57a953 100644 --- a/test/valid-data/type-intersection-conflict/schema.json +++ b/test/valid-data/type-intersection-conflict/schema.json @@ -1,19 +1,19 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "flag" + ], "properties": { "flag": { "type": "boolean", "const": true } }, - "required": [ - "flag" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-intersection-partial-conflict/schema.json b/test/valid-data/type-intersection-partial-conflict/schema.json index 6fcffbef6..935504241 100644 --- a/test/valid-data/type-intersection-partial-conflict/schema.json +++ b/test/valid-data/type-intersection-partial-conflict/schema.json @@ -3,15 +3,17 @@ "$ref": "#/definitions/MyType", "definitions": { "MyType": { - "additionalProperties": false, + "type": "object", + "required": [ + "a" + ], "properties": { "a": { - "const": "hello", - "type": "string" + "type": "string", + "const": "hello" } }, - "required": ["a"], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-intersection-union/schema.json b/test/valid-data/type-intersection-union/schema.json index 838967016..64b3f44e0 100644 --- a/test/valid-data/type-intersection-union/schema.json +++ b/test/valid-data/type-intersection-union/schema.json @@ -1,88 +1,88 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "anyOf": [ { - "additionalProperties": false, - "properties": { - "a": { - "type": "number" - }, - "b": { - "type": "number" - }, - "d": { - "type": "number" - } - }, + "type": "object", "required": [ "a", "b", "d" ], - "type": "object" - }, - { - "additionalProperties": false, "properties": { - "a": { + "d": { "type": "number" }, - "b": { + "a": { "type": "number" }, - "e": { + "b": { "type": "number" } }, + "additionalProperties": false + }, + { + "type": "object", "required": [ "a", "b", "e" ], - "type": "object" - }, - { - "additionalProperties": false, "properties": { - "a": { + "e": { "type": "number" }, - "c": { + "a": { "type": "number" }, - "d": { + "b": { "type": "number" } }, + "additionalProperties": false + }, + { + "type": "object", "required": [ "a", "c", "d" ], - "type": "object" - }, - { - "additionalProperties": false, "properties": { - "a": { + "d": { "type": "number" }, - "c": { + "a": { "type": "number" }, - "e": { + "c": { "type": "number" } }, + "additionalProperties": false + }, + { + "type": "object", "required": [ "a", "c", "e" ], - "type": "object" + "properties": { + "e": { + "type": "number" + }, + "a": { + "type": "number" + }, + "c": { + "type": "number" + } + }, + "additionalProperties": false } ] } diff --git a/test/valid-data/type-intersection/schema.json b/test/valid-data/type-intersection/schema.json index 0d60610a9..d78c19347 100644 --- a/test/valid-data/type-intersection/schema.json +++ b/test/valid-data/type-intersection/schema.json @@ -1,12 +1,20 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "value" + ], "properties": { "value": { "type": "object", - "additionalProperties": false, + "required": [ + "foo", + "value1", + "value2" + ], "properties": { "foo": { "$ref": "#/definitions/Type3" @@ -18,30 +26,22 @@ "type": "string" } }, - "required": [ - "foo", - "value1", - "value2" - ] + "additionalProperties": false } }, - "required": [ - "value" - ], "additionalProperties": false }, "Type3": { "type": "object", + "required": [ + "value3" + ], "properties": { "value3": { "type": "number" } }, - "required": [ - "value3" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-keyof-object-function/schema.json b/test/valid-data/type-keyof-object-function/schema.json index 36ee24f12..f73000707 100644 --- a/test/valid-data/type-keyof-object-function/schema.json +++ b/test/valid-data/type-keyof-object-function/schema.json @@ -1,10 +1,10 @@ { - "$ref": "#/definitions/MyType", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { - "MyType": { - "const": "key", - "type": "string" - } + "MyType": { + "type": "string", + "const": "key" + } } } diff --git a/test/valid-data/type-keyof-object/schema.json b/test/valid-data/type-keyof-object/schema.json index 1314af9ff..10c126c47 100644 --- a/test/valid-data/type-keyof-object/schema.json +++ b/test/valid-data/type-keyof-object/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "string", @@ -8,6 +9,5 @@ "bar" ] } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-keyof-tuple/schema.json b/test/valid-data/type-keyof-tuple/schema.json index d6d574fc7..3a357768d 100644 --- a/test/valid-data/type-keyof-tuple/schema.json +++ b/test/valid-data/type-keyof-tuple/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "number", @@ -8,6 +9,5 @@ 1 ] } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-mapped-additional-props/schema.json b/test/valid-data/type-mapped-additional-props/schema.json index 153e1fd66..121a479c8 100644 --- a/test/valid-data/type-mapped-additional-props/schema.json +++ b/test/valid-data/type-mapped-additional-props/schema.json @@ -1,18 +1,18 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "$ref": "#/definitions/WithNumbers%3CTest%3E" - }, - "WithNumbers": { - "additionalProperties": { - "type": [ - "string", - "number" - ] - }, - "type": "object" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "$ref": "#/definitions/WithNumbers%3CTest%3E" + }, + "WithNumbers": { + "type": "object", + "additionalProperties": { + "type": [ + "string", + "number" + ] + } + } } - } } diff --git a/test/valid-data/type-mapped-array/schema.json b/test/valid-data/type-mapped-array/schema.json index d648d0a40..2fae8aefb 100644 --- a/test/valid-data/type-mapped-array/schema.json +++ b/test/valid-data/type-mapped-array/schema.json @@ -1,15 +1,15 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { + "type": "array", "items": { "type": [ "string", "number" ] - }, - "type": "array" + } } } } diff --git a/test/valid-data/type-mapped-double-exclude/schema.json b/test/valid-data/type-mapped-double-exclude/schema.json index 7260e4cd2..73d9b41a6 100644 --- a/test/valid-data/type-mapped-double-exclude/schema.json +++ b/test/valid-data/type-mapped-double-exclude/schema.json @@ -1,35 +1,37 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "properties": { - "bar": { - "anyOf": [ - { - "type": "number", - "description": "Bar" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "foo" + ], + "properties": { + "foo": { + "anyOf": [ + { + "type": "number", + "description": "Foo" + }, + { + "type": "null" + } + ] + }, + "bar": { + "anyOf": [ + { + "type": "number", + "description": "Bar" + }, + { + "type": "null" + } + ] + } }, - { - "type": "null" - } - ] - }, - "foo": { - "anyOf": [ - { - "description": "Foo", - "type": "number" - }, - { - "type": "null" - } - ] + "additionalProperties": false } - }, - "required": ["foo"], - "type": "object" } - } } diff --git a/test/valid-data/type-mapped-enum-null/schema.json b/test/valid-data/type-mapped-enum-null/schema.json index 3c718952b..c87291663 100644 --- a/test/valid-data/type-mapped-enum-null/schema.json +++ b/test/valid-data/type-mapped-enum-null/schema.json @@ -1,10 +1,10 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-enum-optional/schema.json b/test/valid-data/type-mapped-enum-optional/schema.json index 941637668..fa7f77210 100644 --- a/test/valid-data/type-mapped-enum-optional/schema.json +++ b/test/valid-data/type-mapped-enum-optional/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", @@ -16,6 +17,5 @@ }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-enum/schema.json b/test/valid-data/type-mapped-enum/schema.json index 23e331836..127983313 100644 --- a/test/valid-data/type-mapped-enum/schema.json +++ b/test/valid-data/type-mapped-enum/schema.json @@ -1,8 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "a", + "b", + "c" + ], "properties": { "a": { "type": "string" @@ -14,13 +20,7 @@ "type": "string" } }, - "required": [ - "a", - "b", - "c" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-exclude/schema.json b/test/valid-data/type-mapped-exclude/schema.json index f781c41d0..0daf24649 100644 --- a/test/valid-data/type-mapped-exclude/schema.json +++ b/test/valid-data/type-mapped-exclude/schema.json @@ -1,21 +1,23 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "properties": { - "bar": { - "description": "Bar", - "type": "string" - }, - "foo": { - "description": "Foo", - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "bar" + ], + "properties": { + "foo": { + "type": "string", + "description": "Foo" + }, + "bar": { + "type": "string", + "description": "Bar" + } + }, + "additionalProperties": false } - }, - "required": ["bar"], - "type": "object" } - } } diff --git a/test/valid-data/type-mapped-generic/schema.json b/test/valid-data/type-mapped-generic/schema.json index e26ef3de4..cc2ef2888 100644 --- a/test/valid-data/type-mapped-generic/schema.json +++ b/test/valid-data/type-mapped-generic/schema.json @@ -1,35 +1,35 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "$ref": "#/definitions/NullableAndPartial%3CSomeInterface%3E" - }, - "NullableAndPartial": { - "additionalProperties": false, - "properties": { - "bar": { - "enum": [ - "bar", - null - ], - "type": [ - "string", - "null" - ] + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "$ref": "#/definitions/NullableAndPartial%3CSomeInterface%3E" }, - "foo": { - "enum": [ - 123, - null - ], - "type": [ - "number", - "null" - ] + "NullableAndPartial": { + "type": "object", + "properties": { + "foo": { + "type": [ + "number", + "null" + ], + "enum": [ + 123, + null + ] + }, + "bar": { + "type": [ + "string", + "null" + ], + "enum": [ + "bar", + null + ] + } + }, + "additionalProperties": false } - }, - "type": "object" } - } } diff --git a/test/valid-data/type-mapped-index/schema.json b/test/valid-data/type-mapped-index/schema.json index 28c36a4ce..14bcb7349 100644 --- a/test/valid-data/type-mapped-index/schema.json +++ b/test/valid-data/type-mapped-index/schema.json @@ -1,20 +1,20 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "type": "object", - "properties": { - "foo": { - "type": "number", - "const": 123 - }, - "bar": { - "type": "string", - "const": "baz" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "properties": { + "foo": { + "type": "number", + "const": 123 + }, + "bar": { + "type": "string", + "const": "baz" + } + }, + "additionalProperties": false } - }, - "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/type-mapped-literal/schema.json b/test/valid-data/type-mapped-literal/schema.json index 88d61b057..838542e74 100644 --- a/test/valid-data/type-mapped-literal/schema.json +++ b/test/valid-data/type-mapped-literal/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", @@ -19,6 +20,5 @@ }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-native-single-literal/schema.json b/test/valid-data/type-mapped-native-single-literal/schema.json index c27ed5092..47c08e033 100644 --- a/test/valid-data/type-mapped-native-single-literal/schema.json +++ b/test/valid-data/type-mapped-native-single-literal/schema.json @@ -1,18 +1,18 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "a" + ], "properties": { "a": { "type": "string" } }, - "required": [ - "a" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-native/schema.json b/test/valid-data/type-mapped-native/schema.json index f18ab5256..d3c8cd52c 100644 --- a/test/valid-data/type-mapped-native/schema.json +++ b/test/valid-data/type-mapped-native/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", @@ -19,6 +20,5 @@ }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-optional/schema.json b/test/valid-data/type-mapped-optional/schema.json index fc676dacc..6f97dd83b 100644 --- a/test/valid-data/type-mapped-optional/schema.json +++ b/test/valid-data/type-mapped-optional/schema.json @@ -1,23 +1,23 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "bar" + ], "properties": { - "bar": { - "const": "baz", - "type": "string" - }, "foo": { - "const": 123, - "type": "number" + "type": "number", + "const": 123 + }, + "bar": { + "type": "string", + "const": "baz" } }, - "required": [ - "bar" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-mapped-simple/schema.json b/test/valid-data/type-mapped-simple/schema.json index f31b64b67..fb72db475 100644 --- a/test/valid-data/type-mapped-simple/schema.json +++ b/test/valid-data/type-mapped-simple/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", @@ -13,6 +14,5 @@ }, "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-mapped-symbol/schema.json b/test/valid-data/type-mapped-symbol/schema.json index 71454cc17..949ef3af5 100644 --- a/test/valid-data/type-mapped-symbol/schema.json +++ b/test/valid-data/type-mapped-symbol/schema.json @@ -1,12 +1,12 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": { - "type": "string" - }, - "type": "object" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } } - }, - "$ref": "#/definitions/MyObject" } diff --git a/test/valid-data/type-mapped-union-intersection/schema.json b/test/valid-data/type-mapped-union-intersection/schema.json index 000eb8ca7..b02648dce 100644 --- a/test/valid-data/type-mapped-union-intersection/schema.json +++ b/test/valid-data/type-mapped-union-intersection/schema.json @@ -1,34 +1,16 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { - "A": { - "additionalProperties": false, - "properties": { - "a": { - "type": "string" - } - }, + "MyObject": { + "type": "object", "required": [ - "a" + "d" ], - "type": "object" - }, - "B": { - "additionalProperties": false, "properties": { - "b": { + "d": { "type": "string" - } - }, - "required": [ - "b" - ], - "type": "object" - }, - "MyObject": { - "additionalProperties": false, - "properties": { + }, "c": { "anyOf": [ { @@ -38,15 +20,33 @@ "$ref": "#/definitions/B" } ] - }, - "d": { + } + }, + "additionalProperties": false + }, + "A": { + "type": "object", + "required": [ + "a" + ], + "properties": { + "a": { "type": "string" } }, + "additionalProperties": false + }, + "B": { + "type": "object", "required": [ - "d" + "b" ], - "type": "object" + "properties": { + "b": { + "type": "string" + } + }, + "additionalProperties": false } } } diff --git a/test/valid-data/type-mapped-widened/schema.json b/test/valid-data/type-mapped-widened/schema.json index 70e30daf7..d3dc03876 100644 --- a/test/valid-data/type-mapped-widened/schema.json +++ b/test/valid-data/type-mapped-widened/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", @@ -7,6 +8,5 @@ "type": "boolean" } } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-maps/schema.json b/test/valid-data/type-maps/schema.json index 9679bf5aa..b554dfd09 100644 --- a/test/valid-data/type-maps/schema.json +++ b/test/valid-data/type-maps/schema.json @@ -1,8 +1,14 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "map1", + "map2", + "map3" + ], "properties": { "map1": { "$ref": "#/definitions/MyMap1" @@ -14,11 +20,6 @@ "type": "object" } }, - "required": [ - "map1", - "map2", - "map3" - ], "additionalProperties": false }, "MyMap1": { @@ -40,6 +41,5 @@ ] } } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-primitives/schema.json b/test/valid-data/type-primitives/schema.json index f885ca25e..5a72e9758 100644 --- a/test/valid-data/type-primitives/schema.json +++ b/test/valid-data/type-primitives/schema.json @@ -1,8 +1,18 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "boolean", + "number", + "string", + "integer", + "double", + "decimal", + "datetime" + ], "properties": { "boolean": { "type": "boolean" @@ -26,17 +36,7 @@ "type": "string" } }, - "required": [ - "boolean", - "number", - "string", - "integer", - "double", - "decimal", - "datetime" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-regexp/schema.json b/test/valid-data/type-regexp/schema.json index 3aceecbfd..181357d53 100644 --- a/test/valid-data/type-regexp/schema.json +++ b/test/valid-data/type-regexp/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { "type": "object", + "required": [ + "regexp", + "regexpAlias" + ], "properties": { "regexp": { "type": "string", @@ -13,12 +18,7 @@ "format": "regex" } }, - "required": [ - "regexp", - "regexpAlias" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyObject" + } } diff --git a/test/valid-data/type-typeof-class/schema.json b/test/valid-data/type-typeof-class/schema.json index de14633ac..eb1ce8328 100644 --- a/test/valid-data/type-typeof-class/schema.json +++ b/test/valid-data/type-typeof-class/schema.json @@ -1,23 +1,27 @@ { - "$ref": "#/definitions/MyObject", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyObject": { - "additionalProperties": false, - "properties": { - "classType": { - "additionalProperties": false, - "properties": { - "myValue": { - "type": "number" - } - }, - "required": ["myValue"], - "type": "object" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", + "definitions": { + "MyObject": { + "type": "object", + "required": [ + "classType" + ], + "properties": { + "classType": { + "type": "object", + "required": [ + "myValue" + ], + "properties": { + "myValue": { + "type": "number" + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false } - }, - "required": ["classType"], - "type": "object" } - } } diff --git a/test/valid-data/type-typeof-enum/schema.json b/test/valid-data/type-typeof-enum/schema.json index 60b5a6fef..fa97166b4 100644 --- a/test/valid-data/type-typeof-enum/schema.json +++ b/test/valid-data/type-typeof-enum/schema.json @@ -1,115 +1,115 @@ { - "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyObject", "definitions": { "MyObject": { - "additionalProperties": false, + "type": "object", + "required": [ + "fromZero", + "someCustomInitializers", + "stringEnum", + "mixedEnum" + ], "properties": { "fromZero": { - "additionalProperties": false, + "type": "object", + "required": [ + "a", + "b", + "c" + ], "properties": { "a": { - "const": 0, - "type": "number" + "type": "number", + "const": 0 }, "b": { - "const": 1, - "type": "number" + "type": "number", + "const": 1 }, "c": { - "const": 2, - "type": "number" + "type": "number", + "const": 2 } }, + "additionalProperties": false + }, + "someCustomInitializers": { + "type": "object", "required": [ "a", "b", - "c" + "c", + "d" ], - "type": "object" - }, - "mixedEnum": { - "additionalProperties": false, "properties": { "a": { - "const": 0, - "type": "number" + "type": "number", + "const": 10 }, "b": { - "const": 10, - "type": "number" + "type": "number", + "const": 11 }, "c": { - "const": 11, - "type": "number" + "type": "number", + "const": 20 }, "d": { - "const": "foo", - "type": "string" + "type": "number", + "const": 21 } }, + "additionalProperties": false + }, + "stringEnum": { + "type": "object", "required": [ "a", - "b", - "c", - "d" + "b" ], - "type": "object" - }, - "someCustomInitializers": { - "additionalProperties": false, "properties": { "a": { - "const": 10, - "type": "number" + "type": "string", + "const": "foo" }, "b": { - "const": 11, - "type": "number" - }, - "c": { - "const": 20, - "type": "number" - }, - "d": { - "const": 21, - "type": "number" + "type": "string", + "const": "bar" } }, + "additionalProperties": false + }, + "mixedEnum": { + "type": "object", "required": [ "a", "b", "c", "d" ], - "type": "object" - }, - "stringEnum": { - "additionalProperties": false, "properties": { "a": { - "const": "foo", - "type": "string" + "type": "number", + "const": 0 }, "b": { - "const": "bar", - "type": "string" + "type": "number", + "const": 10 + }, + "c": { + "type": "number", + "const": 11 + }, + "d": { + "type": "string", + "const": "foo" } }, - "required": [ - "a", - "b" - ], - "type": "object" + "additionalProperties": false } }, - "required": [ - "fromZero", - "someCustomInitializers", - "stringEnum", - "mixedEnum" - ], - "type": "object" + "additionalProperties": false } } } diff --git a/test/valid-data/type-typeof-keys/schema.json b/test/valid-data/type-typeof-keys/schema.json index 1b4455523..10c126c47 100644 --- a/test/valid-data/type-typeof-keys/schema.json +++ b/test/valid-data/type-typeof-keys/schema.json @@ -1,10 +1,13 @@ { - "$ref": "#/definitions/MyType", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyType": { - "enum": ["foo", "bar"], - "type": "string" + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", + "definitions": { + "MyType": { + "type": "string", + "enum": [ + "foo", + "bar" + ] + } } - } } diff --git a/test/valid-data/type-typeof-object-property/schema.json b/test/valid-data/type-typeof-object-property/schema.json index 7bfe91e0c..4b2a54905 100644 --- a/test/valid-data/type-typeof-object-property/schema.json +++ b/test/valid-data/type-typeof-object-property/schema.json @@ -1,10 +1,10 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "string", "const": "foo" } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-typeof-value/schema.json b/test/valid-data/type-typeof-value/schema.json index 7bfe91e0c..4b2a54905 100644 --- a/test/valid-data/type-typeof-value/schema.json +++ b/test/valid-data/type-typeof-value/schema.json @@ -1,10 +1,10 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "string", "const": "foo" } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-typeof/schema.json b/test/valid-data/type-typeof/schema.json index 32bcaa351..4d980febc 100644 --- a/test/valid-data/type-typeof/schema.json +++ b/test/valid-data/type-typeof/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": [ @@ -7,6 +8,5 @@ "number" ] } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/type-union-tagged/schema.json b/test/valid-data/type-union-tagged/schema.json index 0e858348c..9dfaec366 100644 --- a/test/valid-data/type-union-tagged/schema.json +++ b/test/valid-data/type-union-tagged/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/Shape", "definitions": { "Shape": { "anyOf": [ @@ -16,6 +17,10 @@ }, "Square": { "type": "object", + "required": [ + "kind", + "size" + ], "properties": { "kind": { "type": "string", @@ -25,14 +30,15 @@ "type": "number" } }, - "required": [ - "kind", - "size" - ], "additionalProperties": false }, "Rectangle": { "type": "object", + "required": [ + "kind", + "width", + "height" + ], "properties": { "kind": { "type": "string", @@ -45,15 +51,14 @@ "type": "number" } }, - "required": [ - "kind", - "width", - "height" - ], "additionalProperties": false }, "Circle": { "type": "object", + "required": [ + "kind", + "radius" + ], "properties": { "kind": { "type": "string", @@ -63,12 +68,7 @@ "type": "number" } }, - "required": [ - "kind", - "radius" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/Shape" + } } diff --git a/test/valid-data/type-union/schema.json b/test/valid-data/type-union/schema.json index 3570c4024..8c37a89fb 100644 --- a/test/valid-data/type-union/schema.json +++ b/test/valid-data/type-union/schema.json @@ -1,8 +1,17 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/TypeUnion", "definitions": { "TypeUnion": { "type": "object", + "required": [ + "var1", + "var2", + "var3", + "var4", + "var5", + "var6" + ], "properties": { "var1": { "type": [ @@ -71,16 +80,7 @@ } } }, - "required": [ - "var1", - "var2", - "var3", - "var4", - "var5", - "var6" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/TypeUnion" + } } diff --git a/test/valid-data/undefined-alias/schema.json b/test/valid-data/undefined-alias/schema.json index 49ba72661..0de9dfe88 100644 --- a/test/valid-data/undefined-alias/schema.json +++ b/test/valid-data/undefined-alias/schema.json @@ -1,9 +1,9 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "not": {} } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/undefined-property/schema.json b/test/valid-data/undefined-property/schema.json index 797109441..a604dae6f 100644 --- a/test/valid-data/undefined-property/schema.json +++ b/test/valid-data/undefined-property/schema.json @@ -1,8 +1,12 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "object", + "required": [ + "b" + ], "properties": { "a": { "not": {} @@ -14,11 +18,7 @@ "type": "string" } }, - "required": [ - "b" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyType" + } } diff --git a/test/valid-data/undefined-union/schema.json b/test/valid-data/undefined-union/schema.json index 1c5f24104..ad6029d64 100644 --- a/test/valid-data/undefined-union/schema.json +++ b/test/valid-data/undefined-union/schema.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "anyOf": [ @@ -11,6 +12,5 @@ } ] } - }, - "$ref": "#/definitions/MyType" + } } From c895ae7d6af05324efed316234a7686fa5650772 Mon Sep 17 00:00:00 2001 From: Shishir Date: Wed, 17 Feb 2021 04:20:03 -0800 Subject: [PATCH 3/9] fix: linter errors --- src/NodeParser/InterfaceAndClassNodeParser.ts | 8 +++- src/NodeParser/MappedTypeNodeParser.ts | 16 ++++++- .../ObjectLiteralExpressionNodeParser.ts | 8 +++- src/SchemaGenerator.ts | 43 +++++++++++-------- src/Utils/isAssignableTo.ts | 10 ++++- 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/NodeParser/InterfaceAndClassNodeParser.ts b/src/NodeParser/InterfaceAndClassNodeParser.ts index 50a9bebc1..7b249c26c 100644 --- a/src/NodeParser/InterfaceAndClassNodeParser.ts +++ b/src/NodeParser/InterfaceAndClassNodeParser.ts @@ -64,7 +64,13 @@ export class InterfaceAndClassNodeParser implements SubNodeParser { } } - return new ObjectType(id, this.getBaseTypes(node, context), properties, additionalProperties, node.getSourceFile().fileName); + return new ObjectType( + id, + this.getBaseTypes(node, context), + properties, + additionalProperties, + node.getSourceFile().fileName + ); } /** diff --git a/src/NodeParser/MappedTypeNodeParser.ts b/src/NodeParser/MappedTypeNodeParser.ts index e99038fce..fd7f9ccb4 100644 --- a/src/NodeParser/MappedTypeNodeParser.ts +++ b/src/NodeParser/MappedTypeNodeParser.ts @@ -40,7 +40,13 @@ export class MappedTypeNodeParser implements SubNodeParser { ); } else if (keyListType instanceof LiteralType) { // Key type resolves to single known property - return new ObjectType(id, [], this.getProperties(node, new UnionType([keyListType]), context), false, node.getSourceFile().fileName); + return new ObjectType( + id, + [], + this.getProperties(node, new UnionType([keyListType]), context), + false, + node.getSourceFile().fileName + ); } else if (keyListType instanceof StringType || keyListType instanceof SymbolType) { // Key type widens to `string` const type = this.childNodeParser.createType(node.type!, context); @@ -49,7 +55,13 @@ export class MappedTypeNodeParser implements SubNodeParser { const type = this.childNodeParser.createType(node.type!, this.createSubContext(node, keyListType, context)); return type === undefined ? undefined : new ArrayType(type); } else if (keyListType instanceof EnumType) { - return new ObjectType(id, [], this.getValues(node, keyListType, context), false, node.getSourceFile().fileName); + return new ObjectType( + id, + [], + this.getValues(node, keyListType, context), + false, + node.getSourceFile().fileName + ); } else { throw new LogicError( // eslint-disable-next-line max-len diff --git a/src/NodeParser/ObjectLiteralExpressionNodeParser.ts b/src/NodeParser/ObjectLiteralExpressionNodeParser.ts index df54726ea..2855b8128 100644 --- a/src/NodeParser/ObjectLiteralExpressionNodeParser.ts +++ b/src/NodeParser/ObjectLiteralExpressionNodeParser.ts @@ -24,7 +24,13 @@ export class ObjectLiteralExpressionNodeParser implements SubNodeParser { ) ); - return new ObjectType(`object-${getKey(node, context)}`, [], properties, false, node.getSourceFile().fileName); + return new ObjectType( + `object-${getKey(node, context)}`, + [], + properties, + false, + node.getSourceFile().fileName + ); } // TODO: implement this? diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index 79737b923..bfedeefa4 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -40,17 +40,22 @@ export class SchemaGenerator { rootTypes.forEach((rootType) => this.appendRootChildDefinitions(rootType, definitions, idNameMap)); const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions); - if (false) { - // TODO: remove this. - console.log( - JSON.stringify({ - rootTypeDefinition, - definitions, - reachableDefinitions, - }, null, 2) - ); - console.log(idNameMap); - } + // if (false) { + // // TODO: remove this. + // console.log( + // JSON.stringify( + // { + // rootTypeDefinition, + // definitions, + // reachableDefinitions, + // }, + // null, + // 2 + // ) + // ); + // console.log(idNameMap); + // } + // create schema - all $ref's use getId(). const schema: JSONSchema7Definition = { ...(this.config?.schemaId ? { $id: this.config.schemaId } : {}), @@ -62,7 +67,7 @@ export class SchemaGenerator { return resolveIdRefs(schema, idNameMap, this.config?.encodeRefs ?? true) as JSONSchema7; } - protected getRootNodes(fullName: string | undefined) { + protected getRootNodes(fullName: string | undefined): ts.Node[] { if (fullName && fullName !== "*") { return [this.findNamedNode(fullName)]; } else { @@ -97,7 +102,11 @@ export class SchemaGenerator { protected getRootTypeDefinition(rootType: BaseType): Definition { return this.typeFormatter.getDefinition(rootType); } - protected appendRootChildDefinitions(rootType: BaseType, childDefinitions: StringMap, idNameMap: Map): void { + protected appendRootChildDefinitions( + rootType: BaseType, + childDefinitions: StringMap, + idNameMap: Map + ): void { const seen = new Set(); const children = this.typeFormatter .getChildren(rootType) @@ -130,7 +139,7 @@ export class SchemaGenerator { return definitions; }, childDefinitions); } - protected partitionFiles() { + protected partitionFiles(): { projectFiles: ts.SourceFile[]; externalFiles: ts.SourceFile[] } { const projectFiles = new Array(); const externalFiles = new Array(); @@ -145,7 +154,7 @@ export class SchemaGenerator { sourceFiles: readonly ts.SourceFile[], typeChecker: ts.TypeChecker, types: Map - ) { + ): void { for (const sourceFile of sourceFiles) { this.inspectNode(sourceFile, typeChecker, types); } @@ -209,7 +218,7 @@ function getCommonPrefixes(paths: string[]) { p .substr(path_pos + 1) .replace(/\//g, "__") - .replace(/\.[^\.]+$/, "") + .replace(/\.[^.]+$/, "") ); } @@ -238,7 +247,7 @@ function resolveIdRefs( if (!schema || typeof schema === "boolean") { return schema; } - let { $ref, allOf, oneOf, anyOf, not, properties, items, definitions, additionalProperties, ...rest } = schema; + const { $ref, allOf, oneOf, anyOf, not, properties, items, definitions, additionalProperties, ...rest } = schema; const result: JSONSchema7Definition = { ...rest }; if ($ref) { // THE Money Shot. diff --git a/src/Utils/isAssignableTo.ts b/src/Utils/isAssignableTo.ts index 3a8fd5c1a..af1fe579d 100644 --- a/src/Utils/isAssignableTo.ts +++ b/src/Utils/isAssignableTo.ts @@ -37,7 +37,15 @@ function combineIntersectingTypes(intersection: IntersectionType): BaseType[] { if (objectTypes.length === 1) { combined.push(objectTypes[0]); } else if (objectTypes.length > 1) { - combined.push(new ObjectType(`combined-objects-${intersection.getId()}`, objectTypes, [], false, `non-terminal-intersection`)); + combined.push( + new ObjectType( + `combined-objects-${intersection.getId()}`, + objectTypes, + [], + false, + `non-terminal-intersection` + ) + ); } return combined; } From 28b6a6053f5e6e87efe9e0df102fc138da02670d Mon Sep 17 00:00:00 2001 From: Shishir Date: Wed, 17 Feb 2021 04:34:11 -0800 Subject: [PATCH 4/9] fix: use proper case for imported filenames --- test/valid-data/duplicates-inheritance/intermediate.ts | 2 +- test/valid-data/duplicates-inheritance/main.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/valid-data/duplicates-inheritance/intermediate.ts b/test/valid-data/duplicates-inheritance/intermediate.ts index 3ae21b2ec..8315188da 100644 --- a/test/valid-data/duplicates-inheritance/intermediate.ts +++ b/test/valid-data/duplicates-inheritance/intermediate.ts @@ -1,4 +1,4 @@ -import * as Base from "./Base"; +import * as Base from "./base"; export interface MyObject extends Base.MyObject { b: string; diff --git a/test/valid-data/duplicates-inheritance/main.ts b/test/valid-data/duplicates-inheritance/main.ts index 95e357b5a..00ad875b7 100644 --- a/test/valid-data/duplicates-inheritance/main.ts +++ b/test/valid-data/duplicates-inheritance/main.ts @@ -1,4 +1,4 @@ -import * as Intermediate from "./Intermediate"; +import * as Intermediate from "./intermediate"; export interface MyObject extends Intermediate.MyObject { c: string; From baaa7f4ec05e65f429108084f94c973b889abe7e Mon Sep 17 00:00:00 2001 From: Shishir Date: Wed, 17 Feb 2021 13:28:58 -0800 Subject: [PATCH 5/9] wip --- src/NodeParser/InterfaceAndClassNodeParser.ts | 12 +++++++- src/SchemaGenerator.ts | 30 +++++++++---------- test/valid-data/shorthand-array/schema.json | 12 ++++---- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/NodeParser/InterfaceAndClassNodeParser.ts b/src/NodeParser/InterfaceAndClassNodeParser.ts index 7b249c26c..7828ed1b8 100644 --- a/src/NodeParser/InterfaceAndClassNodeParser.ts +++ b/src/NodeParser/InterfaceAndClassNodeParser.ts @@ -5,10 +5,12 @@ import { ArrayType } from "../Type/ArrayType"; import { BaseType } from "../Type/BaseType"; import { ObjectProperty, ObjectType } from "../Type/ObjectType"; import { ReferenceType } from "../Type/ReferenceType"; +import { hasJsDocTag } from "../Utils/hasJsDocTag"; import { isNodeHidden } from "../Utils/isHidden"; import { isPublic, isStatic } from "../Utils/modifiers"; import { getKey } from "../Utils/nodeKey"; import { notUndefined } from "../Utils/notUndefined"; +import { localSymbolAtNode } from "../Utils/symbolAtNode"; export class InterfaceAndClassNodeParser implements SubNodeParser { public constructor( @@ -168,6 +170,14 @@ export class InterfaceAndClassNodeParser implements SubNodeParser { private getTypeId(node: ts.Node, context: Context): string { const nodeType = ts.isInterfaceDeclaration(node) ? "interface" : "class"; - return `${nodeType}-${getKey(node, context)}`; + return `${this.isExportType(node) ? "def-" : ""}${nodeType}-${getKey(node, context)}`; + } + + private isExportType(node: ts.Node): boolean { + if (hasJsDocTag(node, "internal")) { + return false; + } + const localSymbol = localSymbolAtNode(node); + return localSymbol ? "exportSymbol" in localSymbol : false; } } diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index bfedeefa4..ce96f867d 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -40,21 +40,21 @@ export class SchemaGenerator { rootTypes.forEach((rootType) => this.appendRootChildDefinitions(rootType, definitions, idNameMap)); const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions); - // if (false) { - // // TODO: remove this. - // console.log( - // JSON.stringify( - // { - // rootTypeDefinition, - // definitions, - // reachableDefinitions, - // }, - // null, - // 2 - // ) - // ); - // console.log(idNameMap); - // } + if (true) { + // TODO: remove this. + console.log( + JSON.stringify( + { + rootTypeDefinition, + definitions, + reachableDefinitions, + }, + null, + 2 + ) + ); + console.log(idNameMap); + } // create schema - all $ref's use getId(). const schema: JSONSchema7Definition = { diff --git a/test/valid-data/shorthand-array/schema.json b/test/valid-data/shorthand-array/schema.json index 562124a2f..aa6e61a62 100644 --- a/test/valid-data/shorthand-array/schema.json +++ b/test/valid-data/shorthand-array/schema.json @@ -1,8 +1,13 @@ { "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/MyType", "definitions": { "MyType": { "type": "object", + "required": [ + "numberArray", + "stringArray" + ], "properties": { "numberArray": { "type": "array", @@ -17,12 +22,7 @@ } } }, - "required": [ - "numberArray", - "stringArray" - ], "additionalProperties": false } - }, - "$ref": "#/definitions/MyType" + } } From fa9b8b5431304a205585c6aab3a29c43ceddc406 Mon Sep 17 00:00:00 2001 From: Shishir Date: Sat, 20 Feb 2021 14:22:48 -0800 Subject: [PATCH 6/9] fix: all tests pass - including vega-lite! --- package.json | 8 +- src/SchemaGenerator.ts | 49 ++++---- src/Type/BaseType.ts | 4 +- src/Type/DefinitionType.ts | 2 +- src/TypeFormatter/DefinitionTypeFormatter.ts | 2 +- src/TypeFormatter/ReferenceTypeFormatter.ts | 2 +- test/valid-data/annotation-custom/schema.json | 1 + test/valid-data/class-generics/schema.json | 96 ++++++++-------- .../duplicates-composition/schema.json | 82 +++++++------- .../duplicates-inheritance/schema.json | 44 ++++---- test/valid-data/duplicates/schema.json | 34 +++--- test/valid-data/generic-hell/schema.json | 27 +---- .../generic-prefixed-number/schema.json | 34 +++--- .../valid-data/literal-array-type/schema.json | 1 + .../valid-data/literal-index-type/schema.json | 1 + .../literal-object-type/schema.json | 25 ++--- .../object-literal-expression/schema.json | 1 + .../schema.json | 64 +++++------ .../schema.json | 35 +++--- .../type-conditional-inheritance/schema.json | 106 +++++++++--------- .../type-extend-circular/schema.json | 14 +-- .../schema.json | 3 + .../type-mapped-additional-props/schema.json | 30 ++--- .../type-mapped-double-exclude/schema.json | 17 ++- .../type-mapped-exclude/schema.json | 5 - .../type-mapped-generic/schema.json | 60 +++++----- test/valid-data/type-typeof-class/schema.json | 1 + test/valid-data/type-typeof-keys/schema.json | 1 + yarn.lock | 9 +- 29 files changed, 374 insertions(+), 384 deletions(-) diff --git a/package.json b/package.json index a81ca86d5..12b65848f 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "ajv": "^7.1.1", "ajv-formats": "^1.5.1", "chai": "^4.3.0", + "cross-env": "^7.0.3", "eslint": "^7.20.0", "eslint-config-prettier": "^7.2.0", "eslint-plugin-prettier": "^3.3.1", @@ -82,11 +83,12 @@ "lint": "eslint {src,test,factory}/**/*.ts", "format": "yarn lint --fix", "test": "jest test/ --verbose", - "test:fast": "FAST_TEST=1 jest test/ --verbose", + "test:fast": "cross-env FAST_TEST=1 jest test/ --verbose", "test:coverage": "yarn jest test/ --collectCoverage=true", - "test:update": "UPDATE_SCHEMA=true yarn test:fast", + "test:update": "cross-env UPDATE_SCHEMA=true yarn test:fast", "debug": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts", - "dbg": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts -p test/valid-data/duplicates-composition/main.ts -t MyObject", + "dbg": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts -p test/valid-data/type-aliases-recursive-anonymous/main.ts -t MyAlias", + "dbg-vega": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts -p ../../../tmp/vega-lite/src/index.ts -t TopLevelSpec --no-type-check --no-ref-encode > ../../../tmp/vega-lite/build/vega-lite-schema2.json", "run": "ts-node ts-json-schema-generator.ts" }, "release": { diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index ce96f867d..e7586ce99 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -13,6 +13,9 @@ import { removeUnreachable } from "./Utils/removeUnreachable"; import { Config } from "./Config"; import { hasJsDocTag } from "./Utils/hasJsDocTag"; import { JSONSchema7, JSONSchema7Definition } from "json-schema"; +import { derefType } from "./Utils/derefType"; +import { AliasType } from "./Type/AliasType"; +import { ObjectType } from "./Type/ObjectType"; export class SchemaGenerator { public constructor( @@ -40,22 +43,6 @@ export class SchemaGenerator { rootTypes.forEach((rootType) => this.appendRootChildDefinitions(rootType, definitions, idNameMap)); const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions); - if (true) { - // TODO: remove this. - console.log( - JSON.stringify( - { - rootTypeDefinition, - definitions, - reachableDefinitions, - }, - null, - 2 - ) - ); - console.log(idNameMap); - } - // create schema - all $ref's use getId(). const schema: JSONSchema7Definition = { ...(this.config?.schemaId ? { $id: this.config.schemaId } : {}), @@ -121,19 +108,19 @@ export class SchemaGenerator { const duplicates: StringMap> = {}; for (const child of children) { - const name = child.getName(); // .replace(/^def-interface/, "interface"); + const name = child.getName(); duplicates[name] = duplicates[name] ?? new Set(); duplicates[name].add(child); } children.reduce((definitions, child) => { - const id = child.getId(); // .replace(/^def-interface/, "interface"); + const id = child.getId().replace(/^def-/, ""); if (!(id in definitions)) { + const name = unambiguousName(child, child === rootType, [...duplicates[child.getName()]]); // Record the schema against the ID, allowing steps like removeUnreachable to work definitions[id] = this.typeFormatter.getDefinition(child.getType()); // Create a record of id->name mapping. This is used in the final step // to resolve id -> name before delivering the schema to caller. - const name = unambiguousName(child, child === rootType, [...duplicates[child.getName()]]); idNameMap.set(id, name); } return definitions; @@ -222,17 +209,33 @@ function getCommonPrefixes(paths: string[]) { ); } -function unambiguousName(child: DefinitionType, isRoot: boolean, peers: DefinitionType[]) { +function unambiguousName(child: DefinitionType, isRoot: boolean, peers: DefinitionType[]): string { if (peers.length === 1 || isRoot) { return child.getName(); - } else { + } else if (child.getType().getSrcFileName()) { let index = -1; - const srcPaths = peers.map((peer: DefinitionType, count) => { + + // filter unique peers to be those that have srcFileNames. + // Intermediate Types - AnnotationTypes, UnionTypes, do not have sourceFileNames + const uniques = peers.filter((peer) => peer.getType().getSrcFileName()); + if (uniques.length === 1) { + return uniques[0].getName(); + } + const srcPaths = uniques.map((peer: DefinitionType, count) => { index = child === peer ? count : index; - return peer.getType().getSrcFileName(); + return peer.getType().getSrcFileName()!; }); const prefixes = getCommonPrefixes(srcPaths); return `${prefixes[index]}-${child.getName()}`; + } else { + // intermediate type. + const name = child.getName(); + // TODO: Perhaps we should maintain a name-id map, and throw a duplicate error on collision. + if (name === undefined) { + // this might be unreachable code. + throw new Error(`Unable to disambiguate types`); + } + return name; } } diff --git a/src/Type/BaseType.ts b/src/Type/BaseType.ts index 864519c5f..e1a5516e9 100644 --- a/src/Type/BaseType.ts +++ b/src/Type/BaseType.ts @@ -12,7 +12,7 @@ export abstract class BaseType { * Provide a base class implementation. Will only be exported for entities * exposed in a schema - Alias|Enum|Class|Interface. */ - public getSrcFileName(): string { - throw new Error(`Missing sourceFileName '${this.getId()}'`); + public getSrcFileName(): string | null { + return null; } } diff --git a/src/Type/DefinitionType.ts b/src/Type/DefinitionType.ts index 2d0c8cbef..689960b50 100644 --- a/src/Type/DefinitionType.ts +++ b/src/Type/DefinitionType.ts @@ -17,7 +17,7 @@ export class DefinitionType extends BaseType { return this.type; } - public getSrcFileName(): string { + public getSrcFileName(): string | null { return this.type.getSrcFileName(); } } diff --git a/src/TypeFormatter/DefinitionTypeFormatter.ts b/src/TypeFormatter/DefinitionTypeFormatter.ts index 1ff913558..824b8dfeb 100644 --- a/src/TypeFormatter/DefinitionTypeFormatter.ts +++ b/src/TypeFormatter/DefinitionTypeFormatter.ts @@ -12,7 +12,7 @@ export class DefinitionTypeFormatter implements SubTypeFormatter { return type instanceof DefinitionType; } public getDefinition(type: DefinitionType): Definition { - const ref = type.getId(); + const ref = type.getId().replace(/^def-/, ""); return { $ref: `#/definitions/${this.encodeRefs ? encodeURIComponent(ref) : ref}` }; } public getChildren(type: DefinitionType): BaseType[] { diff --git a/src/TypeFormatter/ReferenceTypeFormatter.ts b/src/TypeFormatter/ReferenceTypeFormatter.ts index 5ffe4d508..1e819b1cb 100644 --- a/src/TypeFormatter/ReferenceTypeFormatter.ts +++ b/src/TypeFormatter/ReferenceTypeFormatter.ts @@ -12,7 +12,7 @@ export class ReferenceTypeFormatter implements SubTypeFormatter { return type instanceof ReferenceType; } public getDefinition(type: ReferenceType): Definition { - const ref = type.getId(); + const ref = type.getId().replace(/^def-/, ""); return { $ref: `#/definitions/${this.encodeRefs ? encodeURIComponent(ref) : ref}` }; } public getChildren(type: ReferenceType): BaseType[] { diff --git a/test/valid-data/annotation-custom/schema.json b/test/valid-data/annotation-custom/schema.json index 38841062d..2b56dceca 100644 --- a/test/valid-data/annotation-custom/schema.json +++ b/test/valid-data/annotation-custom/schema.json @@ -18,4 +18,5 @@ "customUnquotedProperty": "not-a-valid-json", "type": "object" } + } } diff --git a/test/valid-data/class-generics/schema.json b/test/valid-data/class-generics/schema.json index 56eea589a..4484957dc 100644 --- a/test/valid-data/class-generics/schema.json +++ b/test/valid-data/class-generics/schema.json @@ -1,54 +1,54 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "type": "object", - "required": [ - "a", - "b", - "c", - "d" - ], - "properties": { - "a": { - "type": "number" - }, - "b": { - "type": "string" - }, - "c": { - "$ref": "#/definitions/Base%3Cstring%3E" - }, - "d": { - "$ref": "#/definitions/Base%3Cboolean%3E" - } - }, - "additionalProperties": false + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "Base": { + "additionalProperties": false, + "properties": { + "a": { + "type": "boolean" + } + }, + "required": [ + "a" + ], + "type": "object" + }, + "Base": { + "additionalProperties": false, + "properties": { + "a": { + "type": "string" + } + }, + "required": [ + "a" + ], + "type": "object" + }, + "MyObject": { + "additionalProperties": false, + "properties": { + "a": { + "type": "number" + }, + "b": { + "type": "string" }, - "Base": { - "type": "object", - "required": [ - "a" - ], - "properties": { - "a": { - "type": "string" - } - }, - "additionalProperties": false + "c": { + "$ref": "#/definitions/Base%3Cstring%3E" }, - "Base": { - "type": "object", - "required": [ - "a" - ], - "properties": { - "a": { - "type": "boolean" - } - }, - "additionalProperties": false + "d": { + "$ref": "#/definitions/Base%3Cboolean%3E" } + }, + "required": [ + "a", + "b", + "c", + "d" + ], + "type": "object" } + } } diff --git a/test/valid-data/duplicates-composition/schema.json b/test/valid-data/duplicates-composition/schema.json index 31a28440a..6e4486ec9 100644 --- a/test/valid-data/duplicates-composition/schema.json +++ b/test/valid-data/duplicates-composition/schema.json @@ -1,46 +1,46 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "type": "object", - "required": [ - "a", - "b" - ], - "properties": { - "a": { - "$ref": "#/definitions/componentA-MyObject" - }, - "b": { - "$ref": "#/definitions/componentB-MyObject" - } - }, - "additionalProperties": false + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "additionalProperties": false, + "properties": { + "a": { + "$ref": "#/definitions/componentA-MyObject" }, - "componentA-MyObject": { - "type": "object", - "required": [ - "a" - ], - "properties": { - "a": { - "type": "string" - } - }, - "additionalProperties": false - }, - "componentB-MyObject": { - "type": "object", - "required": [ - "b" - ], - "properties": { - "b": { - "type": "string" - } - }, - "additionalProperties": false + "b": { + "$ref": "#/definitions/componentB-MyObject" + } + }, + "required": [ + "a", + "b" + ], + "type": "object" + }, + "componentA-MyObject": { + "additionalProperties": false, + "properties": { + "a": { + "type": "string" + } + }, + "required": [ + "a" + ], + "type": "object" + }, + "componentB-MyObject": { + "additionalProperties": false, + "properties": { + "b": { + "type": "string" } + }, + "required": [ + "b" + ], + "type": "object" } + } } diff --git a/test/valid-data/duplicates-inheritance/schema.json b/test/valid-data/duplicates-inheritance/schema.json index 127983313..01ebc1a36 100644 --- a/test/valid-data/duplicates-inheritance/schema.json +++ b/test/valid-data/duplicates-inheritance/schema.json @@ -1,26 +1,26 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "type": "object", - "required": [ - "a", - "b", - "c" - ], - "properties": { - "a": { - "type": "string" - }, - "b": { - "type": "string" - }, - "c": { - "type": "string" - } - }, - "additionalProperties": false + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "additionalProperties": false, + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "string" + }, + "c": { + "type": "string" } + }, + "required": [ + "a", + "b", + "c" + ], + "type": "object" } + } } diff --git a/test/valid-data/duplicates/schema.json b/test/valid-data/duplicates/schema.json index 94b7ed2cd..aa2b526de 100644 --- a/test/valid-data/duplicates/schema.json +++ b/test/valid-data/duplicates/schema.json @@ -1,22 +1,22 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyType", - "definitions": { - "MyType": { - "anyOf": [ - { - "$ref": "#/definitions/import1-A" - }, - { - "$ref": "#/definitions/import2-A" - } - ] + "$ref": "#/definitions/MyType", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyType": { + "anyOf": [ + { + "$ref": "#/definitions/import1-A" }, - "import1-A": { - "type": "number" - }, - "import2-A": { - "type": "string" + { + "$ref": "#/definitions/import2-A" } + ] + }, + "import1-A": { + "type": "number" + }, + "import2-A": { + "type": "string" } + } } diff --git a/test/valid-data/generic-hell/schema.json b/test/valid-data/generic-hell/schema.json index 0de61ee87..41305cc24 100644 --- a/test/valid-data/generic-hell/schema.json +++ b/test/valid-data/generic-hell/schema.json @@ -128,31 +128,8 @@ "const": "alias", "type": "string" }, - "SomeGeneric<\"alias\",\"alias\">": { - "type": "object", - "required": [ - "a", - "b", - "c", - "d" - ], - "properties": { - "a": { - "type": "string", - "const": "alias" - }, - "b": { - "type": "string", - "const": "alias" - }, - "c": { - "$ref": "#/definitions/GenericA%3C%22alias%22%3E" - }, - "d": { - "$ref": "#/definitions/GenericC%3C%22alias%22%3E" - } - }, - "additionalProperties": false + "c": { + "$ref": "#/definitions/GenericA%3C%22alias%22%3E" }, "d": { "$ref": "#/definitions/GenericC%3C%22alias%22%3E" diff --git a/test/valid-data/generic-prefixed-number/schema.json b/test/valid-data/generic-prefixed-number/schema.json index 7e244c739..6542118b6 100644 --- a/test/valid-data/generic-prefixed-number/schema.json +++ b/test/valid-data/generic-prefixed-number/schema.json @@ -1,21 +1,21 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "type": "object", - "required": [ - "angle" - ], - "properties": { - "angle": { - "$ref": "#/definitions/Range%3C-180%2C180%3E" - } - }, - "additionalProperties": false - }, - "Range<-180,180>": { - "type": "number" + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "additionalProperties": false, + "properties": { + "angle": { + "$ref": "#/definitions/Range%3C-180%2C180%3E" } + }, + "required": [ + "angle" + ], + "type": "object" + }, + "Range<-180,180>": { + "type": "number" } + } } diff --git a/test/valid-data/literal-array-type/schema.json b/test/valid-data/literal-array-type/schema.json index e91eaeed5..280cb9b2c 100644 --- a/test/valid-data/literal-array-type/schema.json +++ b/test/valid-data/literal-array-type/schema.json @@ -17,4 +17,5 @@ "minItems": 2, "type": "array" } + } } diff --git a/test/valid-data/literal-index-type/schema.json b/test/valid-data/literal-index-type/schema.json index a157b91b1..088b5878a 100644 --- a/test/valid-data/literal-index-type/schema.json +++ b/test/valid-data/literal-index-type/schema.json @@ -9,4 +9,5 @@ ], "type": "string" } + } } diff --git a/test/valid-data/literal-object-type/schema.json b/test/valid-data/literal-object-type/schema.json index 8aed903b9..894bc549c 100644 --- a/test/valid-data/literal-object-type/schema.json +++ b/test/valid-data/literal-object-type/schema.json @@ -1,19 +1,13 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyType", - "definitions": { - "MyType": { - "type": "object", - "required": [ - "abc" - ], - "properties": { - "abc": { - "type": "string", - "const": "def" - } - }, - "additionalProperties": false + "$ref": "#/definitions/MyType", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyType": { + "additionalProperties": false, + "properties": { + "abc": { + "const": "def", + "type": "string" } }, "required": [ @@ -21,4 +15,5 @@ ], "type": "object" } + } } diff --git a/test/valid-data/object-literal-expression/schema.json b/test/valid-data/object-literal-expression/schema.json index ff4fff8a4..85f6d2085 100644 --- a/test/valid-data/object-literal-expression/schema.json +++ b/test/valid-data/object-literal-expression/schema.json @@ -9,4 +9,5 @@ ], "type": "string" } + } } diff --git a/test/valid-data/type-aliases-recursive-anonymous/schema.json b/test/valid-data/type-aliases-recursive-anonymous/schema.json index 10db739d1..a465b36cf 100644 --- a/test/valid-data/type-aliases-recursive-anonymous/schema.json +++ b/test/valid-data/type-aliases-recursive-anonymous/schema.json @@ -1,38 +1,38 @@ { - "$ref": "#/definitions/MyAlias", - "$schema": "http://json-schema.org/draft-07/schema#", - "definitions": { - "MyAlias": { - "additionalProperties": false, - "properties": { - "alias": { - "$ref": "#/definitions/MyAlias" + "$ref": "#/definitions/MyAlias", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyAlias": { + "additionalProperties": false, + "properties": { + "alias": { + "$ref": "#/definitions/MyAlias" + }, + "self": { + "$ref": "#/definitions/interface-425636228-0-62-425636228-0-96" + } }, - "self": { - "$ref": "#/definitions/interface-425636228-0-62-425636228-0-96" - } + "required": [ + "alias", + "self" + ], + "type": "object" }, - "required": [ - "alias", - "self" - ], - "type": "object" - }, - "interface-425636228-0-62-425636228-0-96": { - "additionalProperties": false, - "properties": { - "alias": { - "$ref": "#/definitions/MyAlias" + "interface-425636228-0-62-425636228-0-96": { + "additionalProperties": false, + "properties": { + "alias": { + "$ref": "#/definitions/MyAlias" + }, + "self": { + "$ref": "#/definitions/interface-425636228-0-62-425636228-0-96" + } }, - "self": { - "$ref": "#/definitions/interface-425636228-0-62-425636228-0-96" - } - }, - "required": [ - "alias", - "self" - ], - "type": "object" + "required": [ + "alias", + "self" + ], + "type": "object" + } } } -} diff --git a/test/valid-data/type-aliases-recursive-generics-export/schema.json b/test/valid-data/type-aliases-recursive-generics-export/schema.json index 0320315b0..e9eb4b52a 100644 --- a/test/valid-data/type-aliases-recursive-generics-export/schema.json +++ b/test/valid-data/type-aliases-recursive-generics-export/schema.json @@ -1,24 +1,18 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyAlias", - "definitions": { - "MyAlias": { - "type": "object", - "required": [ - "a" - ], - "properties": { - "a": { - "$ref": "#/definitions/Map%3CMyAlias%3E" - } - }, - "additionalProperties": false - }, - "Map": { - "type": "object", - "additionalProperties": { - "$ref": "#/definitions/MyAlias" - } + "$ref": "#/definitions/MyAlias", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "Map": { + "additionalProperties": { + "$ref": "#/definitions/MyAlias" + }, + "type": "object" + }, + "MyAlias": { + "additionalProperties": false, + "properties": { + "a": { + "$ref": "#/definitions/Map%3CMyAlias%3E" } }, "required": [ @@ -26,4 +20,5 @@ ], "type": "object" } + } } diff --git a/test/valid-data/type-conditional-inheritance/schema.json b/test/valid-data/type-conditional-inheritance/schema.json index d02d266c7..edb8256d5 100644 --- a/test/valid-data/type-conditional-inheritance/schema.json +++ b/test/valid-data/type-conditional-inheritance/schema.json @@ -1,62 +1,62 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "type": "object", - "required": [ - "a", - "b", - "c", - "d", - "e", - "f" - ], - "properties": { - "a": { - "$ref": "#/definitions/Map%3CA%3E" - }, - "b": { - "$ref": "#/definitions/Map%3CB%3E" - }, - "c": { - "$ref": "#/definitions/Map%3CC%3E" - }, - "d": { - "$ref": "#/definitions/Map%3CD%3E" - }, - "e": { - "$ref": "#/definitions/Map%3CE%3E" - }, - "f": { - "$ref": "#/definitions/Map%3CF%3E" - } - }, - "additionalProperties": false + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "Map": { + "const": "a", + "type": "string" + }, + "Map": { + "const": "a", + "type": "string" + }, + "Map": { + "const": "a", + "type": "string" + }, + "Map": { + "const": "d", + "type": "string" + }, + "Map": { + "const": "d", + "type": "string" + }, + "Map": { + "const": "f", + "type": "string" + }, + "MyObject": { + "additionalProperties": false, + "properties": { + "a": { + "$ref": "#/definitions/Map%3CA%3E" }, - "Map": { - "type": "string", - "const": "a" + "b": { + "$ref": "#/definitions/Map%3CB%3E" }, - "Map": { - "type": "string", - "const": "a" + "c": { + "$ref": "#/definitions/Map%3CC%3E" }, - "Map": { - "type": "string", - "const": "a" + "d": { + "$ref": "#/definitions/Map%3CD%3E" }, - "Map": { - "type": "string", - "const": "d" + "e": { + "$ref": "#/definitions/Map%3CE%3E" }, - "Map": { - "type": "string", - "const": "d" - }, - "Map": { - "type": "string", - "const": "f" + "f": { + "$ref": "#/definitions/Map%3CF%3E" } + }, + "required": [ + "a", + "b", + "c", + "d", + "e", + "f" + ], + "type": "object" } + } } diff --git a/test/valid-data/type-extend-circular/schema.json b/test/valid-data/type-extend-circular/schema.json index 6425b4ace..3ebe4afbc 100644 --- a/test/valid-data/type-extend-circular/schema.json +++ b/test/valid-data/type-extend-circular/schema.json @@ -20,17 +20,8 @@ "bar": { "type": "string" }, - "MyObject": { - "type": "object", - "required": [ - "foo" - ], - "properties": { - "foo": { - "$ref": "#/definitions/MyObject" - } - }, - "additionalProperties": false + "foo": { + "$ref": "#/definitions/MyObject" } }, "required": [ @@ -39,4 +30,5 @@ ], "type": "object" } + } } diff --git a/test/valid-data/type-indexed-access-type-union/schema.json b/test/valid-data/type-indexed-access-type-union/schema.json index e50c9aa17..713f4089e 100644 --- a/test/valid-data/type-indexed-access-type-union/schema.json +++ b/test/valid-data/type-indexed-access-type-union/schema.json @@ -11,5 +11,8 @@ "boolean" ] } + }, + "type": "object" } + } } diff --git a/test/valid-data/type-mapped-additional-props/schema.json b/test/valid-data/type-mapped-additional-props/schema.json index 121a479c8..153e1fd66 100644 --- a/test/valid-data/type-mapped-additional-props/schema.json +++ b/test/valid-data/type-mapped-additional-props/schema.json @@ -1,18 +1,18 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "$ref": "#/definitions/WithNumbers%3CTest%3E" - }, - "WithNumbers": { - "type": "object", - "additionalProperties": { - "type": [ - "string", - "number" - ] - } - } + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "$ref": "#/definitions/WithNumbers%3CTest%3E" + }, + "WithNumbers": { + "additionalProperties": { + "type": [ + "string", + "number" + ] + }, + "type": "object" } + } } diff --git a/test/valid-data/type-mapped-double-exclude/schema.json b/test/valid-data/type-mapped-double-exclude/schema.json index a8dac363c..99b1a9a9b 100644 --- a/test/valid-data/type-mapped-double-exclude/schema.json +++ b/test/valid-data/type-mapped-double-exclude/schema.json @@ -11,7 +11,21 @@ "description": "Bar", "type": "number" }, - "additionalProperties": false + { + "type": "null" + } + ] + }, + "foo": { + "anyOf": [ + { + "description": "Foo", + "type": "number" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -19,4 +33,5 @@ ], "type": "object" } + } } diff --git a/test/valid-data/type-mapped-exclude/schema.json b/test/valid-data/type-mapped-exclude/schema.json index 2d0250e9c..0daf24649 100644 --- a/test/valid-data/type-mapped-exclude/schema.json +++ b/test/valid-data/type-mapped-exclude/schema.json @@ -19,10 +19,5 @@ }, "additionalProperties": false } - }, - "required": [ - "bar" - ], - "type": "object" } } diff --git a/test/valid-data/type-mapped-generic/schema.json b/test/valid-data/type-mapped-generic/schema.json index cc2ef2888..e26ef3de4 100644 --- a/test/valid-data/type-mapped-generic/schema.json +++ b/test/valid-data/type-mapped-generic/schema.json @@ -1,35 +1,35 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$ref": "#/definitions/MyObject", - "definitions": { - "MyObject": { - "$ref": "#/definitions/NullableAndPartial%3CSomeInterface%3E" + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "$ref": "#/definitions/NullableAndPartial%3CSomeInterface%3E" + }, + "NullableAndPartial": { + "additionalProperties": false, + "properties": { + "bar": { + "enum": [ + "bar", + null + ], + "type": [ + "string", + "null" + ] }, - "NullableAndPartial": { - "type": "object", - "properties": { - "foo": { - "type": [ - "number", - "null" - ], - "enum": [ - 123, - null - ] - }, - "bar": { - "type": [ - "string", - "null" - ], - "enum": [ - "bar", - null - ] - } - }, - "additionalProperties": false + "foo": { + "enum": [ + 123, + null + ], + "type": [ + "number", + "null" + ] } + }, + "type": "object" } + } } diff --git a/test/valid-data/type-typeof-class/schema.json b/test/valid-data/type-typeof-class/schema.json index 0c009604b..8ea73aa67 100644 --- a/test/valid-data/type-typeof-class/schema.json +++ b/test/valid-data/type-typeof-class/schema.json @@ -23,4 +23,5 @@ ], "type": "object" } + } } diff --git a/test/valid-data/type-typeof-keys/schema.json b/test/valid-data/type-typeof-keys/schema.json index c90e5356e..206e4eece 100644 --- a/test/valid-data/type-typeof-keys/schema.json +++ b/test/valid-data/type-typeof-keys/schema.json @@ -9,4 +9,5 @@ ], "type": "string" } + } } diff --git a/yarn.lock b/yarn.lock index 3947e7f33..b93b58ea7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2544,6 +2544,13 @@ create-require@^1.1.0: resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== +cross-env@^7.0.3: + version "7.0.3" + resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf" + integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw== + dependencies: + cross-spawn "^7.0.1" + cross-spawn@^5.0.1: version "5.1.0" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" @@ -2564,7 +2571,7 @@ cross-spawn@^6.0.0: shebang-command "^1.2.0" which "^1.2.9" -cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3: +cross-spawn@^7.0.0, cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3: version "7.0.3" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6" integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w== From 7b39880343b68fe2c606894fac318202f0565770 Mon Sep 17 00:00:00 2001 From: Shishir Date: Sat, 20 Feb 2021 14:26:38 -0800 Subject: [PATCH 7/9] fix: linter error --- src/SchemaGenerator.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index e7586ce99..5224fb748 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -13,9 +13,6 @@ import { removeUnreachable } from "./Utils/removeUnreachable"; import { Config } from "./Config"; import { hasJsDocTag } from "./Utils/hasJsDocTag"; import { JSONSchema7, JSONSchema7Definition } from "json-schema"; -import { derefType } from "./Utils/derefType"; -import { AliasType } from "./Type/AliasType"; -import { ObjectType } from "./Type/ObjectType"; export class SchemaGenerator { public constructor( From 8a5b517bb5387c54bfc4139d4f9da4445863741c Mon Sep 17 00:00:00 2001 From: Shishir Date: Sat, 20 Feb 2021 15:56:23 -0800 Subject: [PATCH 8/9] chore: cleanup package.json --- package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/package.json b/package.json index 12b65848f..31ef78a48 100644 --- a/package.json +++ b/package.json @@ -87,8 +87,6 @@ "test:coverage": "yarn jest test/ --collectCoverage=true", "test:update": "cross-env UPDATE_SCHEMA=true yarn test:fast", "debug": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts", - "dbg": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts -p test/valid-data/type-aliases-recursive-anonymous/main.ts -t MyAlias", - "dbg-vega": "node -r ts-node/register --inspect-brk ts-json-schema-generator.ts -p ../../../tmp/vega-lite/src/index.ts -t TopLevelSpec --no-type-check --no-ref-encode > ../../../tmp/vega-lite/build/vega-lite-schema2.json", "run": "ts-node ts-json-schema-generator.ts" }, "release": { From db3ddc62329e20e96b1329f6034790ec3011e280 Mon Sep 17 00:00:00 2001 From: Shishir Date: Sat, 20 Feb 2021 16:27:31 -0800 Subject: [PATCH 9/9] chore: reorganize code --- src/SchemaGenerator.ts | 115 +---------------------------------- src/Utils/resolveIdRefs.ts | 57 +++++++++++++++++ src/Utils/unambiguousName.ts | 59 ++++++++++++++++++ 3 files changed, 118 insertions(+), 113 deletions(-) create mode 100644 src/Utils/resolveIdRefs.ts create mode 100644 src/Utils/unambiguousName.ts diff --git a/src/SchemaGenerator.ts b/src/SchemaGenerator.ts index 5224fb748..25fbb193e 100644 --- a/src/SchemaGenerator.ts +++ b/src/SchemaGenerator.ts @@ -13,6 +13,8 @@ import { removeUnreachable } from "./Utils/removeUnreachable"; import { Config } from "./Config"; import { hasJsDocTag } from "./Utils/hasJsDocTag"; import { JSONSchema7, JSONSchema7Definition } from "json-schema"; +import { unambiguousName } from "./Utils/unambiguousName"; +import { resolveIdRefs } from "./Utils/resolveIdRefs"; export class SchemaGenerator { public constructor( @@ -177,116 +179,3 @@ export class SchemaGenerator { return typeChecker.getFullyQualifiedName(symbol).replace(/".*"\./, ""); } } - -/** - * Given a set of paths, will strip the common-prefix, and return an array of remaining substrings. - * Also removes any file extensions, and replaces any '/' with '-' in the path. - * Each return value can be used as a name prefix for disambiguation. - * The returned prefixes array maintains input order. - */ -function getCommonPrefixes(paths: string[]) { - // clone before sorting to maintain input order. - const sorted = [...paths].sort(); - const shortest = sorted[0]; - const second = sorted[sorted.length - 1]; - const maxPrefix = shortest.length; - let pos = 0; - let path_pos = 0; - while (pos < maxPrefix && shortest.charAt(pos) === second.charAt(pos)) { - if (shortest.charAt(pos) === "/") { - path_pos = pos; - } - pos++; - } - return paths.map((p) => - p - .substr(path_pos + 1) - .replace(/\//g, "__") - .replace(/\.[^.]+$/, "") - ); -} - -function unambiguousName(child: DefinitionType, isRoot: boolean, peers: DefinitionType[]): string { - if (peers.length === 1 || isRoot) { - return child.getName(); - } else if (child.getType().getSrcFileName()) { - let index = -1; - - // filter unique peers to be those that have srcFileNames. - // Intermediate Types - AnnotationTypes, UnionTypes, do not have sourceFileNames - const uniques = peers.filter((peer) => peer.getType().getSrcFileName()); - if (uniques.length === 1) { - return uniques[0].getName(); - } - const srcPaths = uniques.map((peer: DefinitionType, count) => { - index = child === peer ? count : index; - return peer.getType().getSrcFileName()!; - }); - const prefixes = getCommonPrefixes(srcPaths); - return `${prefixes[index]}-${child.getName()}`; - } else { - // intermediate type. - const name = child.getName(); - // TODO: Perhaps we should maintain a name-id map, and throw a duplicate error on collision. - if (name === undefined) { - // this might be unreachable code. - throw new Error(`Unable to disambiguate types`); - } - return name; - } -} - -/** - * Resolve all `#/definitions/...` and `$ref` in schema with appropriate disambiguated names - */ -function resolveIdRefs( - schema: JSONSchema7Definition, - idNameMap: Map, - encodeRefs: boolean -): JSONSchema7Definition { - if (!schema || typeof schema === "boolean") { - return schema; - } - const { $ref, allOf, oneOf, anyOf, not, properties, items, definitions, additionalProperties, ...rest } = schema; - const result: JSONSchema7Definition = { ...rest }; - if ($ref) { - // THE Money Shot. - const id = encodeRefs ? decodeURIComponent($ref.slice(14)) : $ref.slice(14); - const name = idNameMap.get(id); - result.$ref = `#/definitions/${encodeRefs ? encodeURIComponent(name!) : name}`; - } - if (definitions) { - result.definitions = Object.entries(definitions).reduce((acc, [prop, value]) => { - const name = idNameMap.get(prop)!; - acc[name] = resolveIdRefs(value, idNameMap, encodeRefs); - return acc; - }, {} as StringMap); - } - if (properties) { - result.properties = Object.entries(properties).reduce((acc, [prop, value]) => { - acc[prop] = resolveIdRefs(value, idNameMap, encodeRefs); - return acc; - }, {} as StringMap); - } - if (additionalProperties || additionalProperties === false) { - result.additionalProperties = resolveIdRefs(additionalProperties, idNameMap, encodeRefs); - } - if (items) { - result.items = Array.isArray(items) - ? items.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)) - : resolveIdRefs(items, idNameMap, encodeRefs); - } - if (allOf) { - result.allOf = allOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); - } - if (anyOf) { - result.anyOf = anyOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); - } - if (oneOf) { - result.oneOf = oneOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); - } - if (not) { - result.not = resolveIdRefs(not, idNameMap, encodeRefs); - } - return result; -} diff --git a/src/Utils/resolveIdRefs.ts b/src/Utils/resolveIdRefs.ts new file mode 100644 index 000000000..dabdc1315 --- /dev/null +++ b/src/Utils/resolveIdRefs.ts @@ -0,0 +1,57 @@ +import { JSONSchema7Definition } from "json-schema"; +import { StringMap } from "./StringMap"; + +/** + * Resolve all `#/definitions/...` and `$ref` in schema with appropriate disambiguated names + */ +export function resolveIdRefs( + schema: JSONSchema7Definition, + idNameMap: Map, + encodeRefs: boolean +): JSONSchema7Definition { + if (!schema || typeof schema === "boolean") { + return schema; + } + const { $ref, allOf, oneOf, anyOf, not, properties, items, definitions, additionalProperties, ...rest } = schema; + const result: JSONSchema7Definition = { ...rest }; + if ($ref) { + // THE Money Shot. + const id = encodeRefs ? decodeURIComponent($ref.slice(14)) : $ref.slice(14); + const name = idNameMap.get(id); + result.$ref = `#/definitions/${encodeRefs ? encodeURIComponent(name!) : name}`; + } + if (definitions) { + result.definitions = Object.entries(definitions).reduce((acc, [prop, value]) => { + const name = idNameMap.get(prop)!; + acc[name] = resolveIdRefs(value, idNameMap, encodeRefs); + return acc; + }, {} as StringMap); + } + if (properties) { + result.properties = Object.entries(properties).reduce((acc, [prop, value]) => { + acc[prop] = resolveIdRefs(value, idNameMap, encodeRefs); + return acc; + }, {} as StringMap); + } + if (additionalProperties || additionalProperties === false) { + result.additionalProperties = resolveIdRefs(additionalProperties, idNameMap, encodeRefs); + } + if (items) { + result.items = Array.isArray(items) + ? items.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)) + : resolveIdRefs(items, idNameMap, encodeRefs); + } + if (allOf) { + result.allOf = allOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); + } + if (anyOf) { + result.anyOf = anyOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); + } + if (oneOf) { + result.oneOf = oneOf.map((el) => resolveIdRefs(el, idNameMap, encodeRefs)); + } + if (not) { + result.not = resolveIdRefs(not, idNameMap, encodeRefs); + } + return result; +} diff --git a/src/Utils/unambiguousName.ts b/src/Utils/unambiguousName.ts new file mode 100644 index 000000000..bec44211b --- /dev/null +++ b/src/Utils/unambiguousName.ts @@ -0,0 +1,59 @@ +import { DefinitionType } from "../Type/DefinitionType"; + +/** + * Given a set of paths, will strip the common-prefix, and return an array of remaining substrings. + * Also removes any file extensions, and replaces any '/' with '-' in the path. + * Each return value can be used as a name prefix for disambiguation. + * The returned prefixes array maintains input order. + */ +function getCommonPrefixes(paths: string[]) { + // clone before sorting to maintain input order. + const sorted = [...paths].sort(); + const shortest = sorted[0]; + const second = sorted[sorted.length - 1]; + const maxPrefix = shortest.length; + let pos = 0; + let path_pos = 0; + while (pos < maxPrefix && shortest.charAt(pos) === second.charAt(pos)) { + if (shortest.charAt(pos) === "/") { + path_pos = pos; + } + pos++; + } + return paths.map((p) => + p + .substr(path_pos + 1) + .replace(/\//g, "__") + .replace(/\.[^.]+$/, "") + ); +} + +export function unambiguousName(child: DefinitionType, isRoot: boolean, peers: DefinitionType[]): string { + if (peers.length === 1 || isRoot) { + return child.getName(); + } else if (child.getType().getSrcFileName()) { + let index = -1; + + // filter unique peers to be those that have srcFileNames. + // Intermediate Types - AnnotationTypes, UnionTypes, do not have sourceFileNames + const uniques = peers.filter((peer) => peer.getType().getSrcFileName()); + if (uniques.length === 1) { + return uniques[0].getName(); + } + const srcPaths = uniques.map((peer: DefinitionType, count) => { + index = child === peer ? count : index; + return peer.getType().getSrcFileName()!; + }); + const prefixes = getCommonPrefixes(srcPaths); + return `${prefixes[index]}-${child.getName()}`; + } else { + // intermediate type. + const name = child.getName(); + // TODO: Perhaps we should maintain a name-id map, and throw a duplicate error on collision. + if (name === undefined) { + // this might be unreachable code. + throw new Error(`Unable to disambiguate types`); + } + return name; + } +}