jquense/yup · error · ValidationError

${errors.length} errors occurred

Error message

${errors.length} errors occurred

What it means

validateSync() collects all validation errors and, if any were found, throws a single yup ValidationError whose message is "N errors occurred". The individual error details are on the error.value/inner errors — the top message is just a summary count.

Source

Thrown at src/schema.ts:621

  validateSync(
    value: any,
    options?: ValidateOptions<TContext>,
  ): this['__outputType'] {
    let schema = this.resolve({ ...options, value });
    let result: any;
    let disableStackTrace =
      options?.disableStackTrace ?? schema.spec.disableStackTrace;

    schema._validate(
      value,
      { ...options, sync: true },
      (error, parsed) => {
        if (ValidationError.isError(error)) error.value = parsed;
        throw error;
      },
      (errors, validated) => {
        if (errors.length)
          throw new ValidationError(
            errors!,
            value,
            undefined,
            undefined,
            disableStackTrace,
          );
        result = validated;
      },
    );

    return result;
  }

  isValid(value: any, options?: ValidateOptions<TContext>): Promise<boolean> {
    return this.validate(value, options).then(
      () => true,
      (err) => {
        if (ValidationError.isError(err)) return false;

View on GitHub (pinned to ff31eee8a2)

Solutions

  1. Wrap validateSync() in try/catch and read err.value / err.inner / err.path for details
  2. Use err.errors array to get all individual messages
  3. If a promise-based flow is fine, use await validate() which rejects with the same details

Example fix

// before
const val = schema.validateSync(data); // throws '2 errors occurred'
// after
let val;
try {
  val = schema.validateSync(data);
} catch (err) {
  console.log(err.errors); // individual messages
}
Defensive patterns

Strategy: try-catch

Validate before calling

const { error } = schema.validateSync(data, { abortEarly: false }) instanceof ValidationError ? { error: ... } : {};
// simpler: pre-check with schema.isValidSync(data)

Type guard

function isYupValidationError(e: unknown): e is yup.ValidationError {
  return yup.ValidationError.isError(e as any);
}

Try / catch

try {
  value = schema.validateSync(data);
} catch (e) {
  if (yup.ValidationError.isError(e)) {
    const messages = e.errors; // individual messages in e.errors
  } else throw e;
}

Prevention

When it happens

Trigger: Calling schema.validateSync(value) when one or more fields fail validation; thrown instead of returning, even for a single error, because the sync path always aggregates into a ValidationError.

Common situations: Synchronous form/server-side validation where submitted data fails multiple rules; code written for validate() (async, returns undefined on failure) ported to validateSync() without a try/catch.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of jquense/yup@ff31eee8a2 (2026-08-31). Data as JSON: /api/errors/04fd2ad2533e93b7. Report an issue: GitHub.