0.2.2 • Published 8 years ago

egg-config-validator v0.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

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.

propertytypemeaningdefaultlimitation
standardObject | stringThe standard of the config, it can be an Object or a path string pointing to the standard.{}
typeStringWe only support jsonschema and json currently. As they are totally the same, you should declare it.jsonschema
showStandardBooleanWe will output the json schema in console.log to help you to debug.false
targetObject | stringWe 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 fileapp.config
requiredPropertiesBooleanWe will set every properties in standard as required properties if this is true.trueOnly work when type is JSON
additionalPropertiesBoolean | ObjectThe 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.trueOnly 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