Skip to content

Commit

Permalink
Merge branch 'master' of github.com:efstajas/validate-env
Browse files Browse the repository at this point in the history
  • Loading branch information
efstajas committed May 4, 2020
2 parents cb11eb1 + 26e5f8f commit 49e76cb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ First, create your `.env.template` file. This file contains all the .env variabl
FOO=string
BAR=number
FOOBAR=array
FOOBARFOO=boolean
FOOBARFOO=boolean?
```

This `.env.template` means you expect all the variables FOO, BAR, FOOBAR and FOOBARFOO to exist. As you can see, after the =, you can specify a type for that given variable:
This `.env.template` means you expect all the variables FOO, BAR, and FOOBAR to exist. The `?` at the end of the FOOBARFOO variable's type means that it's an optional value — so, validation won't fail if it's not set, but if it is, it must be of type `boolean`. As you can see, after the =, you can specify a type for that given variable:

- `number` means your variable must be numeric, meaning it can be something like `1` or `004` or even `6e+2`.
- `array` means your variable must be a valid JSON array, like `["foo", 123]`. Please note it must be **valid JSON**, meaning strings are to be double-quoted.
- `boolean` means your variable must be either `'true'` or `'false'`
- `string` means your variable must be a valid string. In practice, *any value will pass this test*, because .env variables are always strings.

💡*You can put comments in your env template by using `#`. Great for annotations or sharing default values!*

### Usage

To run the test, simply import the main function, and pass it your `.env.template` file path. It returns a Promise that will resolve to a `ValidatorResult`.
Expand Down
12 changes: 9 additions & 3 deletions src/methods/validateVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ const runTest = (testForType: VariableType, input: string) => {
}
}

export default (expectedType: VariableType, actualValue: any): {
export default (
expectedType: VariableType,
actualValue: any,
optional: boolean = false
): {
pass: true
} | {
pass: false
Expand All @@ -44,15 +48,17 @@ export default (expectedType: VariableType, actualValue: any): {
pass: false
failReason: FailReason.WRONG_TYPE
} => {
if (!(actualValue && actualValue !== '')) {
const exists = (actualValue && actualValue !== '')

if (!optional && !exists) {
return {
pass: false,
failReason: FailReason.MISSING
}
}

const typeCheck = runTest(expectedType, actualValue)
if (!typeCheck) {
if (exists && !typeCheck) {
return {
pass: false,
failReason: FailReason.WRONG_TYPE
Expand Down
14 changes: 9 additions & 5 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ export default (location: string, options: {
}),
})

readInterface.on('line', (line) => {
if (!line || line === '') return
readInterface.on('line', (line = '') => {
const lineWithoutComments = line.replace(/\#(.*)/g, '').trim()
if (lineWithoutComments === '') return

const name = line.split('=')[0]
const expectedType = line.split('=')[1].toUpperCase()
const name = lineWithoutComments.split('=')[0].trim()
const expectedType = lineWithoutComments.split('=')[1]
.toUpperCase()
.replace('?', '')
const optional = (lineWithoutComments.substr(-1) === '?')
const actualValue = process.env[name]

if (!Object.values(VariableType).includes(expectedType as any)) {
return reject(new Error(`Var ${name} has unknown expected type ${expectedType}. Valid types: string, number, array`))
}

const result = validateVar(expectedType as VariableType, actualValue)
const result = validateVar(expectedType as VariableType, actualValue, optional)

if (!result.pass) {
res = {
Expand Down

0 comments on commit 49e76cb

Please sign in to comment.