jquense/yup · error · TypeError
You must provide a yup schema constructor function
Error message
You must provide a yup schema constructor function
What it means
addMethod() extends a yup schema constructor's prototype with custom methods, and its first argument must be a yup schema constructor whose prototype is itself a schema (checked with isSchema(schemaType.prototype)). Passing nothing, a non-constructor, or an unrelated class throws this TypeError. It is a guard so methods are only added to real yup types.
Source
Thrown at src/index.ts:56
Message,
MessageParams,
ValidateOptions,
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,View on GitHub (pinned to ff31eee8a2)
Solutions
- Pass the schema class, not an instance: yup.addMethod(yup.string, 'myMethod', fn).
- Fix the import so the schema type is defined at call time (check for circular imports).
- If using a custom class, make sure it extends a yup schema type so its prototype passes isSchema().
Example fix
// before yup.addMethod(yup.string(), 'phone', fn) // after yup.addMethod(yup.string, 'phone', fn)
Defensive patterns
Strategy: validation
Validate before calling
import { isSchema } from 'yup';
const safeAddMethod = (type, name, fn) => {
if (!type || !isSchema(type.prototype)) throw new TypeError('addMethod target must be a yup schema constructor');
yup.addMethod(type, name, fn);
}; Type guard
const isSchemaConstructor = (t) => typeof t === 'function' && isSchema(t.prototype);
Prevention
- Pass the class (yup.string), never an instance (yup.string()).
- Guard against circular imports by calling addMethod lazily or in an init module.
- Add a startup assertion that all yup imports are defined.
When it happens
Trigger: yup.addMethod(undefined, 'foo', fn) (bad/missing import), yup.addMethod(String, ...) with a native constructor, or passing an instance instead of the class (yup.addMethod(yup.string(), ...)).
Common situations: Circular import producing undefined yup at module init; tree-shaking/ESM-CJS interop dropping the named export; upgrading yup where addMethod moved or types changed; custom schema classes not extending yup.Schema.
Related errors
- A Method name must be provided
- Method function must be provided
- conditions must return a schema object
- lazy() functions must return a valid schema
- ref must be a string, got:
AI-assisted analysis of jquense/yup@ff31eee8a2 (2026-08-31).
Data as JSON: /api/errors/5cd57aadab8c0103.
Report an issue: GitHub.