Skip to content

Commit f870566

Browse files
committed
Go back to simple version
1 parent 69adf1b commit f870566

File tree

3 files changed

+151
-9
lines changed

3 files changed

+151
-9
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ jobs:
2121
- name: Check TypeScript definitions
2222
run: npm run check:schema:ts
2323

24-
- name: Check draft-07 schema.json files are up to date
24+
- name: Check schema.json files are up to date
2525
run: npm run check:schema:json
2626

27-
- name: Check 2020-12 schema.json files are up to date
28-
run: npm run check:schema:json:2020-12
29-
3027
- name: Check schema.mdx files are up to date
3128
run: npm run check:schema:md

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@
2222
},
2323
"scripts": {
2424
"check": "npm run check:schema && npm run check:docs",
25-
"check:schema": "npm run check:schema:ts && npm run check:schema:json && npm run check:schema:json:2020-12 && npm run check:schema:md",
25+
"check:schema": "npm run check:schema:ts && npm run check:schema:json && npm run check:schema:md",
2626
"check:schema:ts": "tsc --noEmit && eslint schema/ && prettier --check \"schema/**/*.ts\"",
27-
"check:schema:json": "for f in schema/2024-11-05/schema.ts schema/2025-03-26/schema.ts schema/2025-06-18/schema.ts; do typescript-json-schema --defaultNumberType integer --required --skipLibCheck \"$f\" \"*\" | cmp \"${f%.ts}.json\" - || exit 1; done",
28-
"check:schema:json:2020-12": "npm run generate:schema:json:2020-12 && git diff --exit-code schema/draft/schema.json || (echo 'Error: schema/draft/schema.json is out of date. Run: npm run generate:schema:json:2020-12' && exit 1)",
27+
"check:schema:json": "tsx scripts/generate-schemas.ts --check",
2928
"check:schema:md": "for f in schema/*/schema.mdx; do typedoc --entryPoints \"${f%.mdx}.ts\" --schemaPageTemplate \"$f\" | cmp docs/specification/$(basename -- $(dirname -- \"$f\"))/schema.mdx - || exit 1; done",
3029
"check:docs": "npm run check:docs:format && npm run check:docs:links",
3130
"check:docs:format": "prettier --check \"**/*.{md,mdx}\"",
3231
"check:docs:links": "cd docs && npx mint broken-links",
3332
"generate:schema": "npm run generate:schema:json && npm run generate:schema:md",
34-
"generate:schema:json": "npm run generate:schema:json:2020-12 && for f in schema/2024-11-05/schema.ts schema/2025-03-26/schema.ts schema/2025-06-18/schema.ts; do typescript-json-schema --defaultNumberType integer --required --skipLibCheck \"$f\" \"*\" -o \"${f%.ts}.json\"; done",
35-
"generate:schema:json:2020-12": "typescript-json-schema --defaultNumberType integer --required --skipLibCheck schema/draft/schema.ts \"*\" -o schema/draft/schema.json && sed -i \"s@http://json-schema.org/draft-07/schema#@https://json-schema.org/draft/2020-12/schema@g; s@\\\"definitions\\\":@\\\"\\$defs\\\":@g; s@#/definitions/@#/\\$defs/@g\" schema/draft/schema.json",
33+
"generate:schema:json": "tsx scripts/generate-schemas.ts",
3634
"generate:schema:md": "for f in schema/*/schema.mdx; do typedoc --entryPoints \"${f%.mdx}.ts\" --schemaPageTemplate \"$f\" > docs/specification/$(basename -- $(dirname -- \"$f\"))/schema.mdx; done",
3735
"format": "prettier --write \"**/*.{md,mdx}\" --ignore \"docs/specification/*/schema.mdx\" ",
3836
"serve:docs": "cd docs && npx mint dev",

scripts/generate-schemas.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env tsx
2+
3+
import { execSync } from 'child_process';
4+
import { readFileSync, writeFileSync } from 'fs';
5+
import { join } from 'path';
6+
7+
// Legacy schema versions that should remain as JSON Schema draft-07
8+
const LEGACY_SCHEMAS = ['2024-11-05', '2025-03-26', '2025-06-18'];
9+
10+
// All schema versions to generate
11+
const ALL_SCHEMAS = [...LEGACY_SCHEMAS, 'draft'];
12+
13+
// Check if we're in check mode (validate existing schemas match generated ones)
14+
const CHECK_MODE = process.argv.includes('--check');
15+
16+
/**
17+
* Apply JSON Schema 2020-12 transformations to a schema file
18+
*/
19+
function applyJsonSchema202012Transformations(schemaPath: string): void {
20+
let content = readFileSync(schemaPath, 'utf-8');
21+
22+
// Replace $schema URL
23+
content = content.replace(
24+
/http:\/\/json-schema\.org\/draft-07\/schema#/g,
25+
'https://json-schema.org/draft/2020-12/schema'
26+
);
27+
28+
// Replace "definitions": with "$defs":
29+
content = content.replace(
30+
/"definitions":/g,
31+
'"$defs":'
32+
);
33+
34+
// Replace #/definitions/ with #/$defs/
35+
content = content.replace(
36+
/#\/definitions\//g,
37+
'#/$defs/'
38+
);
39+
40+
writeFileSync(schemaPath, content, 'utf-8');
41+
}
42+
43+
/**
44+
* Generate JSON schema for a specific version
45+
*/
46+
function generateSchema(version: string, check: boolean = false): boolean {
47+
const schemaDir = join('schema', version);
48+
const schemaTs = join(schemaDir, 'schema.ts');
49+
const schemaJson = join(schemaDir, 'schema.json');
50+
51+
if (check) {
52+
console.log(`Checking schema for ${version}...`);
53+
54+
// Read existing schema
55+
const existingSchema = readFileSync(schemaJson, 'utf-8');
56+
57+
// Generate schema to stdout and capture it
58+
try {
59+
const generated = execSync(
60+
`npx typescript-json-schema --defaultNumberType integer --required --skipLibCheck "${schemaTs}" "*"`,
61+
{ encoding: 'utf-8' }
62+
);
63+
64+
let expectedSchema = generated;
65+
66+
// Apply transformations for non-legacy schemas
67+
if (!LEGACY_SCHEMAS.includes(version)) {
68+
expectedSchema = expectedSchema.replace(
69+
/http:\/\/json-schema\.org\/draft-07\/schema#/g,
70+
'https://json-schema.org/draft/2020-12/schema'
71+
);
72+
expectedSchema = expectedSchema.replace(/"definitions":/g, '"$defs":');
73+
expectedSchema = expectedSchema.replace(/#\/definitions\//g, '#/$defs/');
74+
}
75+
76+
// Compare
77+
if (existingSchema.trim() !== expectedSchema.trim()) {
78+
console.error(` ✗ Schema ${version} is out of date!`);
79+
return false;
80+
}
81+
82+
console.log(` ✓ Schema ${version} is up to date`);
83+
return true;
84+
} catch (error) {
85+
console.error(`Failed to check schema for ${version}`);
86+
throw error;
87+
}
88+
} else {
89+
console.log(`Generating schema for ${version}...`);
90+
91+
// Run typescript-json-schema
92+
try {
93+
execSync(
94+
`npx typescript-json-schema --defaultNumberType integer --required --skipLibCheck "${schemaTs}" "*" -o "${schemaJson}"`,
95+
{ stdio: 'inherit' }
96+
);
97+
} catch (error) {
98+
console.error(`Failed to generate schema for ${version}`);
99+
throw error;
100+
}
101+
102+
// Apply transformations for non-legacy schemas
103+
if (!LEGACY_SCHEMAS.includes(version)) {
104+
console.log(`Applying JSON Schema 2020-12 transformations to ${version}...`);
105+
applyJsonSchema202012Transformations(schemaJson);
106+
}
107+
108+
return true;
109+
}
110+
}
111+
112+
/**
113+
* Main function
114+
*/
115+
function main(): void {
116+
if (CHECK_MODE) {
117+
console.log('Checking JSON schemas...\n');
118+
119+
let allValid = true;
120+
for (const version of ALL_SCHEMAS) {
121+
const valid = generateSchema(version, true);
122+
if (!valid) {
123+
allValid = false;
124+
}
125+
}
126+
127+
console.log();
128+
if (!allValid) {
129+
console.error('Error: Some schemas are out of date. Run: npm run generate:schema:json');
130+
process.exit(1);
131+
} else {
132+
console.log('All schemas are up to date!');
133+
}
134+
} else {
135+
console.log('Generating JSON schemas...\n');
136+
137+
for (const version of ALL_SCHEMAS) {
138+
generateSchema(version, false);
139+
}
140+
141+
console.log('\nSchema generation complete!');
142+
console.log(`- (draft-07): ${LEGACY_SCHEMAS.join(', ')}`);
143+
console.log(`- (2020-12): draft`);
144+
}
145+
}
146+
147+
main();

0 commit comments

Comments
 (0)