diff --git a/src/normalizer.ts b/src/normalizer.ts index 88c2d492..f03f8847 100644 --- a/src/normalizer.ts +++ b/src/normalizer.ts @@ -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 diff --git a/test/__snapshots__/test/test.ts.md b/test/__snapshots__/test/test.ts.md index 56e8e749..cec0a479 100644 --- a/test/__snapshots__/test/test.ts.md +++ b/test/__snapshots__/test/test.ts.md @@ -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 @@ -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 diff --git a/test/__snapshots__/test/test.ts.snap b/test/__snapshots__/test/test.ts.snap index 81a1c6eb..75fb7e08 100644 Binary files a/test/__snapshots__/test/test.ts.snap and b/test/__snapshots__/test/test.ts.snap differ diff --git a/test/normalizer/constToEnum.json b/test/normalizer/constToEnum.json new file mode 100644 index 00000000..9e3567c6 --- /dev/null +++ b/test/normalizer/constToEnum.json @@ -0,0 +1,11 @@ +{ + "name": "Normalize const to singleton enum", + "in": { + "id": "foo", + "const": "foobar" + }, + "out": { + "id": "foo", + "enum": ["foobar"] + } +} diff --git a/test/normalizer/defsToDefinitions.json b/test/normalizer/defsToDefinitions.json new file mode 100644 index 00000000..050ca47f --- /dev/null +++ b/test/normalizer/defsToDefinitions.json @@ -0,0 +1,15 @@ +{ + "name": "Normalize $defs to definitions", + "in": { + "id": "foo", + "$defs" : { + "bar": "baz" + } + }, + "out": { + "id": "foo", + "definitions" : { + "bar": "baz" + } + } +}