Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MartinM85 opened this issue Nov 7, 2024 · 3 comments
Open

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

MartinM85 opened this issue Nov 7, 2024 · 3 comments

Comments

@MartinM85
Copy link
Contributor

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

@MartinM85 MartinM85 added the bug label Nov 7, 2024
@waldekmastykarz
Copy link
Member

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
Copy link
Contributor Author

@waldekmastykarz Yes, enhancement makes more sense.

Maybe off topic, but does zod support unknown option?

Alternative to

public allowUnknownOptions(): boolean | undefined {
  return true;
}

@waldekmastykarz
Copy link
Member

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

const options = globalOptionsZod
.extend({
authType: zod.alias('t', zod.coercedEnum(AuthType).optional()),
cloud: zod.coercedEnum(CloudType).optional().default(CloudType.Public),
userName: zod.alias('u', z.string().optional()),
password: zod.alias('p', z.string().optional()),
certificateFile: zod.alias('c', z.string().optional()
.refine(filePath => !filePath || fs.existsSync(filePath), filePath => ({
message: `Certificate file ${filePath} does not exist`
}))),
certificateBase64Encoded: z.string().optional(),
thumbprint: z.string().optional(),
appId: z.string().optional(),
tenant: z.string().optional(),
secret: zod.alias('s', z.string().optional()),
connectionName: z.string().optional(),
ensure: z.boolean().optional()
})
.strict();

@MartinM85 MartinM85 changed the title Bug report: unclear message for wrong option in a Zod schema Enhancement: Improve message of invalid option in a Zod schema Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants