Skip to content

Commit 34de194

Browse files
author
Boris Cherny
committed
cleanup: use defs internally, not definitions
1 parent dc0f63f commit 34de194

File tree

7 files changed

+29
-17
lines changed

7 files changed

+29
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
9393
| maxItems | number | `20` | Maximum number of unioned tuples to emit when representing bounded-size array types, before falling back to emitting unbounded arrays. Increase this to improve precision of emitted types, decrease it to improve performance, or set it to `-1` to ignore `maxItems`.
9494
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
9595
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
96-
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
96+
| unreachableDefinitions | boolean | `false` | Generates code for `$defs` that aren't referenced by the schema. |
9797
| strictIndexSignatures | boolean | `false` | Append all index signatures with `\| undefined` so that they are strictly typed. |
9898
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
9999
## CLI

src/normalizer.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {JSONSchemaTypeName, LinkedJSONSchema, NormalizedJSONSchema, Parent} from
22
import {appendToDescription, escapeBlockComment, isSchemaLike, justName, toSafeString, traverse} from './utils'
33
import {Options} from './'
44
import {DereferencedPaths} from './resolver'
5+
import {isDeepStrictEqual} from 'util'
56

67
type Rule = (
78
schema: LinkedJSONSchema,
@@ -202,10 +203,15 @@ rules.set('Make extends always an array, if it is defined', schema => {
202203
}
203204
})
204205

205-
rules.set('Transform $defs to definitions', schema => {
206-
if (schema.$defs) {
207-
schema.definitions = schema.$defs
208-
delete schema.$defs
206+
rules.set('Transform definitions to $defs', (schema, fileName) => {
207+
if (schema.definitions && schema.$defs && !isDeepStrictEqual(schema.definitions, schema.$defs)) {
208+
throw ReferenceError(
209+
`Schema must define either definitions or $defs, not both. Given id=${schema.id} in ${fileName}`
210+
)
211+
}
212+
if (schema.definitions) {
213+
schema.$defs = schema.definitions
214+
delete schema.definitions
209215
}
210216
})
211217

src/parser.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ via the \`patternProperty\` "${key}".`
384384

385385
if (options.unreachableDefinitions) {
386386
asts = asts.concat(
387-
map(schema.definitions, (value, key: string) => {
387+
map(schema.$defs, (value, key: string) => {
388388
const ast = parse(value, options, key, processed, usedNames)
389389
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema
390390
via the \`definition\` "${key}".`
@@ -453,7 +453,7 @@ function getDefinitions(
453453
}
454454
if (isPlainObject(schema)) {
455455
return {
456-
...(isSchema && hasDefinitions(schema) ? schema.definitions : {}),
456+
...(isSchema && hasDefinitions(schema) ? schema.$defs : {}),
457457
...Object.keys(schema).reduce<Definitions>(
458458
(prev, cur) => ({
459459
...prev,
@@ -472,5 +472,5 @@ const getDefinitionsMemoized = memoize(getDefinitions)
472472
* TODO: Reduce rate of false positives
473473
*/
474474
function hasDefinitions(schema: LinkedJSONSchema): schema is JSONSchemaWithDefinitions {
475-
return 'definitions' in schema
475+
return '$defs' in schema
476476
}

src/types/JSONSchema.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface NormalizedJSONSchema extends LinkedJSONSchema {
7070
additionalProperties: boolean | NormalizedJSONSchema
7171
extends?: string[]
7272
items?: NormalizedJSONSchema | NormalizedJSONSchema[]
73-
definitions?: {
73+
$defs?: {
7474
[k: string]: NormalizedJSONSchema
7575
}
7676
properties?: {
@@ -89,6 +89,7 @@ export interface NormalizedJSONSchema extends LinkedJSONSchema {
8989
required: string[]
9090

9191
// Removed by normalizer
92+
definitions: never
9293
id: never
9394
}
9495

@@ -108,7 +109,7 @@ export interface SchemaSchema extends NormalizedJSONSchema {
108109
}
109110

110111
export interface JSONSchemaWithDefinitions extends NormalizedJSONSchema {
111-
definitions: {
112+
$defs: {
112113
[k: string]: NormalizedJSONSchema
113114
}
114115
}

src/utils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function Try<T>(fn: () => T, err: (e: Error) => any): T {
1414
// keys that shouldn't be traversed by the catchall step
1515
const BLACKLISTED_KEYS = new Set([
1616
'id',
17+
'$defs',
1718
'$id',
1819
'$schema',
1920
'title',
@@ -121,6 +122,9 @@ export function traverse(
121122
if (schema.definitions) {
122123
traverseObjectKeys(schema.definitions, callback, processed)
123124
}
125+
if (schema.$defs) {
126+
traverseObjectKeys(schema.$defs, callback, processed)
127+
}
124128
if (schema.not) {
125129
traverse(schema.not, callback, processed)
126130
}
@@ -362,13 +366,14 @@ export function isSchemaLike(schema: LinkedJSONSchema) {
362366
}
363367

364368
const JSON_SCHEMA_KEYWORDS = [
369+
'$defs',
365370
'allOf',
366371
'anyOf',
372+
'definitions',
367373
'dependencies',
368374
'enum',
369-
'oneOf',
370-
'definitions',
371375
'not',
376+
'oneOf',
372377
'patternProperties',
373378
'properties',
374379
'required'

test/normalizer/destructureUnaryTypes.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
},
2727
"out": {
2828
"$id": "foo",
29-
"type": "object",
30-
"definitions": {
29+
"$defs": {
3130
"a": {
3231
"type": "integer",
3332
"$id": "a"
@@ -39,6 +38,7 @@
3938
"$id": "b"
4039
}
4140
},
41+
"type": "object",
4242
"additionalProperties": true,
4343
"required": []
4444
}

test/normalizer/normalizeDefs.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "Normalize $defs to definitions",
2+
"name": "Normalize definitions to $defs",
33
"in": {
44
"$id": "foo",
5-
"$defs": {
5+
"definitions": {
66
"bar": "baz"
77
}
88
},
99
"out": {
1010
"$id": "foo",
11-
"definitions": {
11+
"$defs": {
1212
"bar": "baz"
1313
}
1414
}

0 commit comments

Comments
 (0)