Skip to content

Commit 478016d

Browse files
authored
Merge pull request #22 from mikunn/improve-support-for-nullable
Inject null in enum (if there's one) if nullable is true (closes #18)
2 parents dace43b + 841d56c commit 478016d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/converters/schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ function convertProperties(properties, options) {
9292
function convertTypes(schema) {
9393
if (schema.type !== undefined && schema.nullable === true) {
9494
schema.type = [schema.type, 'null'];
95+
96+
if (Array.isArray(schema.enum)) {
97+
schema.enum = schema.enum.concat([null]);
98+
}
9599
}
96100

97101
return schema;

test/nullable.test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var test = require('tape')
22
, convert = require('../')
33
;
44

5-
test('handles nullable', function(assert) {
5+
test('handles nullable without enum', function(assert) {
66
var schema
77
, result
88
, expected
@@ -38,3 +38,44 @@ test('handles nullable', function(assert) {
3838

3939
assert.deepEqual(result, expected, 'nullable removed, type untouched');
4040
});
41+
42+
test('handles nullable with enum', function(assert) {
43+
var schema
44+
, result
45+
, expected
46+
;
47+
48+
assert.plan(2);
49+
50+
schema = {
51+
type: 'string',
52+
enum: ['a', 'b'],
53+
nullable: true
54+
};
55+
56+
result = convert(schema);
57+
58+
expected = {
59+
$schema: 'http://json-schema.org/draft-04/schema#',
60+
type: ['string', 'null'],
61+
enum: ['a', 'b', null]
62+
};
63+
64+
assert.deepEqual(result, expected, 'nullable converted');
65+
66+
schema = {
67+
type: 'string',
68+
enum: ['a', 'b'],
69+
nullable: false,
70+
};
71+
72+
result = convert(schema);
73+
74+
expected = {
75+
$schema: 'http://json-schema.org/draft-04/schema#',
76+
type: 'string',
77+
enum: ['a', 'b'],
78+
};
79+
80+
assert.deepEqual(result, expected, 'nullable removed, type untouched');
81+
});

0 commit comments

Comments
 (0)