{"record":{"id":"5cd57aadab8c0103","repo":"jquense/yup","slug":"you-must-provide-a-yup-schema-constructor-function","errorCode":null,"errorMessage":"You must provide a yup schema constructor function","messagePattern":"You must provide a yup schema constructor function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/index.ts","lineNumber":56,"sourceCode":"  Message,\n  MessageParams,\n  ValidateOptions,\n  DefaultThunk,\n} from './types';\n\nfunction addMethod<T extends ISchema<any>>(\n  schemaType: (...arg: any[]) => T,\n  name: string,\n  fn: (this: T, ...args: any[]) => T,\n): void;\nfunction addMethod<T extends abstract new (...args: any) => ISchema<any>>(\n  schemaType: T,\n  name: string,\n  fn: (this: InstanceType<T>, ...args: any[]) => InstanceType<T>,\n): void;\nfunction addMethod(schemaType: any, name: string, fn: any) {\n  if (!schemaType || !isSchema(schemaType.prototype))\n    throw new TypeError('You must provide a yup schema constructor function');\n\n  if (typeof name !== 'string')\n    throw new TypeError('A Method name must be provided');\n  if (typeof fn !== 'function')\n    throw new TypeError('Method function must be provided');\n\n  schemaType.prototype[name] = fn;\n}\n\nexport type AnyObjectSchema = ObjectSchema<any, any, any, any>;\n\nexport type CastOptions = Omit<BaseCastOptions, 'path' | 'resolved'>;\n\nexport type {\n  AnyMessageParams,\n  AnyObject,\n  InferType,\n  InferType as Asserts,","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/index.ts#L38-L74","documentation":"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.","triggerScenarios":"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(), ...)).","commonSituations":"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.","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()."],"exampleFix":"// before\nyup.addMethod(yup.string(), 'phone', fn)\n// after\nyup.addMethod(yup.string, 'phone', fn)","handlingStrategy":"validation","validationCode":"import { isSchema } from 'yup';\nconst safeAddMethod = (type, name, fn) => {\n  if (!type || !isSchema(type.prototype)) throw new TypeError('addMethod target must be a yup schema constructor');\n  yup.addMethod(type, name, fn);\n};","typeGuard":"const isSchemaConstructor = (t) => typeof t === 'function' && isSchema(t.prototype);","tryCatchPattern":null,"preventionTips":["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."],"tags":["yup","addmethod","plugin","type-error"],"backgroundTag":"invalid-add-method-target","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}