From edb6918c6ac37b5d17ef4819204886d3ac21eb2a Mon Sep 17 00:00:00 2001 From: Mike Ralphson Date: Thu, 6 Dec 2018 00:09:48 +0000 Subject: [PATCH] rewrite const as single element enum --- index.js | 11 +++++++++++ test/const.test.js | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/const.test.js diff --git a/index.js b/index.js index f89c908..af590c1 100644 --- a/index.js +++ b/index.js @@ -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); if (typeof schema['patternProperties'] === 'object') { @@ -137,7 +138,17 @@ 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; } module.exports = convert; + diff --git a/test/const.test.js b/test/const.test.js new file mode 100644 index 0000000..be0ca20 --- /dev/null +++ b/test/const.test.js @@ -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'); +});