/egg-config-validator

A plugin to validate the config when we start the app

Primary LanguageJavaScriptMIT LicenseMIT

egg-config-validator

NPM version build status Test coverage David deps Known Vulnerabilities npm download

A plugin to check the config when the app started. If it found the config could not fit the requirement. It will throw error.

It check the config based on a json schema. You can provide a schema or a json which we will transfer into a schema.

Install

$ npm i egg-config-validator --save

Usage

// {app_root}/config/plugin.js
exports.configValidator = {
  enable: true,
  package: 'egg-config-validator',
};

Configuration

// {app_root}/config/config.default.js
exports.configValidator = {
  standard: Object | string,
  type: 'json' | 'jsonschema',
  showStandard: boolean,
};

see config/config.default.js for more detail.

property type meaning default limitation
standard Object | string The standard of the config, it can be an Object or a path string pointing to the standard. {}
type String We only support jsonschema and json currently. As they are totally the same, you should declare it. jsonschema
showStandard Boolean We will output the json schema in console.log to help you to debug. false
target Object | string We use app.config as target object. But you can also customize your config. You can pass in an Object or a path string pointing to the target file app.config
requiredProperties Boolean We will set every properties in standard as required properties if this is true. true Only work when type is JSON
additionalProperties Boolean | Object The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword. By default any additional properties are allowed. The additionalProperties keyword may be either a boolean or an object. If additionalProperties is a boolean and set to false, no additional properties will be allowed. If additionalProperties is an object, that object is a schema that will be used to validate any additional properties not listed in properties. true Only work when type is JSON

Example

standard can be a json.

exports.configValidator = {
  standard: {
    person: {
      firstName: 'hello ',
      lastName: 'world!',
      age: 33,
    },
  },
  type: 'json',
};

standard can be a json schema.

exports.configValidator = {
  standard: {
    title: 'config',
    type: 'object',
    properties: {
      person: {
        title: 'person',
        type: 'object',
        properties: {
          firstName: {
            type: 'string',
          },
          lastName: {
            type: 'string',
          },
          age: {
            description: 'Age in years',
            type: 'integer',
            minimum: 0,
          },
        },
        required: [ 'firstName', 'lastName' ],
      },
    },
    required: [ 'person' ],
  },
  type: 'jsonschema',
};

standard can be stored in file.

exports.configValidator = {
  standard: path.resolve(__dirname, './config.standard.js'),
  type: 'json',
};

Questions & Suggestions

Please open an issue here.

License

MIT