Julien-R44 / vite-plugin-validate-env

✅ Vite plugin for validating your environment variables
MIT License
164 stars 6 forks source link

Fix: Doc on custom config file #23

Open pewulfman opened 6 months ago

pewulfman commented 6 months ago

Hi,

I have try the new feature to add a custom file. The doc indicate ValidateEnv({ envFile: 'config/env' } when it is actually ValidateEnv({ configFile: 'config/env' }).

Plus when the config file is not found, we get this error :

$ vite dev
The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
error when starting dev server:
Error: Failed to validate environment variables : 

[configFile]: 
  validator is not a function 

Which state that configFIle is an expected env variable, not an options for the env path.

I would expect something like this instead :

Error : `config/env` file not found
Julien-R44 commented 6 months ago

Hey! I rushed this feature a bit. Happy to accept a PR I fixed the readme

biesbjerg commented 6 months ago

~~I had issues with the plugin trying to validate debug and configFile as env vars when passed as options — this PR takes care to remove the keys when normalizing the options: https://github.com/Julien-R44/vite-plugin-validate-env/pull/24~~

EDIT: It seems I misconfigured the plugin — you need to specify schema key AND validator or everything will be threated as a schema.

In my case, I configured configFile in vite.config.ts and schema in defineConfig in my-env.ts.

Maybe it's worth considering removing the non-FullPluginOptions and require the schema-key to avoid any confusion?


Regarding invalid configFile paths, I'm not sure what the best way is to handle it, but maybe something along the lines of this?

diff --git a/src/index.ts b/src/index.ts
index 4cbabdb..effe68d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -8,49 +8,72 @@ import { zodValidation } from './validators/zod/index.js'
 import { builtinValidation } from './validators/builtin/index.js'
 import type { FullPluginOptions, PluginOptions, Schema } from './types.js'

+function getConfigFile(options: PluginOptions | undefined) {
+  if (options && 'configFile' in options && options.configFile) {
+    return {
+      name: options.configFile,
+      isDefault: false,
+    }
+  }
+
+  return {
+    name: 'env',
+    isDefault: true,
+  }
+}
+
 /**
  * Load schema defined in `env.ts` file using unconfig
  */
 async function loadOptions(rootDir: string, inlineConfig?: PluginOptions) {
-  let source = 'env'
-
   /**
    * If configFile is defined in the inlineConfig, use it as the source
    */
-  if (inlineConfig && 'configFile' in inlineConfig && inlineConfig.configFile) {
-    source = inlineConfig.configFile
-  }
+  const configFile = getConfigFile(inlineConfig)

   const loader = createLoader<PluginOptions>({
-    sources: [{ files: source, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
+    sources: [{ files: configFile.name, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
     cwd: rootDir,
     defaults: inlineConfig,
   })

   const result = await loader.load()
+
+  if (!configFile.isDefault && !result.sources.length) {
-
   /**
    * If configFile is defined in the inlineConfig, use it as the source
    */
-  if (inlineConfig && 'configFile' in inlineConfig && inlineConfig.configFile) {
-    source = inlineConfig.configFile
-  }
+  const configFile = getConfigFile(inlineConfig)

   const loader = createLoader<PluginOptions>({
-    sources: [{ files: source, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
+    sources: [{ files: configFile.name, extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
     cwd: rootDir,
     defaults: inlineConfig,
   })

   const result = await loader.load()
+
+  if (!configFile.isDefault && !result.sources.length) {
+    throw new Error(`Missing configuration for vite-plugin-validate-env: ${configFile.name}`)
+  }
+
   const config = result.config

-  if (!config) throw new Error('Missing configuration for vite-plugin-validate-env')
+  if (!config) {
+    throw new Error('Missing configuration for vite-plugin-validate-env')
+  }
aconrad commented 1 month ago

I have the same issue. How did you workaround it?