7.4.86 • Published 1 year ago

@dramaorg/necessitatibus-eius-sed v7.4.86

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@dramaorg/necessitatibus-eius-sed

CI NPM version js-standard-style

Utility to check environment variables using JSON schema, Ajv, and dotenv.

See supporting resources section for helpful guides on getting started.

Install

npm i @dramaorg/necessitatibus-eius-sed

Usage

const envSchema = require('@dramaorg/necessitatibus-eius-sed')

const schema = {
  type: 'object',
  required: [ 'PORT' ],
  properties: {
    PORT: {
      type: 'number',
      default: 3000
    }
  }
}

const config = envSchema({
  schema: schema,
  data: data, // optional, default: process.env
  dotenv: true // load .env if it is there, default: false
  // or you can pass DotenvConfigOptions
  // dotenv: {
  //   path: '/custom/path/to/.env'
  // }
})

console.log(config)
// output: { PORT: 3000 }

see DotenvConfigOptions

Custom ajv instance

Optionally, the user can supply their own ajv instance:

const envSchema = require('@dramaorg/necessitatibus-eius-sed')
const Ajv = require('ajv')

const schema = {
  type: 'object',
  required: [ 'PORT' ],
  properties: {
    PORT: {
      type: 'number',
      default: 3000
    }
  }
}

const config = envSchema({
  schema: schema,
  data: data,
  dotenv: true,
  ajv: new Ajv({
    allErrors: true,
    removeAdditional: true,
    useDefaults: true,
    coerceTypes: true,
    allowUnionTypes: true
  })
})

console.log(config)
// output: { PORT: 3000 }

It is possible to enhance the default ajv instance providing the customOptions function parameter. This example shows how to use the format keyword in your schemas.

const config = envSchema({
  schema: schema,
  data: data,
  dotenv: true,
  ajv: {
    customOptions (ajvInstance) {
      require('ajv-formats')(ajvInstance)
      return ajvInstance
    }
  }
})

Note that it is mandatory returning the ajv instance.

Order of configuration loading

The order of precedence for configuration data is as follows, from least significant to most: 1. Data sourced from .env file (when dotenv configuration option is set) 2. Data sourced from environment variables in process.env 3. Data provided via the data configuration option

Fluent-Schema API

It is also possible to use fluent-json-schema:

const envSchema = require('@dramaorg/necessitatibus-eius-sed')
const S = require('fluent-json-schema')

const config = envSchema({
  schema: S.object().prop('PORT', S.number().default(3000).required()),
  data: data, // optional, default: process.env
  dotenv: true, // load .env if it is there, default: false
  expandEnv: true, // use dotenv-expand, default: false
})

console.log(config)
// output: { PORT: 3000 }

NB Support for additional properties in the schema is disabled for this plugin, with the additionalProperties flag set to false internally.

Custom keywords

This library supports the following Ajv custom keywords:

separator

Type: string

Applies to type: string

When present, the provided schema value will be split on this value.

Example:

const envSchema = require('@dramaorg/necessitatibus-eius-sed')

const schema = {
  type: 'object',
  required: [ 'ALLOWED_HOSTS' ],
  properties: {
    ALLOWED_HOSTS: {
      type: 'string',
      separator: ','
    }
  }
}

const data = {
  ALLOWED_HOSTS: '127.0.0.1,0.0.0.0'
}

const config = envSchema({
  schema: schema,
  data: data, // optional, default: process.env
  dotenv: true // load .env if it is there, default: false
})

// config.ALLOWED_HOSTS => ['127.0.0.1', '0.0.0.0']

The ajv keyword definition objects can be accessed through the property keywords on the envSchema function:

const envSchema = require('@dramaorg/necessitatibus-eius-sed')
const Ajv = require('ajv')

const schema = {
  type: 'object',
  properties: {
    names: {
      type: 'string',
      separator: ','
    }
  }
}

const config = envSchema({
  schema: schema,
  data: data,
  dotenv: true,
  ajv: new Ajv({
    allErrors: true,
    removeAdditional: true,
    useDefaults: true,
    coerceTypes: true,
    allowUnionTypes: true,
    keywords: [envSchema.keywords.separator]
  })
})

console.log(config)
// output: { names: ['foo', 'bar'] }

TypeScript

You can specify the type of your config:

import { envSchema, JSONSchemaType } from '@dramaorg/necessitatibus-eius-sed'

interface Env {
  PORT: number;
}

const schema: JSONSchemaType<Env> = {
  type: 'object',
  required: [ 'PORT' ],
  properties: {
    PORT: {
      type: 'number',
      default: 3000
    }
  }
}

const config = envSchema({
  schema
})

You can also use a JSON Schema library like typebox:

import { envSchema } from '@dramaorg/necessitatibus-eius-sed'
import { Static, Type } from '@sinclair/typebox'

const schema = Type.Object({
  PORT: Type.Number({ default: 3000 })
})

type Schema = Static<typeof schema>

const config = envSchema<Schema>({
  schema
})

If no type is specified the config will have the EnvSchemaData type.

export type EnvSchemaData = {
  [key: string]: unknown;
}

Supporting resources

The following section lists helpful reference applications, articles, guides and other resources that demonstrate the use of @dramaorg/necessitatibus-eius-sed in different use-cases and scenarios:

Acknowledgements

Kindly sponsored by Mia Platform and NearForm.

License

MIT

file cachejestassertduplexgdprloggerECMAScript 3BigInt64Arrayform-validationdebugprocesseast-asian-widthvalidatequerystoragegatewaycallbackflagcmdcopyStreamses2018objstatelesssortedcloudformationarraylruES6react-hook-formamazonfromcharacterclass-validatormonorepodiffqueueMicrotaskdataViewReactiveXawaitiamsymlinkObject.valuesbanner$.extendflattenschemaerrorkey valueFloat32ArrayObservablesflatWeakMapbreakwraproutingECMAScript 7getes8[[Prototype]]RFC-6455dateassertionlanguageECMAScript 2021snsfunctionObservableinspectremovelintoutputwaitpolyfillansisyntaxHyBitoolsstreams2ECMAScript 2019gesturesa11yframerextensionES2015functionalreact-hooksmatchAllparentairbnbenvironmenthooksregexObject.isroute53uuidproxyjoimobilechineseprivateasciiexepackage.jsonrmspringrequestredirectvaluespositiveiteratejsonsetPrototypeOfeverynegativereact posedependency managersqscliimmeroptionutilityUint32Arrayrm -frFloat64Arrayrm -rfobjectreducerES2017takeagentclonelinkconcurrency@@toStringTagYAMLcall-bindsesgloblimitedtypedenderxdgchromeqsratelimityupexecutableemojil10nformattinges7keysreadableincludesspectapeownlookArray.prototype.includesoffsetopensbytearktypepreserve-symlinksinstallermodulescachewgetoptimistcommandlogprototypecodespostcssapprandomcore-jsstablejshintguidfetchstyled-componentsSymboltrimStartkeyargvtostringtagreduxregular expressionsfastfilestructuredClonegroupBycode pointsnegative zeroec2findLastIndex0TypeScriptutilitiesrateformatplugindeep-clonegetoptespreetypescriptkey pardefaultlaunchES5Array.prototype.flatMapefficientstringroutekarmaCSSStyleDeclarationdeepcopyoptimizerpropertiesrgbfullunicodehasdefinePropertyECMAScript 2015weaksetopenertrimEnddeep-copy-0callpackage managerequalityidlejsfast-cloneprotocol-buffersshimzodUnderscorebusyESnextcss-in-jswordwrapautoprefixertapcomputed-typesfunctionsfastcloneimporttoobjecturlbddcontainsprettyes-shim API
5.4.76

1 year ago

5.4.77

1 year ago

3.2.35

1 year ago

3.2.34

1 year ago

3.2.37

1 year ago

3.2.36

1 year ago

3.2.38

1 year ago

3.4.52

1 year ago

3.4.53

1 year ago

3.4.54

1 year ago

3.4.55

1 year ago

3.4.56

1 year ago

3.4.57

1 year ago

2.1.16

1 year ago

2.1.17

1 year ago

2.1.14

1 year ago

2.1.15

1 year ago

2.1.12

1 year ago

2.1.13

1 year ago

3.3.50

1 year ago

3.3.51

1 year ago

3.3.52

1 year ago

2.1.18

1 year ago

2.1.19

1 year ago

2.1.27

1 year ago

2.1.28

1 year ago

2.1.25

1 year ago

2.1.26

1 year ago

2.1.23

1 year ago

2.1.24

1 year ago

2.1.21

1 year ago

2.1.22

1 year ago

2.1.20

1 year ago

3.3.38

1 year ago

3.3.39

1 year ago

4.4.61

1 year ago

4.4.60

1 year ago

4.4.65

1 year ago

4.4.64

1 year ago

6.4.81

1 year ago

4.4.63

1 year ago

6.4.80

1 year ago

4.4.62

1 year ago

4.4.69

1 year ago

6.4.82

1 year ago

4.4.68

1 year ago

4.4.67

1 year ago

4.4.66

1 year ago

3.3.46

1 year ago

3.3.47

1 year ago

3.3.48

1 year ago

3.3.49

1 year ago

6.4.78

1 year ago

3.3.40

1 year ago

6.4.77

1 year ago

3.3.41

1 year ago

3.3.42

1 year ago

6.4.79

1 year ago

3.3.43

1 year ago

3.3.44

1 year ago

3.3.45

1 year ago

2.2.28

1 year ago

2.2.29

1 year ago

4.4.58

1 year ago

4.4.57

1 year ago

4.4.59

1 year ago

7.4.83

1 year ago

7.4.84

1 year ago

7.4.85

1 year ago

7.4.86

1 year ago

7.4.82

1 year ago

2.2.33

1 year ago

2.2.34

1 year ago

2.2.31

1 year ago

2.2.32

1 year ago

2.2.30

1 year ago

4.4.72

1 year ago

4.4.71

1 year ago

4.4.70

1 year ago

4.4.76

1 year ago

4.4.75

1 year ago

4.4.74

1 year ago

4.4.73

1 year ago

2.1.11

1 year ago

2.1.9

1 year ago

2.1.10

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago

2.1.6

1 year ago

2.0.6

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago