Skip to content

Commit 1a2a6ff

Browse files
committed
feat: dereference local schemas
This allows us to share the CloudEvents configuration between different events.
1 parent d938cbf commit 1a2a6ff

File tree

12 files changed

+1473
-126
lines changed

12 files changed

+1473
-126
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { expect, test, describe } from "@jest/globals";
2+
3+
import { derefSchemaPath } from "../../src/plugins/deref-json-schemas/deref-json-schema";
4+
5+
describe("deref-json-schemas", () => {
6+
test("keeps the description when merging", async () => {
7+
const result = await derefSchemaPath(
8+
"schemas/com.mbta.ocs.raw_message.json"
9+
);
10+
expect(result).toMatchObject({
11+
properties: {
12+
id: {
13+
description: expect.stringMatching(/.+/),
14+
},
15+
},
16+
});
17+
expect(result["$defs"]).toBeUndefined();
18+
expect(result).not.toMatchObject({
19+
properties: {
20+
id: {
21+
$ref: expect.anything(),
22+
},
23+
},
24+
});
25+
});
26+
27+
test("does not duplicate array items", async () => {
28+
const result = await derefSchemaPath(
29+
"schemas/com.mbta.ocs.raw_message.json"
30+
);
31+
expect(result["required"]).toEqual([
32+
"id",
33+
"source",
34+
"specversion",
35+
"type",
36+
"data",
37+
"time",
38+
"partitionkey",
39+
]);
40+
});
41+
});
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
---
22
---
3-
4-
import Schema from "@site/schemas/com.mbta.ocs.raw_message.json";
5-
import EventSchema from "@site/src/components/EventSchema.tsx";
3+
import EventSchemaPath from "@site/src/components/EventSchemaPath.tsx";
64

75
Raw message from the OCS System. Trike turns the raw message into an event.
86

97
Originally described in [RFC4](https://github.com/mbta/technology-docs/blob/main/rfcs/accepted/0004-socket-proxy-ocs-cloudevents.md).
108

119
If you are internal to the MBTA, documentation for the contents of the `raw` field live in [SharePoint](https://mbta.sharepoint.com/:w:/r/sites/CTD/Shared%20Documents/General/Transit%20Tech%20CTD-wide/Transit%20Data/Product%20Documentation/RTR%20+%20Prediction%20Analyzer/Notes/ocs_messages.docx?d=wd7d5fea4e2104a4cac86b296e3083a47&csf=1&web=1&e=0vRTqp).
1210

13-
<EventSchema schema={ Schema } />
11+
<EventSchemaPath event="com.mbta.ocs.raw_message" />

docusaurus.config.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
11
// @ts-check
22
// Note: type annotations allow type checking and IDEs autocompletion
33

4-
const lightCodeTheme = require('prism-react-renderer/themes/github');
5-
const darkCodeTheme = require('prism-react-renderer/themes/dracula');
4+
const lightCodeTheme = require("prism-react-renderer/themes/github");
5+
const darkCodeTheme = require("prism-react-renderer/themes/dracula");
66

77
/** @type {import('@docusaurus/types').Config} */
88
const config = {
9-
title: 'MBTA Event Schemas',
10-
tagline: '',
11-
favicon: 'img/favicon.ico',
9+
title: "MBTA Event Schemas",
10+
tagline: "",
11+
favicon: "img/favicon.ico",
1212

1313
// Set the production url of your site here
14-
url: (process.env.GITHUB_ORIGIN || "http://localhost:3000"),
14+
url: process.env.GITHUB_ORIGIN || "http://localhost:3000",
1515
// Set the /<baseUrl>/ pathname under which your site is served
1616
// For GitHub pages deployment, it is often '/<projectName>/'
17-
baseUrl: (process.env.GITHUB_BASE || "/"),
17+
baseUrl: process.env.GITHUB_BASE || "/",
1818

1919
// GitHub pages deployment config.
2020
// If you aren't using GitHub pages, you don't need these.
2121
organizationName: "mbta", // Usually your GitHub org/user name.
2222
projectName: "schemas", // Usually your repo name.
2323

24-
onBrokenLinks: 'throw',
25-
onBrokenMarkdownLinks: 'warn',
24+
onBrokenLinks: "throw",
25+
onBrokenMarkdownLinks: "warn",
2626

2727
// Even if you don't use internalization, you can use this field to set useful
2828
// metadata like html lang. For example, if your site is Chinese, you may want
2929
// to replace "en" with "zh-Hans".
3030
i18n: {
31-
defaultLocale: 'en',
32-
locales: ['en'],
31+
defaultLocale: "en",
32+
locales: ["en"],
3333
},
34-
staticDirectories: ['static', 'events'],
34+
staticDirectories: ["static"],
3535
presets: [
3636
[
37-
'classic',
37+
"classic",
3838
/** @type {import('@docusaurus/preset-classic').Options} */
3939
({
4040
docs: {
4141
routeBasePath: "/",
42-
sidebarPath: require.resolve('./sidebars.js'),
42+
sidebarPath: require.resolve("./sidebars.js"),
4343
// Please change this to your repo.
4444
// Remove this to remove the "edit this page" links.
4545
editUrl:
4646
"https://github.com/mbta/schemas/tree/main/",
4747
},
4848
blog: false,
4949
theme: {
50-
customCss: require.resolve('./src/css/custom.css'),
50+
customCss: require.resolve("./src/css/custom.css"),
5151
},
5252
}),
5353
],
5454
],
5555

5656
themes: [["docusaurus-json-schema-plugin", {}]],
57+
plugins: ["./src/plugins/deref-json-schemas"],
5758
themeConfig:
5859
/** @type {import('@docusaurus/preset-classic').ThemeConfig} */
5960
({
6061
// Replace with your project's social card
61-
image: 'img/docusaurus-social-card.jpg',
62+
image: "img/docusaurus-social-card.jpg",
6263
navbar: {
63-
title: 'Schemas',
64+
title: "Schemas",
6465
logo: {
65-
alt: 'MBTA',
66-
src: 'img/logo.png',
66+
alt: "MBTA",
67+
src: "img/logo.png",
6768
},
6869
items: [
6970
{
70-
type: 'docSidebar',
71-
sidebarId: 'eventsSidebar',
72-
position: 'left',
73-
label: 'Events',
71+
type: "docSidebar",
72+
sidebarId: "eventsSidebar",
73+
position: "left",
74+
label: "Events",
7475
},
7576
{
7677
href: "https://github.com/mbta/schemas",
@@ -80,19 +81,19 @@ const config = {
8081
],
8182
},
8283
footer: {
83-
style: 'dark',
84+
style: "dark",
8485
links: [
8586
{
86-
title: 'Docs',
87+
title: "Docs",
8788
items: [
8889
{
89-
label: 'Events',
90-
to: '/category/events',
90+
label: "Events",
91+
to: "/category/events",
9192
},
9293
],
9394
},
9495
{
95-
title: 'More',
96+
title: "More",
9697
items: [
9798
{
9899
label: "GitHub",

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest",
4+
testEnvironment: "node",
5+
reporters: ["default"],
6+
};

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,28 @@
1313
"write-translations": "docusaurus write-translations",
1414
"write-heading-ids": "docusaurus write-heading-ids",
1515
"format": "prettier --write **/*.{js,jsx,ts,tsx,json}",
16-
"typecheck": "tsc"
16+
"typecheck": "tsc",
17+
"test": "jest"
1718
},
1819
"dependencies": {
1920
"@docusaurus/core": "2.4.0",
2021
"@docusaurus/preset-classic": "2.4.0",
2122
"@mdx-js/react": "^1.6.22",
23+
"@stoplight/json-ref-readers": "^1.2.2",
2224
"clsx": "^1.2.1",
2325
"deepmerge": "^4.3.1",
2426
"docusaurus-json-schema-plugin": "^1.6.0",
2527
"prism-react-renderer": "^1.3.5",
2628
"react": "^17.0.2",
27-
"react-dom": "^17.0.2"
29+
"react-dom": "^17.0.2",
30+
"webpack-merge": "^5.8.0"
2831
},
2932
"devDependencies": {
3033
"@docusaurus/module-type-aliases": "2.4.0",
3134
"@tsconfig/docusaurus": "^1.0.5",
35+
"jest": "^29.5.0",
3236
"prettier": "^2.8.7",
37+
"ts-jest": "^29.1.0",
3338
"typescript": "^4.7.4"
3439
},
3540
"browserslist": {

schemas/cloudevents.json

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"$id": "https://github.com/mbta/schemas/blob/main/schemas/cloudevents.json",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
"$comment": "Modified from https://github.com/cloudevents/spec/blob/b43a64bf4fa92baee02dad512f23ea5cb77a321c/cloudevents/formats/cloudevents.json",
5+
"description": "CloudEvents Specification JSON Schema",
6+
"type": "object",
7+
"properties": {
8+
"id": {
9+
"description": "Identifies the event.",
10+
"type": "string",
11+
"minLength": 1,
12+
"examples": ["A234-1234-1234"]
13+
},
14+
"source": {
15+
"description": "Identifies the context in which an event happened.",
16+
"type": "string",
17+
"format": "uri-reference",
18+
"minLength": 1,
19+
"examples": [
20+
"https://github.com/cloudevents",
21+
22+
"urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66",
23+
"cloudevents/spec/pull/123",
24+
"/sensors/tn-1234567/alerts",
25+
"1-555-123-4567"
26+
]
27+
},
28+
"specversion": {
29+
"description": "The version of the CloudEvents specification which the event uses.",
30+
"type": "string",
31+
"minLength": 1,
32+
"examples": ["1.0"]
33+
},
34+
"type": {
35+
"description": "Describes the type of event related to the originating occurrence.",
36+
"type": "string",
37+
"minLength": 1,
38+
"examples": [
39+
"com.github.pull_request.opened",
40+
"com.example.object.deleted.v2"
41+
]
42+
},
43+
"datacontenttype": {
44+
"description": "Content type of the data value. Must adhere to RFC 2046 format.",
45+
"type": ["string", "null"],
46+
"minLength": 1,
47+
"examples": [
48+
"text/xml",
49+
"application/json",
50+
"image/png",
51+
"multipart/form-data"
52+
]
53+
},
54+
"dataschema": {
55+
"description": "Identifies the schema that data adheres to.",
56+
"type": ["string", "null"],
57+
"format": "uri",
58+
"minLength": 1
59+
},
60+
"subject": {
61+
"description": "Describes the subject of the event in the context of the event producer (identified by source).",
62+
"type": ["string", "null"],
63+
"minLength": 1,
64+
"examples": ["mynewfile.jpg"]
65+
},
66+
"time": {
67+
"description": "Timestamp of when the occurrence happened. Must adhere to RFC 3339.",
68+
"type": ["string", "null"],
69+
"format": "date-time",
70+
"minLength": 1,
71+
"examples": ["2018-04-05T17:31:00Z"]
72+
},
73+
"data": {
74+
"description": "The event payload.",
75+
"anyOf": [
76+
{ "type": "object" },
77+
{ "type": "string" },
78+
{ "type": "number" },
79+
{ "type": "array" },
80+
{ "type": "boolean" },
81+
{ "type": "null" }
82+
],
83+
"examples": ["<much wow=\"xml\"/>"]
84+
},
85+
"data_base64": {
86+
"description": "Base64 encoded event payload. Must adhere to RFC4648.",
87+
"type": ["string", "null"],
88+
"contentEncoding": "base64",
89+
"examples": ["Zm9vYg=="]
90+
}
91+
},
92+
"required": ["id", "source", "specversion", "type"]
93+
}

0 commit comments

Comments
 (0)