Skip to content

Commit 2a451b5

Browse files
authored
Migrates 'docs' to Zod. Closes #6915
1 parent b46e017 commit 2a451b5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/m365/commands/docs.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import assert from 'assert';
22
import sinon from 'sinon';
3+
import { z } from 'zod';
34
import { cli } from '../../cli/cli.js';
5+
import { CommandInfo } from '../../cli/CommandInfo.js';
46
import { Logger } from '../../cli/Logger.js';
57
import { telemetry } from '../../telemetry.js';
68
import { app } from '../../utils/app.js';
@@ -16,11 +18,15 @@ describe(commands.DOCS, () => {
1618
let logger: Logger;
1719
let loggerLogSpy: sinon.SinonSpy;
1820
let getSettingWithDefaultValueStub: sinon.SinonStub;
21+
let commandInfo: CommandInfo;
22+
let commandOptionsSchema: z.ZodTypeAny;
1923

2024
before(() => {
2125
sinon.stub(telemetry, 'trackEvent').resolves();
2226
sinon.stub(pid, 'getProcessName').callsFake(() => '');
2327
sinon.stub(session, 'getId').callsFake(() => '');
28+
commandInfo = cli.getCommandInfo(command);
29+
commandOptionsSchema = commandInfo.command.getSchemaToParse()!;
2430
});
2531

2632
beforeEach(() => {
@@ -59,8 +65,18 @@ describe(commands.DOCS, () => {
5965
assert.notStrictEqual(command.description, null);
6066
});
6167

68+
it('passes validation with no options', () => {
69+
const actual = commandOptionsSchema.safeParse({});
70+
assert.strictEqual(actual.success, true);
71+
});
72+
73+
it('fails validation with unknown options', () => {
74+
const actual = commandOptionsSchema.safeParse({ option: "value" });
75+
assert.strictEqual(actual.success, false);
76+
});
77+
6278
it('should log a message and return if autoOpenLinksInBrowser is false', async () => {
63-
await command.action(logger, { options: {} });
79+
await command.action(logger, { options: commandOptionsSchema.parse({}) });
6480
assert(loggerLogSpy.calledWith(app.packageJson().homepage));
6581
});
6682

@@ -74,7 +90,7 @@ describe(commands.DOCS, () => {
7490
}
7591
throw 'Invalid url';
7692
});
77-
await command.action(logger, { options: {} });
93+
await command.action(logger, { options: commandOptionsSchema.parse({}) });
7894
assert(openStub.calledWith(app.packageJson().homepage));
7995
});
8096
});

src/m365/commands/docs.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import { z } from 'zod';
12
import { cli } from '../../cli/cli.js';
23
import { Logger } from '../../cli/Logger.js';
4+
import { globalOptionsZod } from '../../Command.js';
35
import { settingsNames } from '../../settingsNames.js';
46
import { app } from '../../utils/app.js';
57
import { browserUtil } from '../../utils/browserUtil.js';
68
import AnonymousCommand from '../base/AnonymousCommand.js';
79
import commands from './commands.js';
810

11+
const options = globalOptionsZod.strict();
12+
913
class DocsCommand extends AnonymousCommand {
1014
public get name(): string {
1115
return commands.DOCS;
@@ -15,6 +19,10 @@ class DocsCommand extends AnonymousCommand {
1519
return 'Returns the CLI for Microsoft 365 docs webpage URL';
1620
}
1721

22+
public get schema(): z.ZodTypeAny | undefined {
23+
return options;
24+
}
25+
1826
public async commandAction(logger: Logger): Promise<void> {
1927
await logger.log(app.packageJson().homepage);
2028

0 commit comments

Comments
 (0)