How to use convict - 10 common examples

To help you get started, we’ve selected a few convict examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github philhawthorne / ha-dockermon / default_settings.js View on Github external
},
    host: {
      doc: 'Optional. URL to remote docker host',
      format: String,
      default: undefined
    },
    port: {
      doc: 'Optional. Port on remote docker host',
      format: 'port_or_windows_named_pipe',
      default: undefined
    }
  }
});

// Load environment dependent configuration
convict.addParser({ extension: ['yml', 'yaml'], parse: yaml.safeLoad });
var config_dir = process.env.config_dir || "./config";
console.log("Loading settings from " + config_dir);

//Check if the configuration file exists, if it doesn't then skip this
try{
  if (fs.existsSync(config_dir + '/configuration.yaml')) {
    config.loadFile(config_dir + '/configuration.yaml');
  }
} catch(err) {
  console.warn("No configuration file detected. Using default settings");
}

// Perform validation
config.validate({allowed: 'warn'});

module.exports = config;
github mozilla / fxa / packages / fxa-support-panel / config / index.ts View on Github external
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import convict from 'convict';
import fs from 'fs';
import path from 'path';

const conf = convict({
  authHeader: {
    default: 'oidc-claim-id-token-email',
    doc: 'Authentication header that should be logged for the user',
    env: 'AUTH_HEADER',
    format: String,
  },
  authdbUrl: {
    default: 'http://localhost:8000',
    doc: 'fxa-auth-db-mysql url',
    env: 'AUTHDB_URL',
    format: String,
  },
  env: {
    default: 'production',
    doc: 'The current node.js environment',
    env: 'NODE_ENV',
github terascope / teraslice / packages / terafoundation / src / validate-configs.ts View on Github external
function validateConfig(
    cluster: { isMaster: boolean },
    schema: convict.Schema,
    namespaceConfig: any
) {
    try {
        const config = convict(schema || {});
        config.load(namespaceConfig);

        if (cluster.isMaster) {
            config.validate({
                // IMPORTANT: changing this will break things
                // FIXME
                // false is deprecated and will be removed in ^5.0.0
                // must be warn or strict
                allowed: true,
            } as any);
        }

        return config.getProperties();
    } catch (err) {
        throw new TSError(err, { reason: 'Error validating configuration' });
    }
github jeromemacias / node-rob-config / src / index.js View on Github external
if (statFile.isFile()) {
        convict.addFormats(require(formatsFile));
    }
} catch (e) {}

// check and load convict schema
const schemaFile = path.resolve(configDir, 'schema.js');
try {
    const statFile = fs.statSync(schemaFile);
    if (!statFile.isFile()) {
        throw new Error('Not a file');
    }
} catch (e) {
    throw new Error(`[rob-config] Schema file "${e.path}" does not exists.`);
}
const conf = convict(require(schemaFile));

// check and load related environment config
const env = process.env.NODE_ENV || 'development';
const configFile = path.resolve(configDir, env + '.js');
try {
    const statFile = fs.statSync(configFile);
    if (!statFile.isFile()) {
        throw new Error('Not a file');
    }
} catch (e) {
    throw new Error(`[rob-config] Config file "${e.path}" does not exists.`);
}
const envConf = require(configFile);
conf.load(envConf);

// export utility methods and current config properties
github coralproject / talk / src / core / common / config.ts View on Github external
import convict from "convict";
import Joi from "joi";

// Add custom format for the mongo uri scheme.
convict.addFormat({
  name: "mongo-uri",
  validate: (url: string) => {
    Joi.assert(
      url,
      Joi.string().uri({
        scheme: ["mongodb"],
      })
    );
  },
});

// Add custom format for the redis uri scheme.
convict.addFormat({
  name: "redis-uri",
  validate: (url: string) => {
    Joi.assert(
github coralproject / talk / src / core / server / config.ts View on Github external
// Add custom format for the redis uri scheme.
convict.addFormat({
  name: "redis-uri",
  validate: (url: string) => {
    Joi.assert(
      url,
      Joi.string().uri({
        scheme: ["redis"],
      })
    );
  },
});

// Add a custom format for the optional-url that includes a trailing slash.
convict.addFormat({
  name: "optional-url",
  validate: (url: string) => {
    if (url) {
      Joi.assert(url, Joi.string().uri());
    }
  },
  // Ensure that there is an ending slash.
  coerce: (url: string) => (url ? ensureEndSlash(url) : url),
});

// Add a custom format that is a duration parsed with `ms` to used instead of
// the default format which will use `moment`.
convict.addFormat({
  name: "ms",
  validate: (val: number) => {
    Joi.assert(val, Joi.number().min(0));
github jeromemacias / node-rob-config / src / index.js View on Github external
const configDir = process.env.ROB_CONFIG_DIR || 'config';
try {
    const statDir = fs.statSync(configDir);
    if (!statDir.isDirectory()) {
        throw new Error('Not a directory');
    }
} catch (e) {
    throw new Error(`[rob-config] Config directory "${e.path}" does not exists.`);
}

// check and load convict formats if availables
const formatsFile = path.resolve(configDir, 'formats.js');
try {
    const statFile = fs.statSync(formatsFile);
    if (statFile.isFile()) {
        convict.addFormats(require(formatsFile));
    }
} catch (e) {}

// check and load convict schema
const schemaFile = path.resolve(configDir, 'schema.js');
try {
    const statFile = fs.statSync(schemaFile);
    if (!statFile.isFile()) {
        throw new Error('Not a file');
    }
} catch (e) {
    throw new Error(`[rob-config] Schema file "${e.path}" does not exists.`);
}
const conf = convict(require(schemaFile));

// check and load related environment config
github ThauEx / ffrk-proxy / lib / config.js View on Github external
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const convict = require('convict');
convict.addParser({extension: ['yml', 'yaml'], parse: yaml.safeLoad});

const config = convict({
  application: {
    config: {
      doc: 'name of the config to load',
      format: function check(val) {
        if (!/^.+$/.test(val)) {
          throw new Error('must be a string or number');
        }
      },
      default: 'default',
      env: 'CONFIG',
      arg: 'config',
    },
    debug: {
      doc: 'enable debug',
github bholloway / resolve-url-loader / packages / sourcemap-to-string / lib / conviction.js View on Github external
module.exports = (config) => {
  const {create, formats} = createFormatCollection();

  visitConfig({
    default: (v, node) => {
      const result = isObjectHash(v) && v.default;
      node.isRequired = (result === null);
      node.isOptional = (result !== null);
      return result;
    },
    format: v => (typeof v === 'string') ? v : create([].concat(v))
  })(config);

  convict.addFormats(formats);

  const instance = convict(config);

  try {
    instance.validate({allowed: 'strict'});
  } catch ({message}) {
    instance.set(
      'errors',
      message
        .split('\n')
        .map(v => v.split(':').map(vv => vv.trim()))
        .reduce((r, [k, v]) => Object.assign(r, {[k]: v}), {})
    );
  }

  return {
github smooth-code / bundle-analyzer / apps / server / src / config.js View on Github external
import path from 'path'
import convict from 'convict'
import url from 'url'

const workers = 3
const maxConnectionsAllowed = 20
const freeConnectionsForThirdTools = 2

const config = convict({
  env: {
    doc: 'The application environment',
    format: ['production', 'development', 'test'],
    default: 'development',
    env: 'NODE_ENV',
  },
  appBaseUrl: {
    doc: 'Base URL',
    format: String,
    default: 'http://localhost:8080',
    env: 'APP_BASE_URL',
  },
  server: {
    port: {
      doc: 'The server port number',
      format: 'port',

convict

Featureful configuration management library for Node.js (nested structure, schema validation, etc.)

Apache-2.0
Latest version published 2 years ago

Package Health Score

59 / 100
Full package analysis