boardgame-core/src/utils/command/command-validate.ts

27 lines
943 B
TypeScript
Raw Normal View History

2026-04-01 22:37:15 +08:00
import type { Command, CommandSchema } from './types.js';
import { parseCommand } from './command-parse.js';
import { parseCommandSchema } from './schema-parse.js';
2026-04-02 00:14:43 +08:00
import { applyCommandSchema as applyCommandSchemaCore } from './command-apply.js';
export { applyCommandSchemaCore as applyCommandSchema };
2026-04-01 22:37:15 +08:00
export function validateCommand(
command: Command,
schema: CommandSchema
): { valid: true } | { valid: false; errors: string[] } {
2026-04-02 00:14:43 +08:00
const result = applyCommandSchemaCore(command, schema);
if (result.valid) {
return { valid: true };
2026-04-01 22:37:15 +08:00
}
2026-04-02 00:14:43 +08:00
return { valid: false, errors: result.errors };
2026-04-01 22:37:15 +08:00
}
export function parseCommandWithSchema(
input: string,
schemaStr: string
): { command: Command; valid: true } | { command: Command; valid: false; errors: string[] } {
const schema = parseCommandSchema(schemaStr);
const command = parseCommand(input);
2026-04-02 00:14:43 +08:00
return applyCommandSchemaCore(command, schema);
2026-04-01 22:37:15 +08:00
}