# dotenv-ajv

> Dotenv json schema validator

Latest version **1.0.1** (published 2022-03-18) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install dotenv-ajv
pnpm add dotenv-ajv
yarn add dotenv-ajv
bun add dotenv-ajv
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-03-18 |
| First published | 2022-03-18 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | bbriatte |
| Maintainers | bbriatte |
| Keywords | dotenv, ajv, json, schema |

## Links

- npm: https://www.npmjs.com/package/dotenv-ajv
- Repository: https://github.com/bbriatte/dotenv-ajv
- Homepage: https://github.com/bbriatte/dotenv-ajv#readme
- Issues: https://github.com/bbriatte/dotenv-ajv/issues
- npm.io page: https://npm.io/package/dotenv-ajv

## Dependencies (2)

- [ajv](https://npm.io/package/ajv.md) ^8.10.0
- [dotenv](https://npm.io/package/dotenv.md) ^16.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-03-18
- 1.0.0 — 2022-03-18

## README

# dotenv-ajv

[![npm version](https://badge.fury.io/js/dotenv-ajv.svg)](https://badge.fury.io/js/dotenv-ajv)
[![npm downloads](https://badgen.net/npm/dt/dotenv-ajv)](https://badgen.net/npm/dt/dotenv-ajv)

dotenv-ajv is a JS/TS library use to load and validate your env files using JSON schema 

## Installation
`npm i dotenv-ajv`

## Usage

First create a `.env` file
```dosini
NODE_ENV=development
TOKEN=yourapptoken
```

Next create JSON schema using [typebox](https://github.com/sinclairzx81/typebox) in a `env.type.ts` file

```typescript
import {Static, Type} from "@sinclair/typebox"; // `npm i @sinclair/typebox` is required

export enum NodeEnv {
    dev= 'development',
    prod = 'production',
    staging = 'staging'
}

export const EnvSchema = Type.Object({
    NODE_ENV: Type.Enum(NodeEnv, {
        default: NodeEnv.dev
    }),
    PORT: Type.String({
        default: '3000'
    }),
    TOKEN: Type.String()
});

export type EnvType = Static<typeof EnvSchema>;

// Create this function if you want auto-complete
export function ENV(): EnvType {
    return process.env as EnvType;
}
```

Finally, in the `index.ts` load and validate your env file

```typescript
import {loadAndValidateEnv} from "dotenv-ajv";
import {EnvSchema, ENV} from "./env.type.ts";

loadAndValidateEnv(EnvSchema); // Throws an error if your .env file isn't valid

// Now you can use the ENV function
console.log(ENV().PORT); // 3000
console.log(ENV().NODE_ENV); // development
console.log(ENV().TOKEN); // yourapptoken
```

---
_Source: https://npm.io/package/dotenv-ajv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
