Skip to content

Commit

Permalink
Merge pull request #14 from MikeRalphson/const
Browse files Browse the repository at this point in the history
rewrite const as single element enum
  • Loading branch information
Phil Sturgeon authored Dec 18, 2018
2 parents aae1805 + edb6918 commit 28cf9c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function stripIllegalKeywords(schema) {
function convertSchema(schema, path, parent, parentPath) {
schema = stripIllegalKeywords(schema);
schema = convertTypes(schema);
schema = rewriteConst(schema);
schema = convertDependencies(schema);
schema = rewriteIfThenElse(schema);
schema = rewriteExclusiveMinMax(schema);
Expand Down Expand Up @@ -143,6 +144,15 @@ function convertTypes(schema) {
function convertPatternProperties(schema) {
schema['x-patternProperties'] = schema['patternProperties'];
delete schema['patternProperties'];
if (typeof schema.additionalProperties === 'undefined') schema.additionalProperties = true;
return schema;
}

function rewriteConst(schema) {
if (schema.const) {
schema.enum = [ schema.const ];
delete schema.const;
}
return schema;
}

Expand Down
21 changes: 21 additions & 0 deletions test/const.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const convert = require('../');
const should = require('should');

it('const', () => {
const schema = {
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'string',
const: 'hello'
};

const result = convert(schema);

const expected = {
type: 'string',
enum: [ 'hello' ]
};

should(result).deepEqual(expected, 'converted');
});

0 comments on commit 28cf9c0

Please sign in to comment.