Skip to content

Commit 16dd55b

Browse files
committed
migrate param_validator from zod3 to zod 4
- line 325 schema `items` can be access within `def` - line 356 and 360 `_def` is deprecated , use `def` or directly use `options` - line 393 `zodErrorObj.errors[0]` changes to `zodErrorObj.errors[0]` - line 410 `union error` changes to `errors` - line 414 and 459 , `received` changes to `message` ## Important In constants.js, Symbols aren't considered literal values, nor can they be simply compared with ===. This was an oversight in Zod 3.
1 parent 2606c21 commit 16dd55b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/core/friendly_errors/param_validator.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @requires core
44
*/
55
import * as constants from '../constants.js';
6-
import * as z from 'zod';
6+
import { z } from 'zod/v4';
77
import dataDoc from '../../../docs/parameterData.json';
88

99
function validateParams(p5, fn, lifecycles) {
@@ -230,6 +230,10 @@ function validateParams(p5, fn, lifecycles) {
230230
param = param?.replace(/^\.\.\.(.+)\[\]$/, '$1');
231231

232232
let schema = generateTypeSchema(param);
233+
if (!schema || typeof schema.optional !== 'function') {
234+
schema = z.any();
235+
}
236+
233237
if (isOptional) {
234238
schema = schema.optional();
235239
}
@@ -318,7 +322,7 @@ function validateParams(p5, fn, lifecycles) {
318322
}
319323

320324
const numArgs = args.length;
321-
const schemaItems = schema.items;
325+
const schemaItems = schema.def.items;
322326
const numSchemaItems = schemaItems.length;
323327
const numRequiredSchemaItems = schemaItems.filter(item => !item.isOptional()).length;
324328

@@ -353,11 +357,11 @@ function validateParams(p5, fn, lifecycles) {
353357
};
354358

355359
// Default to the first schema, so that we are guaranteed to return a result.
356-
let closestSchema = schema._def.options[0];
360+
let closestSchema = schema.def.options[0];
357361
// We want to return the schema with the lowest score.
358362
let bestScore = Infinity;
359363

360-
const schemaUnion = schema._def.options;
364+
const schemaUnion = schema.def.options;
361365
schemaUnion.forEach(schema => {
362366
const score = scoreSchema(schema);
363367
if (score < bestScore) {
@@ -386,7 +390,7 @@ function validateParams(p5, fn, lifecycles) {
386390
// (after scoring the schema closeness in `findClosestSchema`). Here, we
387391
// always print the first error so that user can work through the errors
388392
// one by one.
389-
let currentError = zodErrorObj.errors[0];
393+
let currentError = zodErrorObj.issues[0];
390394

391395
// Helper function to build a type mismatch message.
392396
const buildTypeMismatchMessage = (actualType, expectedTypeStr, position) => {
@@ -403,11 +407,11 @@ function validateParams(p5, fn, lifecycles) {
403407
const expectedTypes = new Set();
404408
let actualType;
405409

406-
error.unionErrors.forEach(err => {
407-
const issue = err.issues[0];
410+
error.errors.forEach(err => {
411+
const issue = err[0];
408412
if (issue) {
409413
if (!actualType) {
410-
actualType = issue.received;
414+
actualType = issue.message;
411415
}
412416

413417
if (issue.code === 'invalid_type') {
@@ -416,8 +420,9 @@ function validateParams(p5, fn, lifecycles) {
416420
// The case for constants. Since we don't want to print out the actual
417421
// constant values in the error message, the error message will
418422
// direct users to the documentation.
419-
else if (issue.code === 'invalid_literal') {
423+
else if (issue.code === 'invalid_value') {
420424
expectedTypes.add("constant (please refer to documentation for allowed values)");
425+
actualType = args[error.path[0]];
421426
} else if (issue.code === 'custom') {
422427
const match = issue.message.match(/Input not instance of (\w+)/);
423428
if (match) expectedTypes.add(match[1]);
@@ -452,7 +457,7 @@ function validateParams(p5, fn, lifecycles) {
452457
break;
453458
}
454459
case 'invalid_type': {
455-
message += buildTypeMismatchMessage(currentError.received, currentError.expected, currentError.path.join('.'));
460+
message += buildTypeMismatchMessage(currentError.message, currentError.expected, currentError.path.join('.'));
456461
break;
457462
}
458463
case 'too_big': {

0 commit comments

Comments
 (0)