diff --git a/src/normalizer.ts b/src/normalizer.ts index f03f8847..6ba6c006 100644 --- a/src/normalizer.ts +++ b/src/normalizer.ts @@ -139,7 +139,7 @@ rules.set('Transform $defs to definitions', schema => { }) rules.set('Transform const to singleton enum', schema => { - if (schema.const) { + if (schema.const !== undefined) { schema.enum = [schema.const] delete schema.const } diff --git a/test/__snapshots__/test/test.ts.md b/test/__snapshots__/test/test.ts.md index 1084c814..7e03a2c8 100644 --- a/test/__snapshots__/test/test.ts.md +++ b/test/__snapshots__/test/test.ts.md @@ -9010,3 +9010,14 @@ Generated by [AVA](https://avajs.dev). [k: string]: unknown;␊ }␊ ` + +## Normalize empty const to singleton enum + +> Snapshot 1 + + `{␊ + "id": "foo",␊ + "enum": [␊ + ""␊ + ]␊ + }` diff --git a/test/__snapshots__/test/test.ts.snap b/test/__snapshots__/test/test.ts.snap index ae337649..32b63915 100644 Binary files a/test/__snapshots__/test/test.ts.snap and b/test/__snapshots__/test/test.ts.snap differ diff --git a/test/normalizer/emptyStringConstToEnum.json b/test/normalizer/emptyStringConstToEnum.json new file mode 100644 index 00000000..87293397 --- /dev/null +++ b/test/normalizer/emptyStringConstToEnum.json @@ -0,0 +1,11 @@ +{ + "name": "Normalize empty const to singleton enum", + "in": { + "id": "foo", + "const": "" + }, + "out": { + "id": "foo", + "enum": [""] + } +}