jquense/yup · error · TypeError

A Method name must be provided

Error message

A Method name must be provided

What it means

addMethod()'s second argument is the method name that will be assigned onto the schema prototype, so it must be a string. If it is missing, undefined, or a non-string (number, symbol, object), addMethod throws 'A Method name must be provided'. This happens after the schemaType check, so the target constructor was valid.

Source

Thrown at src/index.ts:59

  DefaultThunk,
} from './types';

function addMethod<T extends ISchema<any>>(
  schemaType: (...arg: any[]) => T,
  name: string,
  fn: (this: T, ...args: any[]) => T,
): void;
function addMethod<T extends abstract new (...args: any) => ISchema<any>>(
  schemaType: T,
  name: string,
  fn: (this: InstanceType<T>, ...args: any[]) => InstanceType<T>,
): void;
function addMethod(schemaType: any, name: string, fn: any) {
  if (!schemaType || !isSchema(schemaType.prototype))
    throw new TypeError('You must provide a yup schema constructor function');

  if (typeof name !== 'string')
    throw new TypeError('A Method name must be provided');
  if (typeof fn !== 'function')
    throw new TypeError('Method function must be provided');

  schemaType.prototype[name] = fn;
}

export type AnyObjectSchema = ObjectSchema<any, any, any, any>;

export type CastOptions = Omit<BaseCastOptions, 'path' | 'resolved'>;

export type {
  AnyMessageParams,
  AnyObject,
  InferType,
  InferType as Asserts,
  ISchema,
  Message,
  MessageParams,

View on GitHub (pinned to ff31eee8a2)

Solutions

  1. Pass the method name as a string: yup.addMethod(yup.string, 'myName', fn).
  2. Check argument order — the signature is (schemaType, name, fn).
  3. Validate the name variable is defined before invoking addMethod.

Example fix

// before
yup.addMethod(yup.string, fn, 'phone')
// after
yup.addMethod(yup.string, 'phone', fn)
Defensive patterns

Strategy: validation

Validate before calling

const addNamedMethod = (type, name, fn) => {
  if (typeof name !== 'string' || name === '') throw new TypeError('method name must be a non-empty string');
  yup.addMethod(type, name, fn);
};

Type guard

const isMethodName = (v) => typeof v === 'string' && v.length > 0;

Prevention

When it happens

Trigger: yup.addMethod(yup.string, undefined, fn), addMethod(yup.date, 42, fn), or name coming from a config/destructured variable that is undefined.

Common situations: Renamed exports breaking a constant that held the method name; calling addMethod with arguments swapped (schemaType, fn, name); generated plugin code with missing metadata.

Related errors


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