Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/core/friendly_errors/param_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function validateParams(p5, fn, lifecycles) {
'Boolean': z.boolean(),
'Function': z.function(),
'Integer': z.number().int(),
'Number': z.number(),
'Number': z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was there's a reason to use this instead of z.number().or(z.literal(Infinity)) as per dave's suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

z.number().or(z.literal(Infinity)) only allows finite numbers and Infinity, but not -Infinity.
z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]) allows finite numbers, Infinity, AND -Infinity.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be better alternative : z.number().or(z.literal(Infinity)).or(z.literal(-Infinity));

'Object': z.object({}),
'String': z.string()
};
Expand Down Expand Up @@ -413,6 +413,7 @@ function validateParams(p5, fn, lifecycles) {
const processUnionError = error => {
const expectedTypes = new Set();
let actualType;
let hasConstants = false;

error.errors.forEach(err => {
const issue = err[0];
Expand All @@ -425,12 +426,25 @@ function validateParams(p5, fn, lifecycles) {
actualType = issue.message.split(', received ')[1];
expectedTypes.add(issue.expected);
}
// Handle nested union errors (e.g., Number type with number, Infinity, -Infinity)
else if (issue.code === 'invalid_union') {
issue.errors.forEach(nestedErr => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested failure invalid_union is inside issue.unionErrors. See this doc: https://raftar.io/chene/interfaces/chene.z.ZodInvalidUnionIssue.html

const nestedIssue = nestedErr[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nestedErr won't be an array anymore, it's a zod object?

if (nestedIssue && nestedIssue.code === 'invalid_type') {
expectedTypes.add(nestedIssue.expected);
}
});
}
// The case for constants. Since we don't want to print out the actual
// constant values in the error message, the error message will
// direct users to the documentation.
// Note: Distinguish actual constants from special number values like Infinity/-Infinity
else if (issue.code === 'invalid_value') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you are checking inside the condition for literals infinity and -Infinity but when the union gets failed we check it from : issue.code === 'invalid_union'

expectedTypes.add('constant (please refer to documentation for allowed values)');
actualType = args[error.path[0]];
const isSpecialNumber = issue.values && issue.values.some(val => val === Infinity || val === -Infinity);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will never see literals, so you will always get isSpecialNumber to false.

if (!isSpecialNumber) {
hasConstants = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since isSpecialNumber is always false, hasConstants will be always set to true.

actualType = args[error.path[0]];
}
} else if (issue.code === 'custom') {
const match = issue.message.match(/Input not instance of (\w+)/);
if (match) expectedTypes.add(match[1]);
Expand All @@ -439,6 +453,11 @@ function validateParams(p5, fn, lifecycles) {
}
});

// Only add constants to expected types if constant validation actually failed
if (hasConstants) {
expectedTypes.add('constant (please refer to documentation for allowed values)');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests are accidentally passing, since hasConstants always comes out to true.

}

if (expectedTypes.size > 0) {
if (error.path?.length > 0 && args[error.path[0]] instanceof Promise) {
message += 'Did you mean to put `await` before a loading function? ' +
Expand Down
Loading