Skip to content

Commit 28cf9c0

Browse files
author
Phil Sturgeon
authored
Merge pull request #14 from MikeRalphson/const
rewrite const as single element enum
2 parents aae1805 + edb6918 commit 28cf9c0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

index.js

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function stripIllegalKeywords(schema) {
2929
function convertSchema(schema, path, parent, parentPath) {
3030
schema = stripIllegalKeywords(schema);
3131
schema = convertTypes(schema);
32+
schema = rewriteConst(schema);
3233
schema = convertDependencies(schema);
3334
schema = rewriteIfThenElse(schema);
3435
schema = rewriteExclusiveMinMax(schema);
@@ -143,6 +144,15 @@ function convertTypes(schema) {
143144
function convertPatternProperties(schema) {
144145
schema['x-patternProperties'] = schema['patternProperties'];
145146
delete schema['patternProperties'];
147+
if (typeof schema.additionalProperties === 'undefined') schema.additionalProperties = true;
148+
return schema;
149+
}
150+
151+
function rewriteConst(schema) {
152+
if (schema.const) {
153+
schema.enum = [ schema.const ];
154+
delete schema.const;
155+
}
146156
return schema;
147157
}
148158

test/const.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const convert = require('../');
4+
const should = require('should');
5+
6+
it('const', () => {
7+
const schema = {
8+
$schema: 'http://json-schema.org/draft-04/schema#',
9+
type: 'string',
10+
const: 'hello'
11+
};
12+
13+
const result = convert(schema);
14+
15+
const expected = {
16+
type: 'string',
17+
enum: [ 'hello' ]
18+
};
19+
20+
should(result).deepEqual(expected, 'converted');
21+
});

0 commit comments

Comments
 (0)