Skip to content

Commit 8430bcf

Browse files
authored
Merge pull request #1 from APIOpsCycles/codex/add-json-schemas-and-package.json-for-module
Add JSON schemas and validation for method data
2 parents 82edc85 + 3e50745 commit 8430bcf

15 files changed

Lines changed: 725 additions & 44 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
3+
# translation cache and temporary translation folder
4+
.json-autotranslate-cache
5+
src/data/locales/

README.md

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,48 @@ Read more about the method at (https://www.apiopscycles.com/)
44

55
This repository contains the source for the APIOps Cycles method, including resources such as canvases. It can be used for multiple purposes, such as the method website (https://www.apiopscycles.com/), which is generated from these files, and the Canvas Creator tool that allows creating, importing and exporting the canvases, and provides a UI. (https://canvascreator.apiopscycles.com/).
66

7-
## Requirements
7+
## License
88

9-
You can use the JSON files as is and download a .zip file or clone the repository. You can also install them using `npm install apiops-cycles-method-data`.
9+
All content in this repository is provided under Apache 2 license for programmable use, the content and the design of the canvases is under the CC BY-SA 4.0 license unless noted otherwise.
10+
APIOps and APIOps Cycles are trademarks owned by Osaango Oy (https://www.osaango.com). Permission is given to use them in connection with the method, for community purposes. Contact Osaango for partnerships, or sponsoring to support the method development. See method website for current partners (https://www.apiopscycles.com/)
1011

11-
- **Node.js 22** or newer
12-
- npm
1312

14-
Install dependencies once with:
13+
## Repository structure
1514

16-
```bash
17-
npm install
15+
```
16+
├── src/
17+
│ ├── assets/ # APIOps Cycles logos and other core assets which you might need in your tooling or product
18+
│ ├── data/method/ # The Method JSON files (structure, relationship, and guideline content)
19+
│ ├── data/method/canvas/ # The Canvases included in the method as JSON files (also used by tools like Canvas Creator)
20+
│ ├── snippets/ # Raw markdown files used for long content for resource docs (only essential extensions for the json files)
21+
├── scripts/ # Utility scripts
22+
└── package.json
1823
```
1924

20-
### Canvas editor
25+
## Requirements
26+
27+
You can use the JSON files as is and download a .zip file or clone the repository. You can also install them using `npm install apiops-cycles-method-data`.
2128

22-
Interactive canvases on the site are powered by the [Canvas Creator](https://canvascreator.apiopscycles.com) tool. The component is installed from npm so make sure to run `npm install` before building or running the dev server.
29+
The module exposes top-level exports so you can import the data files directly, for example:
2330

24-
## Local development
31+
```js
32+
import stations from "apiops-cycles-method-data/method/stations.json";
33+
import canvasData from "apiops-cycles-method-data/canvasData";
34+
```
2535

26-
Run a local dev server:
36+
Validate the files locally with:
2737

2838
```bash
29-
npm run dev
30-
```
39+
npm test
40+
```
3141

32-
This command copies the resource files to `public/` so that links to canvas JSON
33-
files work during development.
42+
- **Node.js 22** or newer
43+
- npm
3444

35-
Build the production site:
45+
Install dependencies once with:
3646

3747
```bash
38-
npm run build
48+
npm install
3949
```
4050

4151
## Contributing
@@ -46,17 +56,13 @@ If you spot a problem in the documentation or have an idea for new content, plea
4656

4757
### Editing or adding content
4858

49-
Most pages are generated from the JSON files in `src/data/method/`. These base files (`lines.json`, `stations.json`, `resources.json`, `criteria.json` and `station-criteria.json`) are not localized and live at the root of the folder. Textual values in them reference label keys. English labels are in `src/data/method/en-US` and translations are provided in `labels.lines.json`, `labels.stations.json`, `labels.resources.json` and `labels.criteria.json` under each locale folder. Some longer or more complex resource pages like the API Audit Checklist also use markdown snippets `src/snippets/` linked to the `resources.json`. Do not use any frontmatter in the snippet files. Any supported markdown markup is ok. See references from [Starlight markdown reference](https://starlight.astro.build/guides/authoring-content/) and [Extended markdown reference](https://www.markdownguide.org/extended-syntax/).
59+
The main method files (instructions, guidelines, method structure) is located in the the JSON files at `src/data/method/`. These base files (`lines.json`, `stations.json`, `resources.json`, `criteria.json` and `station-criteria.json`) are not localized and live at the root of the folder. Textual values in them reference label keys. English labels are in `src/data/method/en-US` and translations are provided in `labels.lines.json`, `labels.stations.json`, `labels.resources.json` and `labels.criteria.json` under each locale folder. Some longer or more complex resource pages like the API Audit Checklist also use markdown snippets `src/snippets/` linked to the `resources.json`. Do not use any frontmatter in the snippet files. Any supported markdown markup is ok. See references from [Starlight markdown reference](https://starlight.astro.build/guides/authoring-content/) and [Extended markdown reference](https://www.markdownguide.org/extended-syntax/).
5060

51-
Each station page lists the station's entry criteria followed by the next core station's criteria as exit criteria.
52-
53-
After editing these files, run `npm run generate:method` to update the Markdown files in `src/content/docs/`. Note that the generated markdown files are not inlcuded in version control to avoid confusion on where editing should happen.
54-
55-
For fixes in the Markdown files included in version control under `src/content/docs/`, edit them directly.
61+
Each station links to specific entry criteria followed by the next core station's criteria as exit criteria.`criteria.json`, `station-criteria.json` and `labels.criteria.json`
5662

5763
#### Editing existing content of Method pages (metrolines, core- and substations, resources).
5864
1. Go to `src/data/method/en` and edit the content in English (English is considered the master langauge, and for the translations to work for other languages, it must always be the first to be edited).
59-
2. Validate that your changes work TODO
65+
2. Validate that your changes work by running the schema validations (`npm test`)
6066
3. Follow the translation guide if you are able to translate the content to other languages manually or automatically.
6167
4. Commit your code and make a pull request.
6268
5. If you were not able to create the translations of your changes to all languages, create an issue for in the repository for the translations.
@@ -71,6 +77,8 @@ Content under `src/snippets/` and `src/assets/resource/` can be localized by pla
7177

7278
Create/Edit the translaton files and put them in to the `src/data/method/` with correct language subfolder manually, or...
7379

80+
Install json-autotranslate with `npm install json-autotranslate`. It is not included as a dev-dependency at all, because most people would never need it and it's installing over 200+ dependencies.
81+
7482
1. Create a copy of the `src/data/method/` folder as `.json-autotranslate-cache` (in the root of the project). The cache is not under version control.
7583
2. Create a new folder under the `src/data/locale` (this is a temporary folder, not versioned ) using the 2-letter ISO-code for the new language you are about to translate, e.g. `src/data/locale/fi` if creating translations for Finnish language.
7684
3. Make sure the target language folder is *empty*, e.g. `src/data/locale/fi`. The folder really needs to be empty, even if there are already translations done for this language.
@@ -93,26 +101,8 @@ Create/Edit the translaton files and put them in to the `src/data/method/` with
93101
9. If the translated files look ok, go to the translation cache folder `.json-autotranslate-cache` and look for the folder in the target language, i.e. `.json-autotranslate-cache/fi`.
94102
- (Explanation: For some strange reason the plugin writes the contents in different (nested) format in the source folder (i.e. `locales`) than what it can actually read during the next translation round. The flat format it uses in the cache is perfect for it and also the most efficient for our page generation scripts).
95103
10. These files are in the correct flat format for our page generation scripts, so copy these files and paste them to `src/data/method/` folder for the target language, in this case `src/data/method/fi`
96-
11. Validate that your changes work TODO
104+
11. Validate that your changes work with `npm test`
97105
12. Commit your code and make a pull request.
98106
13. After your pull request has been merged, remember to pull changes so that your local branch is deleted, too. (Make sure you don't leave locally any old files under `.json-autotranslate-cache`.)
99107
100-
If you are planning to use automated translation services: Note that there are some languages that are supported by some of the translation services with local varieties. Check the documentation (https://github.com/leolabs/json-autotranslate) of supported automated translation services and how to see which languages they support.
101-
102-
103-
## Repository structure
104-
105-
```
106-
├── src/
107-
│ ├── assets/ # APIOps Cycles logos and other core assets which you might need in your tooling or product
108-
│ ├── data/method/ # The Method JSON files (structure, relationship, and guideline content)
109-
│ ├── data/method/canvas/ # The Canvases included in the method as JSON files (also used by tools like Canvas Creator)
110-
│ ├── snippets/ # Raw markdown files used for long content for resource docs (only essential extensions for the json files)
111-
├── scripts/ # Utility scripts
112-
└── package.json
113-
```
114-
115-
## License
116-
117-
All content in this repository is provided under Apache 2 license for programmable use, the content and the design of the canvases is under the CC BY-SA 4.0 license unless noted otherwise.
118-
APIOps and APIOps Cycles are trademarks owned by Osaango Oy (https://www.osaango.com). Permission is given to use them in connection with the method, for community purposes. Contact Osaango for partnerships, or sponsoring to support the method development. See method website for current partners (https://www.apiopscycles.com/)
108+
If you are planning to use automated translation services: Note that there are some languages that are supported by some of the translation services with local varieties. Check the documentation (https://github.com/leolabs/json-autotranslate) of supported automated translation services and how to see which languages they support.

package-lock.json

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "apiops-cycles-method-data",
3+
"version": "3.0.0",
4+
"description": "APIOps Cycles Method data and canvases",
5+
"license": "Apache-2.0",
6+
"type": "module",
7+
"files": ["src/data","src/snippets", "src/assets/", "schemas/"],
8+
"exports": {
9+
"./method/": "./src/data/method/",
10+
"./canvasData": "./src/data/canvas/canvasData.json",
11+
"./localizedData": "./src/data/canvas/localizedData.json",
12+
"./canvas/": "./src/data/canvas/import-export-templates/"
13+
},
14+
"scripts": {
15+
"test": "node scripts/validate.mjs"
16+
},
17+
"devDependencies": {
18+
"ajv": "^8.17.1"
19+
},
20+
"engines": {
21+
"node": ">=22"
22+
}
23+
}

scripts/validate.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Ajv from 'ajv';
2+
import fs from 'fs/promises';
3+
import path from 'path';
4+
5+
const ajv = new Ajv({ allErrors: true });
6+
7+
async function loadJSON(file) {
8+
return JSON.parse(await fs.readFile(file, 'utf8'));
9+
}
10+
11+
async function validate(schemaPath, files) {
12+
const schema = await loadJSON(schemaPath);
13+
const validate = ajv.compile(schema);
14+
for (const file of files) {
15+
const data = await loadJSON(file);
16+
const valid = validate(data);
17+
if (!valid) {
18+
console.error(`Validation failed for ${file}`);
19+
console.error(validate.errors);
20+
process.exitCode = 1;
21+
} else {
22+
console.log(`Validated ${file}`);
23+
}
24+
}
25+
}
26+
27+
async function gatherLabelFiles() {
28+
const base = 'src/data/method';
29+
const dirs = await fs.readdir(base, { withFileTypes: true });
30+
const files = [];
31+
for (const dir of dirs) {
32+
if (dir.isDirectory()) {
33+
const subfiles = await fs.readdir(path.join(base, dir.name));
34+
for (const f of subfiles) {
35+
if (f.startsWith('labels') && f.endsWith('.json')) {
36+
files.push(path.join(base, dir.name, f));
37+
}
38+
}
39+
}
40+
}
41+
return files;
42+
}
43+
44+
async function gatherTemplateFiles() {
45+
const dir = 'src/data/canvas/import-export-templates';
46+
const files = await fs.readdir(dir);
47+
return files.filter(f => f.endsWith('.json')).map(f => path.join(dir, f));
48+
}
49+
50+
(async () => {
51+
await validate('src/schemas/criteria.schema.json', ['src/data/method/criteria.json']);
52+
await validate('src/schemas/lines.schema.json', ['src/data/method/lines.json']);
53+
await validate('src/schemas/resources.schema.json', ['src/data/method/resources.json']);
54+
await validate('src/schemas/station-criteria.schema.json', ['src/data/method/station-criteria.json']);
55+
await validate('src/schemas/stations.schema.json', ['src/data/method/stations.json']);
56+
await validate('src/schemas/canvasData.schema.json', ['src/data/canvas/canvasData.json']);
57+
await validate('src/schemas/canvas-localized.schema.json', ['src/data/canvas/localizedData.json']);
58+
await validate('src/schemas/import-export-template.schema.json', await gatherTemplateFiles());
59+
await validate('src/schemas/labels.schema.json', await gatherLabelFiles());
60+
})();

0 commit comments

Comments
 (0)