From f6c457a0208e642bf7407e1cfba57b2373ac3b7a Mon Sep 17 00:00:00 2001 From: Nitin Rajpoot Date: Wed, 24 Sep 2025 21:14:52 +0530 Subject: [PATCH 1/3] error fixed --- src/core/friendly_errors/param_validator.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/core/friendly_errors/param_validator.js b/src/core/friendly_errors/param_validator.js index 97f23dfff4..424403c355 100644 --- a/src/core/friendly_errors/param_validator.js +++ b/src/core/friendly_errors/param_validator.js @@ -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)]), 'Object': z.object({}), 'String': z.string() }; @@ -573,10 +573,7 @@ function validateParams(p5, fn, lifecycles) { lifecycles.presetup = function(){ loadP5Constructors(); - if( - p5.disableParameterValidator !== true && - p5.disableFriendlyErrors !== true - ){ + if(p5.disableParameterValidator !== true){ const excludes = ['validate']; for(const f in this){ if(!excludes.includes(f) && !f.startsWith('_') && typeof this[f] === 'function'){ From edf1abc779895848e22c34eeabc6d4cfe14b6105 Mon Sep 17 00:00:00 2001 From: Nitin Rajpoot Date: Wed, 24 Sep 2025 21:27:59 +0530 Subject: [PATCH 2/3] remove some change --- src/core/friendly_errors/param_validator.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/friendly_errors/param_validator.js b/src/core/friendly_errors/param_validator.js index 424403c355..ee5837759c 100644 --- a/src/core/friendly_errors/param_validator.js +++ b/src/core/friendly_errors/param_validator.js @@ -573,7 +573,10 @@ function validateParams(p5, fn, lifecycles) { lifecycles.presetup = function(){ loadP5Constructors(); - if(p5.disableParameterValidator !== true){ + if( + p5.disableParameterValidator !== true && + p5.disableFriendlyErrors !== true + ){ const excludes = ['validate']; for(const f in this){ if(!excludes.includes(f) && !f.startsWith('_') && typeof this[f] === 'function'){ From 24352282db19aa3c1c12433fd6773c47bbaa93f4 Mon Sep 17 00:00:00 2001 From: Nitin Rajpoot Date: Mon, 29 Sep 2025 20:50:26 +0530 Subject: [PATCH 3/3] test case passed --- src/core/friendly_errors/param_validator.js | 23 +++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/friendly_errors/param_validator.js b/src/core/friendly_errors/param_validator.js index ee5837759c..9845d3807d 100644 --- a/src/core/friendly_errors/param_validator.js +++ b/src/core/friendly_errors/param_validator.js @@ -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]; @@ -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 => { + const nestedIssue = nestedErr[0]; + 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') { - 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); + if (!isSpecialNumber) { + hasConstants = 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]); @@ -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)'); + } + 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? ' +