Created
July 20, 2020 02:08
-
-
Save invalidred/67f9b114b2bf59332263c51a3d8c34d5 to your computer and use it in GitHub Desktop.
JSONSchema to validate env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ajv from 'ajv' | |
import { JSONSchema7 } from 'json-schema' | |
const ajv = new Ajv({ allErrors: true }) | |
const envSchema: JSONSchema7 = { | |
type: 'object', | |
required: ['SERVICE_NAME', 'ENVIRONMENT', 'LAUNCH_DARKLY_SDK_KEY'], | |
properties: { | |
SERVICE_NAME: { type: 'string' }, | |
ENVIRONMENT: { type: 'string' }, | |
LAUNCH_DARKLY_SDK_KEY: { type: 'string' } | |
} | |
} | |
const isValid = ajv.compile(envSchema) | |
export default function getEnvFromProcess( | |
proc = process, | |
cons = console | |
) { | |
if (!isValid(proc.env)) { | |
// tslint:disable-next-line: no-console | |
cons.error(ajv.errorsText( | |
isValid.errors, | |
{ dataVar: 'process.env' } | |
)) | |
proc.exit(0) | |
} | |
return { | |
SERVICE_NAME: proc.env.SERVICE_NAME as string, | |
ENVIRONMENT: proc.env.ENVIRONMENT as string, | |
LAUNCH_DARKLY_SDK_KEY: proc.env.LAUNCH_DARKLY_SDK_KEY as string | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment