8.12.104 • Published 1 year ago

@kollorg/quod-autem-neque v8.12.104

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

@kollorg/quod-autem-neque

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 @kollorg/quod-autem-neque

Usage

const envSchema = require('@kollorg/quod-autem-neque')

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('@kollorg/quod-autem-neque')
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('@kollorg/quod-autem-neque')
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('@kollorg/quod-autem-neque')

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('@kollorg/quod-autem-neque')
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 '@kollorg/quod-autem-neque'

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 '@kollorg/quod-autem-neque'
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 @kollorg/quod-autem-neque in different use-cases and scenarios:

Acknowledgements

Kindly sponsored by Mia Platform and NearForm.

License

MIT

@kollorg/eveniet-praesentium@kollorg/eveniet-et@kollorg/exercitationem-assumenda-qui@kollorg/fuga-harum@kollorg/facere-optio@kollorg/fugiat-animi@kollorg/facilis-eum@kollorg/fugiat-cum-libero@kollorg/fuga-ipsum-illum@kollorg/facere-suscipit@kollorg/fugit-id-quisquam@kollorg/fuga-laborum@kollorg/ipsam-omnis@kollorg/ipsum-porro@kollorg/ipsum-sint@kollorg/itaque-voluptatum@kollorg/ipsam-labore@kollorg/labore-facilis-ab@kollorg/iste-sed-quos@kollorg/itaque-laudantium-nesciunt@kollorg/labore-facilis@kollorg/laudantium-molestias@kollorg/labore-minus-nesciunt@kollorg/laboriosam-neque@kollorg/labore-quidem@kollorg/laudantium-fuga@kollorg/labore-magnam@kollorg/magnam-iure@kollorg/magnam-natus@kollorg/molestias-asperiores@kollorg/minus-eveniet-dolor@kollorg/molestias-non@kollorg/molestias-aperiam@kollorg/molestias-quae-iste@kollorg/modi-saepe@kollorg/molestiae-pariatur@kollorg/mollitia-cupiditate@kollorg/natus-ipsum@kollorg/mollitia-neque-maiores@kollorg/natus-tenetur-laudantium@kollorg/nemo-omnis@kollorg/necessitatibus-qui@kollorg/neque-non@kollorg/nihil-dolorem@kollorg/nisi-deserunt-suscipit@kollorg/nisi-aliquid-eum@kollorg/officia-voluptatem-eius@kollorg/officiis-earum-ducimus@kollorg/odio-cupiditate@kollorg/officia-porro@kollorg/occaecati-totam@kollorg/odit-exercitationem@kollorg/officia-explicabo@kollorg/odio-aspernatur@kollorg/odit-accusantium@kollorg/praesentium-perferendis@kollorg/quae-nam-minus@kollorg/provident-unde@kollorg/placeat-sapiente@kollorg/possimus-dolorem-occaecati@kollorg/possimus-dolores@kollorg/qui-itaque@kollorg/quaerat-veritatis@kollorg/quam-hic@kollorg/quaerat-dolorem-soluta@kollorg/quaerat-error@kollorg/quaerat-ad@kollorg/quidem-dolores-ad@kollorg/quis-dolorem@kollorg/quisquam-totam@kollorg/repudiandae-accusamus-corrupti@kollorg/rerum-quaerat-et@kollorg/saepe-eum@kollorg/rerum-laudantium@kollorg/sapiente-consequatur-maxime@kollorg/rem-rem@kollorg/saepe-rem@kollorg/saepe-ut@kollorg/reiciendis-pariatur@kollorg/reiciendis-rem@kollorg/unde-quae-dignissimos@kollorg/unde-ullam@kollorg/temporibus-quas@kollorg/tenetur-eius@kollorg/ullam-consequuntur@kollorg/ullam-modi@kollorg/tempora-maiores@kollorg/temporibus-explicabo@kollorg/veritatis-eius@kollorg/vitae-tempore-qui@kollorg/veritatis-aliquam@kollorg/veritatis-alias@kollorg/vitae-quia-incidunt@kollorg/vero-quaerat-itaque@kollorg/voluptatum-veniam@kollorg/animi-quisquam@kollorg/amet-minima@kollorg/architecto-dolorum@kollorg/animi-id-saepe@kollorg/aperiam-facere
8.12.104

1 year ago

8.12.101

1 year ago

8.12.103

1 year ago

8.12.102

1 year ago

8.12.100

1 year ago

8.12.98

1 year ago

8.12.99

1 year ago

8.12.97

1 year ago

8.12.96

1 year ago

8.12.95

1 year ago

8.12.94

1 year ago

8.12.93

1 year ago

8.12.92

1 year ago

8.12.91

1 year ago

8.12.90

1 year ago

8.12.89

1 year ago

8.11.82

1 year ago

8.11.83

1 year ago

8.11.80

1 year ago

8.11.81

1 year ago

6.10.66

1 year ago

6.10.65

1 year ago

6.10.64

1 year ago

6.10.63

1 year ago

6.10.62

1 year ago

1.4.20

1 year ago

6.10.61

1 year ago

6.10.60

1 year ago

1.4.21

1 year ago

6.10.67

1 year ago

4.6.33

1 year ago

4.6.34

1 year ago

4.6.31

1 year ago

4.6.32

1 year ago

4.6.37

1 year ago

4.6.35

1 year ago

4.6.36

1 year ago

7.10.67

1 year ago

7.10.69

1 year ago

7.10.68

1 year ago

1.2.17

1 year ago

5.7.41

1 year ago

5.7.40

1 year ago

7.11.80

1 year ago

1.6.26

1 year ago

1.6.25

1 year ago

1.6.28

1 year ago

1.6.27

1 year ago

1.6.29

1 year ago

2.6.31

1 year ago

5.9.53

1 year ago

5.9.54

1 year ago

5.9.55

1 year ago

5.9.56

1 year ago

5.9.51

1 year ago

5.9.52

1 year ago

1.6.31

1 year ago

1.6.30

1 year ago

5.9.57

1 year ago

5.9.58

1 year ago

1.4.19

1 year ago

1.4.18

1 year ago

1.3.17

1 year ago

1.3.18

1 year ago

5.6.40

1 year ago

3.6.31

1 year ago

7.11.75

1 year ago

7.11.76

1 year ago

7.11.77

1 year ago

7.11.78

1 year ago

7.11.79

1 year ago

5.8.50

1 year ago

5.8.51

1 year ago

7.10.70

1 year ago

7.10.72

1 year ago

7.10.71

1 year ago

7.10.74

1 year ago

5.10.58

1 year ago

7.10.73

1 year ago

7.10.75

1 year ago

5.10.59

1 year ago

5.6.37

1 year ago

5.6.39

1 year ago

5.6.38

1 year ago

5.8.41

1 year ago

5.8.42

1 year ago

5.8.43

1 year ago

5.8.44

1 year ago

8.12.83

1 year ago

8.12.84

1 year ago

8.12.87

1 year ago

8.12.88

1 year ago

8.12.85

1 year ago

8.12.86

1 year ago

1.5.21

1 year ago

1.5.23

1 year ago

1.5.22

1 year ago

5.8.49

1 year ago

1.5.25

1 year ago

1.5.24

1 year ago

5.8.45

1 year ago

5.10.60

1 year ago

5.8.46

1 year ago

5.8.47

1 year ago

5.8.48

1 year ago

1.1.17

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.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