Skip to content

Commit 47c50a3

Browse files
fix(nullable-enum): avoid adding duplicate null item (#35)
* Avoid adding a second null item in enum values Fix #34 * Added Unit Test for duplicate Null * Delete yarn.lock Co-authored-by: Gabriel Santos-Blanchet <[email protected]>
1 parent 88d1729 commit 47c50a3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/converters/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function convertTypes (schema) {
127127
if (schema.type !== undefined && schema.nullable === true) {
128128
schema.type = [schema.type, 'null']
129129

130-
if (Array.isArray(schema.enum)) {
130+
if (Array.isArray(schema.enum) && !schema.enum.includes(null)) {
131131
schema.enum = schema.enum.concat([null])
132132
}
133133
}

test/nullable.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('handles nullable without enum', function (assert) {
3838
test('handles nullable with enum', function (assert) {
3939
var schema, result, expected;
4040

41-
assert.plan(2)
41+
assert.plan(3)
4242

4343
schema = {
4444
type: 'string',
@@ -56,6 +56,22 @@ test('handles nullable with enum', function (assert) {
5656

5757
assert.deepEqual(result, expected, 'nullable converted')
5858

59+
schema = {
60+
type: 'string',
61+
enum: ['a', 'b', null],
62+
nullable: true
63+
}
64+
65+
result = convert(schema)
66+
67+
expected = {
68+
$schema: 'http://json-schema.org/draft-04/schema#',
69+
type: ['string', 'null'],
70+
enum: ['a', 'b', null]
71+
}
72+
73+
assert.deepEqual(result, expected, 'nullable converted, no duplicate')
74+
5975
schema = {
6076
type: 'string',
6177
enum: ['a', 'b'],

0 commit comments

Comments
 (0)