Closed ZilvinasAbr closed 2 months ago
Thank you for your kind words. Can you share your code that logs this information to the console? Maybe flatten
or getDotPath
is what you are looking for.
@fabian-hiller Hi, thanks for suggestions, they might work, will try them when I have time. Though in my case I am actually not handling the errors. And an issue might be that I am doing the parsing in file global scope, e.g.:
// serverEnvs.mjs
const serverEnvsSchema = object({
SOME_ENV_VARIABLE: string()
})
export const serverEnvs = parse(serverEnvSchema, { SOME_ENV_VARIABLE: process.env['SOME_ENV_VARIABLE'] })
// next.config.mjs
import { serverEnvs } from "./serverEnvs"
// Importing it here makes it validate on build-time of Next.js
Can you try if this works?
import * as v from 'valibot';
const ServerEnvsSchema = v.object({
SOME_ENV_VARIABLE: v.string(),
});
const result = v.safeParse(ServerEnvsSchema, process.env);
if (result.issues) {
console.error('Issues:', v.flatten(result.issues).nested);
throw new Error('Invalid environment variables');
}
export const serverEnvs = result.output;
Yeah, silly me, this looks exactly what I would need, thank you! I guess we can close the issue since no changes to valibot
are necessary to be made.
For some reason I thought only about using try..catch to handle any errors and edit the error output, forgot I can use safeParse
Yeah, my first attempt was also with try/catch š
Note that you can also write a loop to freely format and log the issues:
for (const issue of result.issues) {
console.error(v.getDotPath(issue), issue.message);
}
First of all, thanks for a great library!
There is a small improvement I thought would help me and others. A common case of using Valibot is to parse environment variables. I often get valibot errors on build time since I forget to add some env variables. An issue is that on deployment logs it does not tell which property has failed to be parsed. Example log I just recently got:
I guess the failed key would be seen in the
path
, but the issue is that in logs it does not expand the Array and just show[Array]
.Is there any way possible to maybe somehow show the failed property path?