Skip to content

Commit

Permalink
fix(markdown): handle null as a constant value
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 14, 2019
1 parent 2e057fd commit e652e11
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,7 @@ function build({

function makeconstraintssection(schema, level = 1) {
const constraints = [];
if (schema[keyword`const`]) {
// console.log('const!', schema[s.pointer]);
if (schema[keyword`const`] !== undefined) {
constraints.push(paragraph([strong(text(i18n`constant`)), text(': '), text(i18n`the value of this property must be equal to:`)]));
constraints.push(code('json', JSON.stringify(schema[keyword`const`], undefined, 2)));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/schemaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const handler = ({
if (retval === undefined && prop === keyword`examples` && !receiver[symbols.parent]) {
return loadExamples(receiver[symbols.filename], 1);
}
if (typeof retval === 'object') {
if (typeof retval === 'object' && retval !== null) {
if (retval.$ref) {
const [uri, pointer] = retval.$ref.split('#');
// console.log('resolving ref', uri, pointer, 'from', receiver[symbols.filename]);
Expand Down
2 changes: 1 addition & 1 deletion lib/traverseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function sflat(arr) {
function traverseSchema(schema) {
if (Array.isArray(schema)) {
return sflat(schema.map(traverseSchema));
} else if (typeof schema === 'object') {
} else if (schema && typeof schema === 'object') {
return sflat([schema, ...Object.values(schema).map(traverseSchema)]);
}
return [];
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/null/null.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "object",
"properties": {
"may": {
"type": ["null", "boolean"]
},
"must": {
"type": "null"
},
"constant": {
"const": null
}
}
}
17 changes: 17 additions & 0 deletions test/markdownBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ const { assertMarkdown, loadschemas } = require('./testUtils');

const build = require('../lib/markdownBuilder');

describe('Testing Markdown Builder: null', () => {
let results;

before(async () => {
const schemas = await loadschemas('null');
const builder = build({ header: true });
results = builder(schemas);
});

it('Null Schema looks OK', () => {
assertMarkdown(results.null)
.contains('the value must be null')
.contains('can be null')
.contains('the value of this property must be equal to:');
});
});

describe('Testing Markdown Builder: additionalprops', () => {
let results;

Expand Down

0 comments on commit e652e11

Please sign in to comment.