Skip to content

Commit ca2a4f4

Browse files
authored
feat(cli): new "check" command for validating ZModel (#1652)
1 parent b863060 commit ca2a4f4

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getDefaultSchemaLocation, loadDocument } from '../cli-util';
2+
3+
type Options = {
4+
schema: string;
5+
};
6+
7+
/**
8+
* CLI action for checking schema
9+
*/
10+
export async function check(_projectPath: string, options: Options) {
11+
const schema = options.schema ?? getDefaultSchemaLocation();
12+
await loadDocument(schema);
13+
console.log('The schema is valid.');
14+
}

packages/schema/src/cli/actions/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
export * from './check';
2+
export * from './format';
13
export * from './generate';
24
export * from './info';
35
export * from './init';
46
export * from './repl';
5-
export * from './format';

packages/schema/src/cli/cli-util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const CHECK_VERSION_TIMEOUT = 1000;
2727
* @param services Language services
2828
* @returns Parsed and validated AST
2929
*/
30-
export async function loadDocument(fileName: string): Promise<Model> {
30+
export async function loadDocument(fileName: string, validateOnly = false): Promise<Model> {
3131
const services = createZModelServices(NodeFileSystem).ZModel;
3232
const extensions = services.LanguageMetaData.fileExtensions;
3333
if (!extensions.includes(path.extname(fileName))) {
@@ -93,6 +93,10 @@ export async function loadDocument(fileName: string): Promise<Model> {
9393

9494
const model = document.parseResult.value as Model;
9595

96+
if (validateOnly) {
97+
return model;
98+
}
99+
96100
// merge all declarations into the main document
97101
const imported = mergeImportsDeclarations(langiumDocuments, model);
98102

packages/schema/src/cli/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ export const formatAction = async (options: Parameters<typeof actions.format>[1]
6060
);
6161
};
6262

63+
export const checkAction = async (options: Parameters<typeof actions.check>[1]): Promise<void> => {
64+
await telemetry.trackSpan(
65+
'cli:command:start',
66+
'cli:command:complete',
67+
'cli:command:error',
68+
{ command: 'check' },
69+
() => actions.check(process.cwd(), options)
70+
);
71+
};
72+
6373
export function createProgram() {
6474
const program = new Command('zenstack');
6575

@@ -131,6 +141,12 @@ export function createProgram() {
131141
.option('--no-prisma-style', 'do not use prisma style')
132142
.action(formatAction);
133143

144+
program
145+
.command('check')
146+
.description('Check a ZenStack schema file for syntax or semantic errors.')
147+
.addOption(schemaOption)
148+
.action(checkAction);
149+
134150
// make sure config is loaded before actions run
135151
program.hook('preAction', async (_, actionCommand) => {
136152
let configFile: string | undefined = actionCommand.opts().config;

0 commit comments

Comments
 (0)