From db6fadbe33e064699761871ceadce6f63da06fa3 Mon Sep 17 00:00:00 2001 From: sixlive Date: Sat, 9 Feb 2019 07:28:19 -0500 Subject: [PATCH 1/3] Adds draft 6 examples --- index.js | 13 +++++++++++-- test/examples.test.js | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/examples.test.js diff --git a/index.js b/index.js index 1d902cf..7a1834f 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']) { + 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', + }); +}); From 04be69f4a4e3e60cdc0ce08702a467815540aa14 Mon Sep 17 00:00:00 2001 From: sixlive Date: Mon, 18 Feb 2019 12:24:59 -0500 Subject: [PATCH 2/3] Added an additional check for array accessibility --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7a1834f..71d8fab 100644 --- a/index.js +++ b/index.js @@ -150,7 +150,7 @@ function convertPatternProperties(schema) { } function convertExamples(schema) { - if (schema['examples']) { + if (schema['examples'] && Array.isArray(schema['examples'])) { schema['example'] = schema['examples'][0]; delete schema['examples']; } From 441c1174420cd67e40b0e2e527e962e257f2089d Mon Sep 17 00:00:00 2001 From: TJ Miller Date: Thu, 11 Jul 2019 12:07:01 -0400 Subject: [PATCH 3/3] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 71d8fab..4422cdb 100644 --- a/index.js +++ b/index.js @@ -32,8 +32,8 @@ function convertSchema(schema, path, parent, parentPath) { schema = rewriteConst(schema); schema = convertDependencies(schema); schema = rewriteIfThenElse(schema); - schema = rewriteExclusiveMinMax(schema); - schema = convertExamples(schema); + schema = rewriteExclusiveMinMax(schema); + schema = convertExamples(schema); if (typeof schema['patternProperties'] === 'object') { schema = convertPatternProperties(schema);