From 6df4888c885343b13713045d8c81d292d55dab04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alvaro=20Cabrera=20Dur=C3=A1n?= <pateketrueke@gmail.com> Date: Mon, 1 Oct 2018 20:17:31 -0500 Subject: [PATCH] Minor tweaks to support $id and const; cleanup --- src/core/run.js | 5 ++++- src/core/traverse.js | 10 +++++++++- src/core/utils.js | 2 +- src/index.js | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/core/run.js b/src/core/run.js index 46dde8ad..5a8b8dec 100644 --- a/src/core/run.js +++ b/src/core/run.js @@ -97,8 +97,11 @@ function run(refs, schema, container) { } // cleanup - if (sub.id && typeof sub.id === 'string') { + const _id = sub.$id || sub.id; + + if (typeof _id === 'string') { delete sub.id; + delete sub.$id; delete sub.$schema; } diff --git a/src/core/traverse.js b/src/core/traverse.js index 1ed942a5..43c100d7 100644 --- a/src/core/traverse.js +++ b/src/core/traverse.js @@ -17,7 +17,11 @@ function traverse(schema, path, resolve, rootSchema) { if (path[path.length - 1] !== 'properties') { // example values have highest precedence if (optionAPI('useExamplesValue') && Array.isArray(schema.examples)) { - return utils.typecast(null, schema, () => random.pick(schema.examples)); + // include `default` value as example too + const fixedExamples = schema.examples + .concat('default' in schema ? [schema.default] : []); + + return utils.typecast(null, schema, () => random.pick(fixedExamples)); } if (optionAPI('useDefaultValue') && 'default' in schema) { @@ -33,6 +37,10 @@ function traverse(schema, path, resolve, rootSchema) { schema = utils.notValue(schema.not, utils.omitProps(schema, ['not'])); } + if ('const' in schema) { + return schema.const; + } + if (Array.isArray(schema.enum)) { return utils.typecast(null, schema, () => random.pick(schema.enum)); } diff --git a/src/core/utils.js b/src/core/utils.js index a6530ac7..e140112f 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -265,7 +265,7 @@ function validate(value, schemas) { } function isKey(prop) { - return prop === 'enum' || prop === 'default' || prop === 'required' || prop === 'definitions'; + return ['enum', 'const', 'default', 'examples', 'required', 'definitions'].indexOf(prop) !== -1; } function omitProps(obj, props) { diff --git a/src/index.js b/src/index.js index 7fd07fe6..ed70aba8 100644 --- a/src/index.js +++ b/src/index.js @@ -59,7 +59,7 @@ function getRefs(refs) { if (Array.isArray(refs)) { refs.forEach(schema => { - $refs[schema.id] = schema; + $refs[schema.$id || schema.id] = schema; }); } else { $refs = refs || {}; -- GitLab