0.0.1-security.1 • Published 2 years ago

@patrtorg/sint-velit-maiores v0.0.1-security.1

Weekly downloads
-
License
-
Repository
-
Last release
2 years 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

0.0.1-security

2 years ago

0.0.1-security.0

2 years ago

0.0.1-security.1

2 years ago

4.15.116

2 years ago

3.13.101

2 years ago

4.15.115

2 years ago

3.13.100

2 years ago

2.7.62

2 years ago

2.7.61

2 years ago

2.7.60

2 years ago

1.6.42

2 years ago

1.6.44

2 years ago

1.6.43

2 years ago

1.6.46

2 years ago

1.6.45

2 years ago

1.6.48

2 years ago

1.6.47

2 years ago

2.7.63

2 years ago

3.8.81

2 years ago

1.4.31

2 years ago

1.4.30

2 years ago

1.4.33

2 years ago

1.4.32

2 years ago

1.4.34

2 years ago

1.2.16

2 years ago

1.2.17

2 years ago

1.2.15

2 years ago

4.13.103

2 years ago

4.13.104

2 years ago

4.13.101

2 years ago

4.13.102

2 years ago

4.13.107

2 years ago

4.13.108

2 years ago

4.13.105

2 years ago

4.13.106

2 years ago

3.12.98

2 years ago

3.12.97

2 years ago

3.12.99

2 years ago

3.12.96

2 years ago

1.3.30

2 years ago

1.1.15

2 years ago

3.10.86

2 years ago

3.10.87

2 years ago

3.10.84

2 years ago

3.10.85

2 years ago

3.10.82

2 years ago

2.6.48

2 years ago

3.10.83

2 years ago

2.6.49

2 years ago

3.7.79

2 years ago

2.6.50

2 years ago

3.7.77

2 years ago

3.7.78

2 years ago

3.7.75

2 years ago

3.7.76

2 years ago

4.14.115

2 years ago

4.14.111

2 years ago

4.14.112

2 years ago

4.14.113

2 years ago

1.5.34

2 years ago

4.14.114

2 years ago

3.7.73

2 years ago

2.6.55

2 years ago

1.5.36

2 years ago

3.7.74

2 years ago

2.6.56

2 years ago

1.5.35

2 years ago

3.7.71

2 years ago

2.6.57

2 years ago

1.5.38

2 years ago

3.7.72

2 years ago

2.6.58

2 years ago

1.5.37

2 years ago

2.6.51

2 years ago

3.7.70

2 years ago

2.6.52

2 years ago

1.5.39

2 years ago

2.6.53

2 years ago

2.6.54

2 years ago

1.5.41

2 years ago

1.5.40

2 years ago

1.5.42

2 years ago

3.7.80

2 years ago

1.3.28

2 years ago

3.7.81

2 years ago

1.3.29

2 years ago

1.3.27

2 years ago

1.2.18

2 years ago

1.2.19

2 years ago

3.11.88

2 years ago

3.11.87

2 years ago

3.11.89

2 years ago

1.2.20

2 years ago

4.16.120

2 years ago

1.2.23

2 years ago

1.2.24

2 years ago

1.2.21

2 years ago

1.2.22

2 years ago

1.2.27

2 years ago

1.2.25

2 years ago

1.2.26

2 years ago

3.7.68

2 years ago

3.7.69

2 years ago

3.7.66

2 years ago

3.7.67

2 years ago

3.7.64

2 years ago

3.7.65

2 years ago

3.11.96

2 years ago

4.14.110

2 years ago

3.12.100

2 years ago

4.16.117

2 years ago

4.16.118

2 years ago

4.16.119

2 years ago

3.11.91

2 years ago

3.11.90

2 years ago

3.11.93

2 years ago

3.11.92

2 years ago

3.11.95

2 years ago

3.11.94

2 years ago

3.7.63

2 years ago

4.14.108

2 years ago

3.9.82

2 years ago

2.7.59

2 years ago

4.14.109

2 years ago

2.7.58

2 years ago

4.16.116

2 years ago

3.9.81

2 years ago

1.1.14

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.13

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago