Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 ];
Expand Down Expand Up @@ -191,4 +201,3 @@ function rewriteExclusiveMinMax(schema) {
}

module.exports = convert;

20 changes: 20 additions & 0 deletions test/examples.test.js
Original file line number Diff line number Diff line change
@@ -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',
});
});