Skip to content

Commit

Permalink
feat(schema): add support for readOnly and writeOnly schemas and prop…
Browse files Browse the repository at this point in the history
…erties
  • Loading branch information
trieloff committed Dec 16, 2019
1 parent 934b856 commit 7452882
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
12 changes: 12 additions & 0 deletions lib/formatInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ function getstatus(schema) {
return schema[keyword`meta:status`] || undefined;
}

function getrestrictions(schema) {
if (schema[keyword`readOnly`] === true && schema[keyword`writeOnly`] === true) {
return 'secret';
} else if (schema[keyword`readOnly`] === true) {
return 'readOnly';
} else if (schema[keyword`writeOnly`] === true) {
return 'writeOnly';
}
return undefined;
}

function formatmeta(schema) {
return {
abstract: isabstract(schema),
Expand All @@ -107,6 +118,7 @@ function formatmeta(schema) {
custom: iscustom(schema),
additional: schema[keyword`additionalProperties`] !== false,
definedin: getdefined(schema),
restrictions: getrestrictions(schema),
...parsedescription(plaindescription(schema)),
};
}
Expand Down
34 changes: 34 additions & 0 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ function build({
falselabel: i18n`Forbidden`,
undefinedlabel: i18n`Unknown additional properties`,
},
{
name: 'restrictions',
title: i18n`Access Restrictions`,
readOnlylabel: i18n`Read only`,
writeOnlylabel: i18n`Write only`,
secretlabel: i18n`cannot be read or written`,
undefinedlabel: i18n`none`,
},
{
name: 'definedin',
title: i18n`Defined In`,
Expand Down Expand Up @@ -653,6 +661,29 @@ function build({
return [];
}

function makerestrictions(schema, level = 1) {
if (schema[keyword`readOnly`] && schema[keyword`writeOnly`]) {
return [
heading(level + 1, text(i18n`${simpletitle(schema)} Access Restrictions`)),
paragraph(text(i18n`The value of this property is managed exclusively by the owning authority and never exposed to the outside. It can neither be read nor written.`)),
];
}
if (schema[keyword`readOnly`]) {
return [
heading(level + 1, text(i18n`${simpletitle(schema)} Access Restrictions`)),
paragraph(text(i18n`The value of this property is managed exclusively by the owning authority, and attempts by an application to modify the value of this property are expected to be ignored or rejected by that owning authority`)),
];
}
if (schema[keyword`writeOnly`]) {
return [
heading(level + 1, text(i18n`${simpletitle(schema)} Access Restrictions`)),
paragraph(text(i18n`The value of this property is never present when the instance is retrieved from the owning authority. It can be present when sent to the owning authority to update or create the document (or the resource it represents), but it will not be included in any updated or newly created version of the instance.`)),
];
}
return [];
}


function makeproplist(properties = {},
patternProperties = {},
additionalProperties,
Expand All @@ -671,6 +702,7 @@ function build({
...makeconstraintssection(definition, level + 1),
...makedefault(definition, level + 1),
...makeexamples(definition, level + 1),
...makerestrictions(definition, level + 1),
];
}))),
...flist(flat(Object.entries(patternProperties || {}).map(([name, definition]) => {
Expand All @@ -685,6 +717,7 @@ function build({
...makeconstraintssection(definition, level + 1),
...makedefault(definition, level + 1),
...makeexamples(definition, level + 1),
...makerestrictions(definition, level + 1),
];
}))),
...((definition) => {
Expand All @@ -699,6 +732,7 @@ function build({
...makeconstraintssection(definition, level + 1),
...makedefault(definition, level + 1),
...makeexamples(definition, level + 1),
...makerestrictions(definition, level + 1),
];
} else if (additionalProperties === true) {
return [
Expand Down
8 changes: 4 additions & 4 deletions schemasupport.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# JSON Schema Spec Coverage Report

This report lists the keywords of the JSON Schema spec that are covered in the tests. The overall coverage is 76%
This report lists the keywords of the JSON Schema spec that are covered in the tests. The overall coverage is 78%

## The JSON Schema Core Vocabulary

Expand Down Expand Up @@ -135,14 +135,14 @@ Coverage for [A Vocabulary for the Contents of String-Encoded Data](https://json

## A Vocabulary for Basic Meta-Data Annotations

Coverage for [A Vocabulary for Basic Meta-Data Annotations](https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9) is 71%.
Coverage for [A Vocabulary for Basic Meta-Data Annotations](https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9) is 100%.

| Keyword | Supported |
| :------------ | --------- |
| `default` | Yes |
| `deprecated` | Yes |
| `description` | Yes |
| `examples` | Yes |
| `readOnly` | No |
| `readOnly` | Yes |
| `title` | Yes |
| `writeOnly` | No |
| `writeOnly` | Yes |
12 changes: 9 additions & 3 deletions test/fixtures/readme-1/abstract.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"$schema": "http://json-schema.org/draft-06/schema#",
"$id": "https://example.com/schemas/abstract",
"title": "Abstract",
"readOnly": true,
"writeOnly": true,
"description": "This is an abstract schema. It has `definitions`, but does not declare any properties",
"type": "object",
"definitions": {
Expand All @@ -18,14 +20,16 @@
"type": "string",
"description": "A unique identifier given to every addressable thing.",
"version": "1.0.0",
"testProperty": "test"
"testProperty": "test",
"readOnly": true
},
"nonfoo": {
"type": "boolean",
"const": false,
"description": "This is not foo.",
"version": "1.0.0",
"testProperty": "test"
"testProperty": "test",
"writeOnly": true
}
}
},
Expand All @@ -36,7 +40,9 @@
"type": "string",
"description": "A unique identifier given to every addressable thing.",
"version": "1.0.0",
"testProperty": "test"
"testProperty": "test",
"readOnly": true,
"writeOnly": true
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/readme-1/complex.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"$id": "https://example.com/schemas/complex",
"title": "Complex References",
"type": "object",
"readOnly": true,
"description": "This is an example schema that uses types defined in other schemas.",
"properties": {
"refabstract": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/readme-1/simple.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"$id": "https://example.com/schemas/simple",
"title": "Simple",
"deprecated": true,
"writeOnly": true,
"description": "This is a *very* simple example of a JSON schema. There is only one property.",
"type": "object",
"properties": {
Expand Down
6 changes: 6 additions & 0 deletions test/markdownBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ describe('Testing Markdown Builder: readme-1', () => {
it('Abstract Schema looks OK', () => {
assertMarkdown(results.abstract)
.equals('heading > text', { type: 'text', value: 'Abstract Schema' })
.contains('cannot be read or written')
.contains('nonfoo Access Restrictions')
.contains('bar Access Restrictions')
.contains('### foo Access Restrictions')
.fuzzy`
## Definitions group second
Expand All @@ -274,13 +278,15 @@ Reference this group by using

it('Complex Schema looks OK', () => {
assertMarkdown(results.complex)
.contains('Read only')
.contains('# Complex References Schema');
});

it('Simple Schema looks OK', () => {
assertMarkdown(results.simple)
.contains('Deprecated')
.contains('"Simply Untitled"')
.contains('Write only')
.contains('# Simple Schema');
});
});

0 comments on commit 7452882

Please sign in to comment.