Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ rules.set('Make extends always an array, if it is defined', schema => {
}
})

rules.set('Transform $defs to definitions', schema => {
if (schema.$defs) {
schema.definitions = schema.$defs
delete schema.$defs
}
})

rules.set('Transform const to singleton enum', schema => {
if (schema.const) {
schema.enum = [schema.const]
delete schema.const
}
})

export function normalize(rootSchema: LinkedJSONSchema, filename: string, options: Options): NormalizedJSONSchema {
rules.forEach(rule => traverse(rootSchema, schema => rule(schema, filename, options)))
return rootSchema as NormalizedJSONSchema
Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -7759,6 +7759,17 @@ Generated by [AVA](https://avajs.dev).
"required": []␊
}`

## Normalize const to singleton enum

> Snapshot 1

`{␊
"id": "foo",␊
"enum": [␊
"foobar"␊
]␊
}`

## Default additionalProperties to true

> Snapshot 1
Expand Down Expand Up @@ -7807,6 +7818,17 @@ Generated by [AVA](https://avajs.dev).
"id": "DefaultID"␊
}`

## Normalize $defs to definitions

> Snapshot 1

`{␊
"id": "foo",␊
"definitions": {␊
"bar": "baz"␊
}␊
}`

## Destructure unary types

> Snapshot 1
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
11 changes: 11 additions & 0 deletions test/normalizer/constToEnum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Normalize const to singleton enum",
"in": {
"id": "foo",
"const": "foobar"
},
"out": {
"id": "foo",
"enum": ["foobar"]
}
}
15 changes: 15 additions & 0 deletions test/normalizer/defsToDefinitions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Normalize $defs to definitions",
"in": {
"id": "foo",
"$defs" : {
"bar": "baz"
}
},
"out": {
"id": "foo",
"definitions" : {
"bar": "baz"
}
}
}