diff --git a/index.js b/index.js index 1d902cf..4422cdb 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,8 @@ function convertSchema(schema, path, parent, parentPath) { schema = rewriteConst(schema); schema = convertDependencies(schema); schema = rewriteIfThenElse(schema); - schema = rewriteExclusiveMinMax(schema); + schema = rewriteExclusiveMinMax(schema); + schema = convertExamples(schema); if (typeof schema['patternProperties'] === 'object') { schema = convertPatternProperties(schema); @@ -148,6 +149,15 @@ function convertPatternProperties(schema) { return schema; } +function convertExamples(schema) { + if (schema['examples'] && Array.isArray(schema['examples'])) { + schema['example'] = schema['examples'][0]; + delete schema['examples']; + } + + return schema; +} + function rewriteConst(schema) { if (schema.const) { schema.enum = [ schema.const ]; @@ -191,4 +201,3 @@ function rewriteExclusiveMinMax(schema) { } module.exports = convert; - diff --git a/test/examples.test.js b/test/examples.test.js new file mode 100644 index 0000000..4d62f47 --- /dev/null +++ b/test/examples.test.js @@ -0,0 +1,20 @@ +'use strict'; + +const convert = require('../'); +const should = require('should'); + +it('uses the first example from a schema', () => { + const schema = { + $schema: 'http://json-schema.org/draft-06/schema#', + examples: [ + 'foo', + 'bar' + ] + }; + + const result = convert(schema); + + should(result).deepEqual({ + example: 'foo', + }); +});