Skip to content
This repository was archived by the owner on Jun 22, 2023. It is now read-only.

Commit ec78099

Browse files
authored
Add normalizer rules for $defs and const (bcherny#290)
* Add normalizer rules for $defs and const * Add tests for $defs and const normalizers
1 parent f35a325 commit ec78099

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

src/normalizer.ts

+14
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ rules.set('Make extends always an array, if it is defined', schema => {
131131
}
132132
})
133133

134+
rules.set('Transform $defs to definitions', schema => {
135+
if (schema.$defs) {
136+
schema.definitions = schema.$defs
137+
delete schema.$defs
138+
}
139+
})
140+
141+
rules.set('Transform const to singleton enum', schema => {
142+
if (schema.const) {
143+
schema.enum = [schema.const]
144+
delete schema.const
145+
}
146+
})
147+
134148
export function normalize(rootSchema: LinkedJSONSchema, filename: string, options: Options): NormalizedJSONSchema {
135149
rules.forEach(rule => traverse(rootSchema, schema => rule(schema, filename, options)))
136150
return rootSchema as NormalizedJSONSchema

test/__snapshots__/test/test.ts.md

+22
Original file line numberDiff line numberDiff line change
@@ -7759,6 +7759,17 @@ Generated by [AVA](https://avajs.dev).
77597759
"required": []␊
77607760
}`
77617761

7762+
## Normalize const to singleton enum
7763+
7764+
> Snapshot 1
7765+
7766+
`{␊
7767+
"id": "foo",␊
7768+
"enum": [␊
7769+
"foobar"␊
7770+
]␊
7771+
}`
7772+
77627773
## Default additionalProperties to true
77637774

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

7821+
## Normalize $defs to definitions
7822+
7823+
> Snapshot 1
7824+
7825+
`{␊
7826+
"id": "foo",␊
7827+
"definitions": {␊
7828+
"bar": "baz"␊
7829+
}␊
7830+
}`
7831+
78107832
## Destructure unary types
78117833

78127834
> Snapshot 1

test/__snapshots__/test/test.ts.snap

81 Bytes
Binary file not shown.

test/normalizer/constToEnum.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Normalize const to singleton enum",
3+
"in": {
4+
"id": "foo",
5+
"const": "foobar"
6+
},
7+
"out": {
8+
"id": "foo",
9+
"enum": ["foobar"]
10+
}
11+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Normalize $defs to definitions",
3+
"in": {
4+
"id": "foo",
5+
"$defs" : {
6+
"bar": "baz"
7+
}
8+
},
9+
"out": {
10+
"id": "foo",
11+
"definitions" : {
12+
"bar": "baz"
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)