Skip to content

Commit a9dfda8

Browse files
authored
fix enum without type (#254)
1 parent 2082eb8 commit a9dfda8

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ const objectKeywords = [
165165
'properties',
166166
'patternProperties',
167167
'additionalProperties',
168-
'dependencies'
168+
'dependencies',
169+
'enum'
169170
]
170171

171172
const arrayKeywords = [
@@ -1105,6 +1106,10 @@ function nested (laterCode, name, key, location, subKey, isArray) {
11051106
var inferredType = inferTypeByKeyword(schema)
11061107
if (inferredType) {
11071108
schema.type = inferredType
1109+
1110+
if (inferredType === 'object' && schema.enum && !Object.hasOwnProperty.call(schema, 'additionalProperties')) {
1111+
schema.additionalProperties = true
1112+
}
11081113
}
11091114
}
11101115

test/additionalProperties.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,36 @@ test('field passed to fastSafeStringify as undefined should be removed', (t) =>
251251
const obj = { ap: { value: 'string', someNumber: undefined } }
252252
t.equal('{"ap":{"value":"string"}}', stringify(obj))
253253
})
254+
255+
test('property without type but with enum, will acts as additionalProperties', (t) => {
256+
t.plan(1)
257+
const stringify = build({
258+
title: 'automatic additionalProperties',
259+
type: 'object',
260+
properties: {
261+
ap: {
262+
enum: ['foobar', 42, ['foo', 'bar'], {}]
263+
}
264+
}
265+
})
266+
267+
const obj = { ap: { additional: 'field' } }
268+
t.equal('{"ap":{"additional":"field"}}', stringify(obj))
269+
})
270+
271+
test('property without type but with enum, will acts as additionalProperties without overwriting', (t) => {
272+
t.plan(1)
273+
const stringify = build({
274+
title: 'automatic additionalProperties',
275+
type: 'object',
276+
properties: {
277+
ap: {
278+
additionalProperties: false,
279+
enum: ['foobar', 42, ['foo', 'bar'], {}]
280+
}
281+
}
282+
})
283+
284+
const obj = { ap: { additional: 'field' } }
285+
t.equal('{"ap":{}}', stringify(obj))
286+
})

0 commit comments

Comments
 (0)