pnp / cli-microsoft365

Manage Microsoft 365 and SharePoint Framework projects on any platform
https://aka.ms/cli-m365
MIT License
927 stars 326 forks source link

Enhancement: Improve message of invalid option in a Zod schema #6478

Open MartinM85 opened 2 weeks ago

MartinM85 commented 2 weeks ago

Priority

(Low) Something is a little off

Description

When the command defines options in a Zod schema and the user type specify wrong option the output is

Error: : Unrecognized key(s) in object: 'xxx'

I would expect the message

Error: Invalid option: 'xxx'

Steps to reproduce

Run the command

m365 login --tenant <tenantId> --appId <appId> --xxx

Expected results

Error: Invalid option: 'xxx'
Run 'm365 login -h' for help.

Actual results

Error: : Unrecognized key(s) in object: 'xxx'
Run 'm365 login -h' for help.

Diagnostics

No response

CLI for Microsoft 365 version

v10.1.0

nodejs version

v20.8.1

Operating system (environment)

Windows

Shell

PowerShell

cli doctor

No response

Additional Info

No response

waldekmastykarz commented 2 weeks ago

Thanks for bringing this up, @MartinM85. I wouldn't say it's a bug, because it's the default zod's behavior. That said, I agree that it could be clearer. Let's look for ways to improve it.

MartinM85 commented 2 weeks ago

@waldekmastykarz Yes, enhancement makes more sense.

Maybe off topic, but does zod support unknown option?

Alternative to

public allowUnknownOptions(): boolean | undefined {
  return true;
}
waldekmastykarz commented 2 weeks ago

Yes, it does. By default, schema allows unknown options. You can restrict by including .strict() in the schema definition:

https://github.com/pnp/cli-microsoft365/blob/10765f60bcd058e27d7c8e94226e130b9ac08080/src/m365/commands/login.ts#L13-L31