Skip to content
Snippets Groups Projects
Commit 6df4888c authored by Alvaro Cabrera Durán's avatar Alvaro Cabrera Durán
Browse files

Minor tweaks to support $id and const; cleanup

parent fd3e5a36
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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));
}
......
......@@ -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) {
......
......@@ -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 || {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment