jquense/yup · error · TypeError

Method function must be provided

Error message

Method function must be provided

What it means

addMethod()'s third argument is the function installed on the schema prototype; it must be a function since it will later be invoked as a schema method with `this` bound to the schema instance. If fn is missing or not callable, addMethod throws 'Method function must be provided'.

Source

Thrown at src/index.ts:61

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,
  AnySchema,
  MixedOptions,

View on GitHub (pinned to ff31eee8a2)

Solutions

  1. Pass an actual function as the third argument: yup.addMethod(yup.string, 'phone', function () { ... }).
  2. Check the fn import/assignment resolves to a function (log typeof fn before addMethod).
  3. Move addMethod registration so it runs after fn is defined (avoid hoisting/TLZ assumptions).

Example fix

// before
yup.addMethod(yup.string, 'phone', validators.phone) // validators.phone undefined
// after
import { phoneValidator } from './validators';
yup.addMethod(yup.string, 'phone', phoneValidator)
Defensive patterns

Strategy: validation

Validate before calling

const addFnMethod = (type, name, fn) => {
  if (typeof fn !== 'function') throw new TypeError(`method for '${name}' must be a function, got ${typeof fn}`);
  yup.addMethod(type, name, fn);
};

Type guard

const isCallable = (v) => typeof v === 'function';

Prevention

When it happens

Trigger: yup.addMethod(yup.string, 'phone', undefined), passing the implementation as a non-callable (object of functions, string), or fn from an import that failed to load.

Common situations: Conditional imports returning undefined (SSR/build-time mismatch); wrapping implementations in objects instead of passing a single function; method defined only in some builds/feature flags.

Related errors


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