## Classes
JsonConfigThe class of Validator
ElementParsedResultObject.<string, SchemaElement>JsonConfig](#JsonConfig)
**Kind**: global class
**Extends**: [JsonConfig](#JsonConfig)
* [ConsulConfigSync](#ConsulConfigSync) ⇐ [JsonConfig](#JsonConfig)
* [new ConsulConfigSync(option)](#new_ConsulConfigSync_new)
* [.params](#JsonConfig+params)
* [.getValue(key)](#JsonConfig+getValue) ⇒ \*
### new ConsulConfigSync(option)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| option | Object | | |
| option.consulAddr | String | | The consul address, in the format of `ip:port`. |
| option.pathPrefix | String | | The prefix of consul key element. |
| option.schema | [Schema](#Schema) | | The schema object. |
| [option.waitTimeBeforeThrowErrorMs] | Number | 0 | The wait time before trigger error when the value returned is not expected. The default value is `0`, but it will throw the error in async way even if you set the parameter to `0`. |
| [option.timeout4RequestConsulMs] | Number | 5000 | The timeout milliseconds for consul request. |
| [option.retryLimit] | Number | 0 | The retry times before getting data from consul failed. The default is `0`, which means no limit. |
| [option.consulOption] | [ConsulOption](#ConsulOption) | {} | The consul option used to init a consul client. |
### consulConfigSync.params
Get All the value.
**Kind**: instance property of [ConsulConfigSync](#ConsulConfigSync)
**Overrides**: [params](#JsonConfig+params)
### consulConfigSync.getValue(key) ⇒ \*
Get the value via key name.
**Kind**: instance method of [ConsulConfigSync](#ConsulConfigSync)
**Overrides**: [getValue](#JsonConfig+getValue)
| Param | Type |
| --- | --- |
| key | String |
## JsonConfig
**Kind**: global class
* [JsonConfig](#JsonConfig)
* [new JsonConfig(option)](#new_JsonConfig_new)
* [.params](#JsonConfig+params)
* [.getValue(key)](#JsonConfig+getValue) ⇒ \*
### new JsonConfig(option)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| option | Object | | |
| option.configObject | Object | | The Json object. |
| option.schema | [Schema](#Schema) | | The schema object. |
| [option.waitTimeBeforeThrowErrorMs] | Number | 0 | The wait time before trigger error when the value returned is not expected. The default value is `0`, but it will throw the error in async way even if you set the parameter to `0`. |
### jsonConfig.params
Get All the value.
**Kind**: instance property of [JsonConfig](#JsonConfig)
### jsonConfig.getValue(key) ⇒ \*
Get the value via key name.
**Kind**: instance method of [JsonConfig](#JsonConfig)
| Param | Type |
| --- | --- |
| key | String |
## Parser
The class of Validator
**Kind**: global class
* [Parser](#Parser)
* [new Parser(option)](#new_Parser_new)
* [.doParseElement(option)](#Parser+doParseElement) ⇒ [ElementParsedResult](#ElementParsedResult)
* [.doParse(params)](#Parser+doParse) ⇒ Object
### new Parser(option)
Creates an instance of Validator.
| Param | Type |
| --- | --- |
| option | Object |
| option.schema | [Schema](#Schema) |
### parser.doParseElement(option) ⇒ [ElementParsedResult](#ElementParsedResult)
Parse one config.
**Kind**: instance method of [Parser](#Parser)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| option | Object | | |
| option.key | String | | |
| option.value | \* | | |
| [option.isFromWatch] | Boolean | false | |
| [option.oldValue] | \* | | pass to when isFromWatch is true |
### parser.doParse(params) ⇒ Object
Parse all the config.
**Kind**: instance method of [Parser](#Parser)
| Param | Type |
| --- | --- |
| params | Object |
## CustomParseFunction(option) ⇒ [ElementParsedResult](#ElementParsedResult)
**Kind**: global function
**Returns**: [ElementParsedResult](#ElementParsedResult) - the validate result.
| Param | Type | Description |
| --- | --- | --- |
| option | Object | |
| option.key | String | the key |
| option.value | \* | the value you want to validated |
| option.schemaElement | [SchemaElement](#SchemaElement) | the element of schema. |
## AfterParseFunction(newValue, isFromWatch)
**Kind**: global function
| Param | Type |
| --- | --- |
| newValue | \* |
| isFromWatch | Boolean |
## WatchFunction(error, key, newValue)
**Kind**: global function
| Param | Type |
| --- | --- |
| error | Error \| String |
| key | String |
| newValue | \* |
## getConfigFromConsulSync(option)
**Kind**: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| option | Object | | |
| option.consulAddr | String | | The consul address, in the format of `ip:port`. |
| option.pathPrefix | String | | The prefix of consul key element. |
| option.savePath | String | | The full path name of the generated config file. |
| option.keys | Array.<String> | | The consul keys' name. |
| [option.timeoutMs] | Number | 5000 | The timeout milliseconds for consul request. |
| [option.retryLimit] | Number | 0 | The retry times before getting data from consul failed. The default is `0`, which means no limit. |
| [option.consulOption] | [ConsulOption](#ConsulOption) | {} | The consul option used to init a consul client. |
## ConsulOption
**Kind**: global typedef
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [secure] | Boolean | false | enable HTTPS |
| [ca] | Array.<String> | | array of strings or Buffers of trusted certificates in PEM format |
| [defaults] | Object | | common method call options that will be included with every call (ex: set default token) |
## SchemaElement
**Kind**: global typedef
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [preferred] | \* \| function | | when it not empty , it will been used instead of the value get from consul or json object. It can be an function or a value of type of String/Number/Object etc. |
| [required] | Boolean \| Array | false | indicate whether the current field necessary |
| type | Number \| JSON \| Date \| String \| Object | | declare the current field's type, it can be `Number` `JSON` `String` `Parser.TYPE_FILE` `Parser.TYPE_DIRECTORY` `Parser.TYPE_URL` or an Object with properties. see |
| [custom] | [CustomParseFunction](#CustomParseFunction) | | the custom validate function |
| [options] | Object | | the extended options used to check validate. |
| [afterParse] | [AfterParseFunction](#AfterParseFunction) | | the function to do after parse. |
| [watch] | [WatchFunction](#WatchFunction) | | the function used in consul watch callback |
| [isWatchDisabled] | Boolean | false | whether disable watching the key's changes, default is false. |
## Schema : Object.<string, SchemaElement>
**Kind**: global typedef
**Example**
```javascript
{
numberFiled : {
required:true,
type : Number,
options: {
gte: 10,
lte: 1
},
afterParse: function(value) {}
}
}
```
**Example**
```javascript
{
dateField:{
required:[true,'the dateField can not be empty']
type:[Date,'the dataField must be a Date']
}
}
```
**Example**
```javascript
{
objectField: {
type: {
field1: {type: [Number, 'field must be a Number']},
field2: {type: String, required: true}
}
}
}
```
## ElementParsedResult
**Kind**: global typedef
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [error] | String \| Error | | when the value is invalid, the error is not empty. |
| value | \* | | when the value is valid, return the value , it may be transfromed to the type you want. |
| changed | Boolean | | whether the value is changed, it may be true when the key changed from consul. |