diff --git a/src/m365/entra/commands/groupsetting/groupsetting-add.spec.ts b/src/m365/entra/commands/groupsetting/groupsetting-add.spec.ts index bd43e5a4011..48c430a7967 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-add.spec.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-add.spec.ts @@ -12,12 +12,14 @@ import { session } from '../../../../utils/session.js'; import { sinonUtil } from '../../../../utils/sinonUtil.js'; import commands from '../../commands.js'; import command from './groupsetting-add.js'; +import { options } from './groupsetting-get.js'; describe(commands.GROUPSETTING_ADD, () => { let log: string[]; let logger: Logger; let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; + let commandOptionsSchema: typeof options; before(() => { sinon.stub(auth, 'restoreAuth').resolves(); @@ -26,6 +28,7 @@ describe(commands.GROUPSETTING_ADD, () => { sinon.stub(session, 'getId').returns(''); auth.connection.active = true; commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options; }); beforeEach(() => { @@ -481,14 +484,14 @@ describe(commands.GROUPSETTING_ADD, () => { new CommandError(`A conflicting object with one or more of the specified property values is present in the directory.`)); }); - it('fails validation if the templateId is not a valid GUID', async () => { - const actual = await command.validate({ options: { templateId: 'invalid' } }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if the templateId is not a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ templateId: 'invalid' }); + assert.strictEqual(actual.success, false); }); - it('passes validation if the templateId is a valid GUID', async () => { - const actual = await command.validate({ options: { templateId: '68be84bf-a585-4776-80b3-30aa5207aa22' } }, commandInfo); - assert.strictEqual(actual, true); + it('passes validation if the templateId is a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ templateId: '68be84bf-a585-4776-80b3-30aa5207aa22' }); + assert.strictEqual(actual.success, true); }); it('allows unknown properties', () => { diff --git a/src/m365/entra/commands/groupsetting/groupsetting-add.ts b/src/m365/entra/commands/groupsetting/groupsetting-add.ts index 325b8439bfb..51c47442ed3 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-add.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-add.ts @@ -1,19 +1,22 @@ import { GroupSettingTemplate } from '@microsoft/microsoft-graph-types'; -import GlobalOptions from '../../../../GlobalOptions.js'; +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; +import { globalOptionsZod } from '../../../../Command.js'; import request, { CliRequestOptions } from '../../../../request.js'; -import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +const options = z.looseObject({ + ...globalOptionsZod.shape, + templateId: z.uuid().alias('i') +}); + +declare type Options = z.infer; + interface CommandArgs { options: Options; } -interface Options extends GlobalOptions { - templateId: string; -} - class EntraGroupSettingAddCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTING_ADD; @@ -23,37 +26,14 @@ class EntraGroupSettingAddCommand extends GraphCommand { return 'Creates a group setting'; } - constructor() { - super(); - - this.#initOptions(); - this.#initValidators(); - } - - #initOptions(): void { - this.options.unshift( - { - option: '-i, --templateId ' - } - ); - } - - #initValidators(): void { - this.validators.push( - async (args: CommandArgs) => { - if (!validation.isValidGuid(args.options.templateId)) { - return `${args.options.templateId} is not a valid GUID`; - } - - return true; - } - ); - } - public allowUnknownOptions(): boolean | undefined { return true; } + public get schema(): z.ZodType | undefined { + return options; + } + public async commandAction(logger: Logger, args: CommandArgs): Promise { if (this.verbose) { await logger.logToStderr(`Retrieving group setting template with id '${args.options.templateId}'...`); diff --git a/src/m365/entra/commands/groupsetting/groupsetting-get.spec.ts b/src/m365/entra/commands/groupsetting/groupsetting-get.spec.ts index 608a117b00f..3194ba46af3 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-get.spec.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-get.spec.ts @@ -11,13 +11,14 @@ import { pid } from '../../../../utils/pid.js'; import { session } from '../../../../utils/session.js'; import { sinonUtil } from '../../../../utils/sinonUtil.js'; import commands from '../../commands.js'; -import command from './groupsetting-get.js'; +import command, { options } from './groupsetting-get.js'; describe(commands.GROUPSETTING_GET, () => { let log: string[]; let logger: Logger; let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; + let commandOptionsSchema: typeof options; before(() => { sinon.stub(auth, 'restoreAuth').resolves(); @@ -26,6 +27,7 @@ describe(commands.GROUPSETTING_GET, () => { sinon.stub(session, 'getId').returns(''); auth.connection.active = true; commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options; }); beforeEach(() => { @@ -153,24 +155,13 @@ describe(commands.GROUPSETTING_GET, () => { new CommandError(`Resource '1caf7dcd-7e83-4c3a-94f7-932a1299c843' does not exist or one of its queried reference-property objects are not present.`)); }); - it('fails validation if the id is not a valid GUID', async () => { - const actual = await command.validate({ options: { id: '123' } }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if the id is not a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: '123' }); + assert.strictEqual(actual.success, false); }); - it('passes validation if the id is a valid GUID', async () => { - const actual = await command.validate({ options: { id: '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } }, commandInfo); - assert.strictEqual(actual, true); - }); - - it('supports specifying id', () => { - const options = command.options; - let containsOption = false; - options.forEach(o => { - if (o.option.indexOf('--id') > -1) { - containsOption = true; - } - }); - assert(containsOption); + it('passes validation if the id is a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: '1caf7dcd-7e83-4c3a-94f7-932a1299c844' }); + assert.strictEqual(actual.success, true); }); }); diff --git a/src/m365/entra/commands/groupsetting/groupsetting-get.ts b/src/m365/entra/commands/groupsetting/groupsetting-get.ts index 95305c50ae8..3ae1767de10 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-get.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-get.ts @@ -1,18 +1,21 @@ +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; -import GlobalOptions from '../../../../GlobalOptions.js'; +import { globalOptionsZod } from '../../../../Command.js'; import request, { CliRequestOptions } from '../../../../request.js'; -import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +export const options = z.strictObject({ + ...globalOptionsZod.shape, + id: z.uuid().alias('i') +}); + +declare type Options = z.infer; + interface CommandArgs { options: Options; } -interface Options extends GlobalOptions { - id: string; -} - class EntraGroupSettingGetCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTING_GET; @@ -22,31 +25,8 @@ class EntraGroupSettingGetCommand extends GraphCommand { return 'Gets information about the particular group setting'; } - constructor() { - super(); - - this.#initOptions(); - this.#initValidators(); - } - - #initOptions(): void { - this.options.unshift( - { - option: '-i, --id ' - } - ); - } - - #initValidators(): void { - this.validators.push( - async (args: CommandArgs) => { - if (!validation.isValidGuid(args.options.id)) { - return `${args.options.id} is not a valid GUID`; - } - - return true; - } - ); + public get schema(): z.ZodType | undefined { + return options; } public async commandAction(logger: Logger, args: CommandArgs): Promise { diff --git a/src/m365/entra/commands/groupsetting/groupsetting-list.ts b/src/m365/entra/commands/groupsetting/groupsetting-list.ts index eeab4ae3ebd..ae75ad7b8e3 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-list.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-list.ts @@ -1,9 +1,13 @@ import { GroupSetting } from '@microsoft/microsoft-graph-types'; +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; +import { globalOptionsZod } from '../../../../Command.js'; import { odata } from '../../../../utils/odata.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +export const options = z.strictObject({ ...globalOptionsZod.shape }); + class EntraGroupSettingListCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTING_LIST; @@ -17,6 +21,10 @@ class EntraGroupSettingListCommand extends GraphCommand { return ['id', 'displayName']; } + public get schema(): z.ZodType | undefined { + return options; + } + public async commandAction(logger: Logger): Promise { try { const groupSettings = await odata.getAllItems(`${this.resource}/v1.0/groupSettings`); diff --git a/src/m365/entra/commands/groupsetting/groupsetting-remove.spec.ts b/src/m365/entra/commands/groupsetting/groupsetting-remove.spec.ts index 3babce302a0..6029296c6e6 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-remove.spec.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-remove.spec.ts @@ -1,5 +1,4 @@ import assert from 'assert'; -import fs from 'fs'; import sinon from 'sinon'; import auth from '../../../../Auth.js'; import { cli } from '../../../../cli/cli.js'; @@ -12,12 +11,13 @@ import { pid } from '../../../../utils/pid.js'; import { session } from '../../../../utils/session.js'; import { sinonUtil } from '../../../../utils/sinonUtil.js'; import commands from '../../commands.js'; -import command from './groupsetting-remove.js'; +import command, { options } from './groupsetting-remove.js'; describe(commands.GROUPSETTING_REMOVE, () => { let log: string[]; let logger: Logger; let commandInfo: CommandInfo; + let commandOptionsSchema: typeof options; let promptIssued: boolean = false; before(() => { @@ -25,9 +25,9 @@ describe(commands.GROUPSETTING_REMOVE, () => { sinon.stub(telemetry, 'trackEvent').resolves(); sinon.stub(pid, 'getProcessName').returns(''); sinon.stub(session, 'getId').returns(''); - sinon.stub(fs, 'readFileSync').returns('abc'); auth.connection.active = true; commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options; }); beforeEach(() => { @@ -153,35 +153,13 @@ describe(commands.GROUPSETTING_REMOVE, () => { new CommandError('File Not Found.')); }); - it('supports specifying id', () => { - const options = command.options; - let containsOption = false; - options.forEach(o => { - if (o.option.indexOf('--id') > -1) { - containsOption = true; - } - }); - assert(containsOption); - }); - - it('supports specifying confirmation flag', () => { - const options = command.options; - let containsOption = false; - options.forEach(o => { - if (o.option.indexOf('--force') > -1) { - containsOption = true; - } - }); - assert(containsOption); - }); - - it('fails validation if the id is not a valid GUID', async () => { - const actual = await command.validate({ options: { id: 'abc' } }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if the id is not a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: 'abc' }); + assert.strictEqual(actual.success, false); }); - it('passes validation when the id is a valid GUID', async () => { - const actual = await command.validate({ options: { id: '2c1ba4c4-cd9b-4417-832f-92a34bc34b2a' } }, commandInfo); - assert.strictEqual(actual, true); + it('passes validation when the id is a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: '2c1ba4c4-cd9b-4417-832f-92a34bc34b2a' }); + assert.strictEqual(actual.success, true); }); }); diff --git a/src/m365/entra/commands/groupsetting/groupsetting-remove.ts b/src/m365/entra/commands/groupsetting/groupsetting-remove.ts index 7f89a7a3d03..001116f708b 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-remove.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-remove.ts @@ -1,20 +1,23 @@ +import { z } from 'zod'; import { cli } from '../../../../cli/cli.js'; import { Logger } from '../../../../cli/Logger.js'; -import GlobalOptions from '../../../../GlobalOptions.js'; +import { globalOptionsZod } from '../../../../Command.js'; import request, { CliRequestOptions } from '../../../../request.js'; -import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +export const options = z.strictObject({ + ...globalOptionsZod.shape, + id: z.uuid().alias('i'), + force: z.boolean().optional().alias('f') +}); + +declare type Options = z.infer; + interface CommandArgs { options: Options; } -interface Options extends GlobalOptions { - id: string; - force?: boolean; -} - class EntraGroupSettingRemoveCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTING_REMOVE; @@ -24,43 +27,8 @@ class EntraGroupSettingRemoveCommand extends GraphCommand { return 'Removes the particular group setting'; } - constructor() { - super(); - - this.#initTelemetry(); - this.#initOptions(); - this.#initValidators(); - } - - #initTelemetry(): void { - this.telemetry.push((args: CommandArgs) => { - Object.assign(this.telemetryProperties, { - force: (!(!args.options.force)).toString() - }); - }); - } - - #initOptions(): void { - this.options.unshift( - { - option: '-i, --id ' - }, - { - option: '-f, --force' - } - ); - } - - #initValidators(): void { - this.validators.push( - async (args: CommandArgs) => { - if (!validation.isValidGuid(args.options.id)) { - return `${args.options.id} is not a valid GUID`; - } - - return true; - } - ); + public get schema(): z.ZodType | undefined { + return options; } public async commandAction(logger: Logger, args: CommandArgs): Promise { diff --git a/src/m365/entra/commands/groupsetting/groupsetting-set.spec.ts b/src/m365/entra/commands/groupsetting/groupsetting-set.spec.ts index 19ea4347cc3..03d40866aa1 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-set.spec.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-set.spec.ts @@ -12,12 +12,14 @@ import { session } from '../../../../utils/session.js'; import { sinonUtil } from '../../../../utils/sinonUtil.js'; import commands from '../../commands.js'; import command from './groupsetting-set.js'; +import { options } from './groupsetting-get.js'; describe(commands.GROUPSETTING_SET, () => { let log: string[]; let logger: Logger; let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; + let commandOptionsSchema: typeof options; before(() => { sinon.stub(auth, 'restoreAuth').resolves(); @@ -26,6 +28,7 @@ describe(commands.GROUPSETTING_SET, () => { sinon.stub(session, 'getId').returns(''); auth.connection.active = true; commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options; }); beforeEach(() => { @@ -383,14 +386,14 @@ describe(commands.GROUPSETTING_SET, () => { new CommandError(`Resource '62375ab9-6b52-47ed-826b-58e47e0e304c' does not exist or one of its queried reference-property objects are not present.`)); }); - it('fails validation if the id is not a valid GUID', async () => { - const actual = await command.validate({ options: { id: 'invalid' } }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if the id is not a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: 'invalid' }); + assert.strictEqual(actual.success, false); }); - it('passes validation if the id is a valid GUID', async () => { - const actual = await command.validate({ options: { id: '68be84bf-a585-4776-80b3-30aa5207aa22' } }, commandInfo); - assert.strictEqual(actual, true); + it('passes validation if the id is a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: '68be84bf-a585-4776-80b3-30aa5207aa22' }); + assert.strictEqual(actual.success, true); }); it('allows unknown properties', () => { diff --git a/src/m365/entra/commands/groupsetting/groupsetting-set.ts b/src/m365/entra/commands/groupsetting/groupsetting-set.ts index 78be6f8c18d..4a75d87d166 100644 --- a/src/m365/entra/commands/groupsetting/groupsetting-set.ts +++ b/src/m365/entra/commands/groupsetting/groupsetting-set.ts @@ -1,19 +1,22 @@ import { GroupSetting } from '@microsoft/microsoft-graph-types'; +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; -import GlobalOptions from '../../../../GlobalOptions.js'; +import { globalOptionsZod } from '../../../../Command.js'; import request, { CliRequestOptions } from '../../../../request.js'; -import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +const options = z.looseObject({ + ...globalOptionsZod.shape, + id: z.uuid().alias('i') +}); + +declare type Options = z.infer; + interface CommandArgs { options: Options; } -interface Options extends GlobalOptions { - id: string; -} - class EntraGroupSettingSetCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTING_SET; @@ -27,31 +30,8 @@ class EntraGroupSettingSetCommand extends GraphCommand { return true; } - constructor() { - super(); - - this.#initOptions(); - this.#initValidators(); - } - - #initOptions(): void { - this.options.unshift( - { - option: '-i, --id ' - } - ); - } - - #initValidators(): void { - this.validators.push( - async (args: CommandArgs) => { - if (!validation.isValidGuid(args.options.id)) { - return `${args.options.id} is not a valid GUID`; - } - - return true; - } - ); + public get schema(): z.ZodType | undefined { + return options; } public async commandAction(logger: Logger, args: CommandArgs): Promise { diff --git a/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.spec.ts b/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.spec.ts index ca0bc1c559c..228f0f3d053 100644 --- a/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.spec.ts +++ b/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.spec.ts @@ -11,14 +11,14 @@ import { pid } from '../../../../utils/pid.js'; import { session } from '../../../../utils/session.js'; import { sinonUtil } from '../../../../utils/sinonUtil.js'; import commands from '../../commands.js'; -import command from './groupsettingtemplate-get.js'; -import { settingsNames } from '../../../../settingsNames.js'; +import command, { options } from './groupsettingtemplate-get.js'; describe(commands.GROUPSETTINGTEMPLATE_GET, () => { let log: string[]; let logger: Logger; let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; + let commandOptionsSchema: typeof options; before(() => { sinon.stub(auth, 'restoreAuth').resolves(); @@ -27,6 +27,7 @@ describe(commands.GROUPSETTINGTEMPLATE_GET, () => { sinon.stub(session, 'getId').returns(''); auth.connection.active = true; commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options; }); beforeEach(() => { @@ -48,8 +49,7 @@ describe(commands.GROUPSETTINGTEMPLATE_GET, () => { afterEach(() => { sinonUtil.restore([ - request.get, - cli.getSettingWithDefaultValue + request.get ]); }); @@ -125,44 +125,28 @@ describe(commands.GROUPSETTINGTEMPLATE_GET, () => { new CommandError('An error has occurred')); }); - it('fails validation if neither the id nor the displayName are specified', async () => { - sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => { - if (settingName === settingsNames.prompt) { - return false; - } - - return defaultValue; - }); - - const actual = await command.validate({ options: {} }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if neither the id nor the displayName are specified', () => { + const actual = commandOptionsSchema.safeParse({}); + assert.strictEqual(actual.success, false); }); - it('fails validation if both the id and the displayName are specified', async () => { - sinon.stub(cli, 'getSettingWithDefaultValue').callsFake((settingName, defaultValue) => { - if (settingName === settingsNames.prompt) { - return false; - } - - return defaultValue; - }); - - const actual = await command.validate({ options: { id: '68be84bf-a585-4776-80b3-30aa5207aa22', displayName: 'Group.Unified' } }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if both the id and the displayName are specified', () => { + const actual = commandOptionsSchema.safeParse({ id: '68be84bf-a585-4776-80b3-30aa5207aa22', displayName: 'Group.Unified' }); + assert.strictEqual(actual.success, false); }); - it('fails validation if the id is not a valid GUID', async () => { - const actual = await command.validate({ options: { id: 'invalid' } }, commandInfo); - assert.notStrictEqual(actual, true); + it('fails validation if the id is not a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: 'invalid' }); + assert.strictEqual(actual.success, false); }); - it('passes validation if the id is a valid GUID', async () => { - const actual = await command.validate({ options: { id: '68be84bf-a585-4776-80b3-30aa5207aa22' } }, commandInfo); - assert.strictEqual(actual, true); + it('passes validation if the id is a valid GUID', () => { + const actual = commandOptionsSchema.safeParse({ id: '68be84bf-a585-4776-80b3-30aa5207aa22' }); + assert.strictEqual(actual.success, true); }); - it('passes validation if the displayName is specified', async () => { - const actual = await command.validate({ options: { displayName: 'Group.Unified' } }, commandInfo); - assert.strictEqual(actual, true); + it('passes validation if the displayName is specified', () => { + const actual = commandOptionsSchema.safeParse({ displayName: 'Group.Unified' }); + assert.strictEqual(actual.success, true); }); }); diff --git a/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.ts b/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.ts index 13646081cd8..07974bfea15 100644 --- a/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.ts +++ b/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-get.ts @@ -1,20 +1,23 @@ import { GroupSettingTemplate } from '@microsoft/microsoft-graph-types'; +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; -import GlobalOptions from '../../../../GlobalOptions.js'; +import { globalOptionsZod } from '../../../../Command.js'; import { odata } from '../../../../utils/odata.js'; -import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +export const options = z.strictObject({ + ...globalOptionsZod.shape, + id: z.uuid().optional().alias('i'), + displayName: z.string().optional().alias('n') +}); + +declare type Options = z.infer; + interface CommandArgs { options: Options; } -interface Options extends GlobalOptions { - id?: string; - displayName?: string; -} - class EntraGroupSettingTemplateGetCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTINGTEMPLATE_GET; @@ -24,50 +27,19 @@ class EntraGroupSettingTemplateGetCommand extends GraphCommand { return 'Gets information about the specified Entra group settings template'; } - constructor() { - super(); - - this.#initTelemetry(); - this.#initOptions(); - this.#initValidators(); - this.#initOptionSets(); - } - - #initTelemetry(): void { - this.telemetry.push((args: CommandArgs) => { - Object.assign(this.telemetryProperties, { - id: typeof args.options.id !== 'undefined', - displayName: typeof args.options.displayName !== 'undefined' - }); - }); - } - - #initOptions(): void { - this.options.unshift( - { - option: '-i, --id [id]' - }, - { - option: '-n, --displayName [displayName]' - } - ); + public get schema(): z.ZodType | undefined { + return options; } - #initValidators(): void { - this.validators.push( - async (args: CommandArgs) => { - if (args.options.id && - !validation.isValidGuid(args.options.id)) { - return `${args.options.id} is not a valid GUID`; + public getRefinedSchema(schema: typeof options): z.ZodObject | undefined { + return schema + .refine(options => [options.id, options.displayName].filter(Boolean).length === 1, { + error: 'Specify either id or displayName', + params: { + customCode: 'optionSet', + options: ['id', 'displayName'] } - - return true; - } - ); - } - - #initOptionSets(): void { - this.optionSets.push({ options: ['id', 'displayName'] }); + }); } public async commandAction(logger: Logger, args: CommandArgs): Promise { diff --git a/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-list.ts b/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-list.ts index 301d0654cd6..61fce91f149 100644 --- a/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-list.ts +++ b/src/m365/entra/commands/groupsettingtemplate/groupsettingtemplate-list.ts @@ -1,9 +1,13 @@ import { GroupSettingTemplate } from '@microsoft/microsoft-graph-types'; +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; +import { globalOptionsZod } from '../../../../Command.js'; import { odata } from '../../../../utils/odata.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +export const options = z.strictObject({ ...globalOptionsZod.shape }); + class EntraGroupSettingTemplateListCommand extends GraphCommand { public get name(): string { return commands.GROUPSETTINGTEMPLATE_LIST; @@ -17,6 +21,10 @@ class EntraGroupSettingTemplateListCommand extends GraphCommand { return ['id', 'displayName']; } + public get schema(): z.ZodType | undefined { + return options; + } + public async commandAction(logger: Logger): Promise { try { const templates = await odata.getAllItems(`${this.resource}/v1.0/groupSettingTemplates`);