Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normalizer rules for $defs and const #290

Merged
merged 2 commits into from
Jan 4, 2021
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"
}
}
}