feat: add resource schemas and validation scripts
- Introduced new resource schemas for maps, scenarios, unit sets, and turn commands using Zod. - Implemented a script to generate JSON schemas from Zod schemas. - Added a validation script to ensure resource JSON files conform to their respective schemas. - Updated the logic package to export new resource schemas. - Added new dependencies for schema generation and validation. - Created a tools-scripts package for resource management tasks.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import type { ZodTypeAny } from 'zod';
|
||||
import { zodToJsonSchema } from 'zod-to-json-schema';
|
||||
import {
|
||||
MapResourceSchema,
|
||||
ScenarioResourceSchema,
|
||||
TurnCommandProfileInputSchema,
|
||||
UnitSetDefinitionInputSchema,
|
||||
} from '@sammo-ts/logic';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const REPO_ROOT = path.resolve(__dirname, '..', '..', '..');
|
||||
const RESOURCE_ROOT = path.join(REPO_ROOT, 'resources');
|
||||
|
||||
const writeSchema = async (relativePath: string, schema: ZodTypeAny, name: string): Promise<void> => {
|
||||
const outputPath = path.join(RESOURCE_ROOT, relativePath);
|
||||
const jsonSchema = zodToJsonSchema(schema, { name, target: 'jsonSchema7' });
|
||||
const serialized = `${JSON.stringify(jsonSchema, null, 4)}\n`;
|
||||
await fs.writeFile(outputPath, serialized, 'utf8');
|
||||
};
|
||||
|
||||
const main = async (): Promise<void> => {
|
||||
await writeSchema(path.join('map', 'schema.json'), MapResourceSchema, 'MapResource');
|
||||
await writeSchema(path.join('scenario', 'schema.json'), ScenarioResourceSchema, 'ScenarioResource');
|
||||
await writeSchema(path.join('unitset', 'schema.json'), UnitSetDefinitionInputSchema, 'UnitSetDefinition');
|
||||
await writeSchema(path.join('turn-commands', 'schema.json'), TurnCommandProfileInputSchema, 'TurnCommandProfile');
|
||||
};
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user