Fatima Logofatima

Building your own validator

Fatima is completely agnostic on how you validate your secrets.

There's currently some built-in validators that you can use, but I will show you how to create your own.

Do not proceed on your own with these libraries

Unless you know what you're doing, I highly recommend using the built-in validators for the following libraries:

  • Typia

How the validate function works

Inside your env.config.ts there's an available validate key that you can fill with a function of the following type:

types.d.ts
export type FatimaValidator = (
  env: UnsafeEnvironmentVariables,
  context: FatimaContext
) => Promisable<{
  isValid: boolean;
  errors?: Array<{
    key: string;
    message: string;
  }>;
}>;

Creating a custom validate function with zod

Info

Fatima alredy comes with a built-in zod validator, this is just an example.

Here's an example using zod:

env.config.ts
import { config, validators } from "fatima";
import { z, ZodType } from "zod";
import { EnvKeys } from "env";
 
type ZodEnv = Partial<Record<EnvKeys, ZodType>>;
 
const schema = z.object<ZodEnv>({
  ADMIN_PASSWORD: z.string().min(12),
});
 
export default config({
  validate: ({ env }) => {
    const result = schema.safeParse(env);
 
    const isValid = result.success;
 
    const errors = result.error?.errors.map((error) => ({
      key: error.path.join("."),
      message: error.message,
    }));
 
    return {
      isValid,
      errors,
    };
  },
});

Validate

To validate, just run the validate command:

npm fatima validate

On this page