4.16.120 • Published 10 months ago

@patrtorg/sint-velit-maiores v4.16.120

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@patrtorg/sint-velit-maiores

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 @patrtorg/sint-velit-maiores

Usage

const envSchema = require('@patrtorg/sint-velit-maiores')

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('@patrtorg/sint-velit-maiores')
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('@patrtorg/sint-velit-maiores')
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('@patrtorg/sint-velit-maiores')

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('@patrtorg/sint-velit-maiores')
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 '@patrtorg/sint-velit-maiores'

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 '@patrtorg/sint-velit-maiores'
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 @patrtorg/sint-velit-maiores in different use-cases and scenarios:

Acknowledgements

Kindly sponsored by Mia Platform and NearForm.

License

MIT

4.15.116

10 months ago

3.13.101

11 months ago

4.15.115

10 months ago

3.13.100

11 months ago

2.7.62

1 year ago

2.7.61

1 year ago

2.7.60

1 year ago

1.6.42

1 year ago

1.6.44

1 year ago

1.6.43

1 year ago

1.6.46

1 year ago

1.6.45

1 year ago

1.6.48

1 year ago

1.6.47

1 year ago

2.7.63

1 year ago

3.8.81

12 months ago

1.4.31

1 year ago

1.4.30

1 year ago

1.4.33

1 year ago

1.4.32

1 year ago

1.4.34

1 year ago

1.2.16

1 year ago

1.2.17

1 year ago

1.2.15

1 year ago

4.13.103

11 months ago

4.13.104

11 months ago

4.13.101

11 months ago

4.13.102

11 months ago

4.13.107

10 months ago

4.13.108

10 months ago

4.13.105

11 months ago

4.13.106

11 months ago

3.12.98

11 months ago

3.12.97

11 months ago

3.12.99

11 months ago

3.12.96

11 months ago

1.3.30

1 year ago

1.1.15

1 year ago

3.10.86

11 months ago

3.10.87

11 months ago

3.10.84

11 months ago

3.10.85

11 months ago

3.10.82

11 months ago

2.6.48

1 year ago

3.10.83

11 months ago

2.6.49

1 year ago

3.7.79

12 months ago

2.6.50

1 year ago

3.7.77

12 months ago

3.7.78

12 months ago

3.7.75

12 months ago

3.7.76

12 months ago

4.14.115

10 months ago

4.14.111

10 months ago

4.14.112

10 months ago

4.14.113

10 months ago

1.5.34

1 year ago

4.14.114

10 months ago

3.7.73

12 months ago

2.6.55

1 year ago

1.5.36

1 year ago

3.7.74

12 months ago

2.6.56

1 year ago

1.5.35

1 year ago

3.7.71

12 months ago

2.6.57

1 year ago

1.5.38

1 year ago

3.7.72

12 months ago

2.6.58

1 year ago

1.5.37

1 year ago

2.6.51

1 year ago

3.7.70

12 months ago

2.6.52

1 year ago

1.5.39

1 year ago

2.6.53

1 year ago

2.6.54

1 year ago

1.5.41

1 year ago

1.5.40

1 year ago

1.5.42

1 year ago

3.7.80

12 months ago

1.3.28

1 year ago

3.7.81

12 months ago

1.3.29

1 year ago

1.3.27

1 year ago

1.2.18

1 year ago

1.2.19

1 year ago

3.11.88

11 months ago

3.11.87

11 months ago

3.11.89

11 months ago

1.2.20

1 year ago

4.16.120

10 months ago

1.2.23

1 year ago

1.2.24

1 year ago

1.2.21

1 year ago

1.2.22

1 year ago

1.2.27

1 year ago

1.2.25

1 year ago

1.2.26

1 year ago

3.7.68

12 months ago

3.7.69

12 months ago

3.7.66

12 months ago

3.7.67

12 months ago

3.7.64

12 months ago

3.7.65

12 months ago

3.11.96

11 months ago

4.14.110

10 months ago

3.12.100

11 months ago

4.16.117

10 months ago

4.16.118

10 months ago

4.16.119

10 months ago

3.11.91

11 months ago

3.11.90

11 months ago

3.11.93

11 months ago

3.11.92

11 months ago

3.11.95

11 months ago

3.11.94

11 months ago

3.7.63

1 year ago

4.14.108

10 months ago

3.9.82

11 months ago

2.7.59

1 year ago

4.14.109

10 months ago

2.7.58

1 year ago

4.16.116

10 months ago

3.9.81

12 months ago

1.1.14

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.13

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

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago