Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for draft-6 examples #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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);
sixlive marked this conversation as resolved.
Show resolved Hide resolved
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']) {
sixlive marked this conversation as resolved.
Show resolved Hide resolved
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',
});
});