{"id":16750,"library":"validation-better-auth","title":"Better Auth Validation Plugin","description":"The `validation-better-auth` package provides a flexible and extensible validation plugin specifically designed for the `better-auth` framework. It enables developers to integrate API request validation using various schema definition libraries such as Zod, Valibot, and ArkType. It also supports Yup by internally wrapping Yup schemas to a standard format. This package is currently at version 1.3.4 and shows an active development cadence with frequent patch releases addressing build, module, and type resolution issues. Its key differentiator is its tight integration with `better-auth` and its adapter pattern, allowing for broad compatibility with different validation schema libraries rather than enforcing a specific one.","status":"active","version":"1.3.4","language":"javascript","source_language":"en","source_url":"https://github.com/Daanish2003/better-auth-validator","tags":["javascript","better-auth","auth","typescript","plugin","validation-better-auth"],"install":[{"cmd":"npm install validation-better-auth","lang":"bash","label":"npm"},{"cmd":"yarn add validation-better-auth","lang":"bash","label":"yarn"},{"cmd":"pnpm add validation-better-auth","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This is a plugin for the Better Auth framework and requires it as a peer dependency.","package":"better-auth","optional":false}],"imports":[{"note":"Primarily designed for ESM usage in modern Node.js environments. CommonJS `require` might work with transpilation, but ESM imports are canonical.","wrong":"const { validator } = require('validation-better-auth')","symbol":"validator","correct":"import { validator } from 'validation-better-auth'"},{"note":"Used when explicitly working with the internal standard schema adapter or if custom adapters need to extend it.","symbol":"StandardAdapter","correct":"import { StandardAdapter } from 'validation-better-auth'"},{"note":"This adapter is required to integrate Yup schemas into the validation plugin configuration.","symbol":"YupAdapter","correct":"import { YupAdapter } from 'validation-better-auth'"}],"quickstart":{"code":"import { betterAuth } from \"better-auth\";\nimport { validator, StandardAdapter } from \"validation-better-auth\";\nimport { z } from 'zod'; // Assuming Zod is used for SignupSchema and SignInSchema\n\n// Define your schemas (e.g., using Zod)\nconst SignupSchema = z.object({\n    email: z.string().email(),\n    password: z.string().min(8),\n    confirmPassword: z.string()\n}).refine(data => data.password === data.confirmPassword, {\n    message: \"Passwords don't match\",\n    path: [\"confirmPassword\"],\n});\n\nconst SignInSchema = z.object({\n    email: z.string().email(),\n    password: z.string().min(8)\n});\n\nexport const auth = betterAuth({\n    appName: \"My Validation App\",\n    plugins: [\n        validator(\n            [\n                {\n                    path: \"/sign-up/email\",\n                    schema: SignupSchema,\n                    before: (ctx) => {\n                        console.log('Before signup validation:', ctx.payload);\n                    },\n                    after: (ctx) => {\n                        console.log('After signup validation:', ctx.payload);\n                    }\n                },\n                {\n                    path: \"/sign-in/email\",\n                    schema: SignInSchema,\n                    before: (ctx) => {\n                        console.log('Before signin validation:', ctx.payload);\n                    },\n                    after: (ctx) => {\n                        console.log('After signin validation:', ctx.payload);\n                    }\n                }\n            ]\n        )\n    ]\n});\n\n// In a real application, 'auth' would then be used to configure your authentication routes.\n// For demonstration, we'll just log its presence.\nconsole.log('Better Auth instance configured with validation plugin.');","lang":"typescript","description":"This quickstart demonstrates how to integrate the `validation-better-auth` plugin into the `better-auth` framework, using Zod schemas for request body validation on specific API paths, including `before` and `after` hooks."},"warnings":[{"fix":"Ensure your `tsconfig.json` and build tools are configured for modern module resolution (e.g., `\"module\": \"NodeNext\"`, `\"moduleResolution\": \"NodeNext\"`) and verify imports are using ESM syntax for recent Node.js versions.","message":"Multiple patch releases (v1.3.4, v1.3.3, v1.3.2, v1.3.1) have addressed module resolution and build issues related to ESM and CommonJS. Developers should ensure their build setup correctly handles module imports.","severity":"gotcha","affected_versions":">=1.3.1"},{"fix":"Always install `better-auth` and `validation-better-auth` such that their peer dependency ranges are satisfied. Check the `package.json` for compatible versions.","message":"The package relies on `better-auth` as a peer dependency. Mismatched major versions between `validation-better-auth` and `better-auth` could lead to unexpected behavior or runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For new projects, consider using a 'standard' schema library like Zod or Valibot to leverage direct integration. If using Yup, ensure `YupAdapter` is correctly imported and applied.","message":"While the package supports various schema libraries via adapters (e.g., YupAdapter), using a 'Standard Schema' directly (e.g., Zod, Valibot) might offer a more streamlined experience without the overhead of an adapter layer.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Check your `tsconfig.json` for `module` and `moduleResolution` settings. For modern Node.js, set them to `\"NodeNext\"` or `\"ESNext\"`. Ensure your build process transpiles correctly if targeting older environments.","cause":"Incorrect module resolution due to CommonJS/ESM incompatibility or improper TypeScript configuration.","error":"Error: Cannot find module 'validation-better-auth' or its corresponding type declarations."},{"fix":"Switch to ESM import syntax: `import { validator } from 'validation-better-auth'`. If forced to use CommonJS, ensure your bundler (e.g., Webpack, Rollup) is configured to handle ESM interop correctly, or transpile your code.","cause":"Attempting to `require()` the package in a CommonJS context without proper interop or when the package is primarily ESM.","error":"TypeError: validator is not a function"},{"fix":"For each object in the `validator` plugin array, ensure it has either `schema: YourSchemaInstance` or `adapter: YourAdapter(YourSchemaInstance)` defined for the specific `path`.","cause":"A validation entry in the `validator` plugin array is missing either the `schema` property (for standard schemas) or the `adapter` property (for custom/wrapped schemas).","error":"Plugin validation failed: Expected schema property or adapter property to be defined."}],"ecosystem":"npm"}