How to use the convict.addFormats function in convict

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 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 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 {

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