## Valichain
**Kind**: global class
* [Valichain](#Valichain)
* [new Valichain([input])](#new_Valichain_new)
* _instance_
* [.validate(input)](#Valichain+validate) ⇒ [Valichain](#Valichain)
* [.msg(message)](#Valichain+msg) ⇒ [Valichain](#Valichain)
* [.default(value, [checkFn])](#Valichain+default) ⇒ [Valichain](#Valichain)
* [.custom(fn, type, [name])](#Valichain+custom) ⇒ [Valichain](#Valichain)
* _static_
* [.$](#Valichain.$) : object
* [.isNilOrEmptyString(v)](#Valichain.$.isNilOrEmptyString) ⇒ boolean
* [.isNullOrEmptyString(v)](#Valichain.$.isNullOrEmptyString) ⇒ boolean
* [.isNotNilPrimitive(v)](#Valichain.$.isNotNilPrimitive) ⇒ boolean
* [.register(context, scope, prefix, fn, type)](#Valichain.register)
* [.validate(rules, data)](#Valichain.validate) ⇒ map
* [.extract(v)](#Valichain.extract) ⇒ map
### new Valichain([input])
A validator.
If input is given, creates a synchronous simple validator, which works once.
If input is not given, creates a deferred validator, which works multiple
times by calling its [validate](#Valichain+validate) method.
| Param | Description |
| --- | --- |
| [input] | the input value to be validated |
### valichain.validate(input) ⇒ [Valichain](#Valichain)
Validates some input against this validator instance.
The result may be checked using valid, value, fail and what fields.
**Kind**: instance method of [Valichain](#Valichain)
**Returns**: [Valichain](#Valichain) - this - the same validator instance.
| Param | Description |
| --- | --- |
| input | The input value to be validated. |
### valichain.msg(message) ⇒ [Valichain](#Valichain)
Defines the message for the last validation function.
It is put on what field if validation fails for such a validation function.
**Kind**: instance method of [Valichain](#Valichain)
**Returns**: [Valichain](#Valichain) - this - the same validator instance for chaining.
| Param | Type | Description |
| --- | --- | --- |
| message | string | the message. |
### valichain.default(value, [checkFn]) ⇒ [Valichain](#Valichain)
Defines the default value for a optional input value.
It is applied when the checkFn evaluates to true for the input value.
**Kind**: instance method of [Valichain](#Valichain)
**Returns**: [Valichain](#Valichain) - this - the same validator instance for chaining.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | \* | | the default value. |
| [checkFn] | function | {@link Valichain.$.isNilOrEmptyString} | the static function which evaluates to true if the input value is considered absent. |
### valichain.custom(fn, type, [name]) ⇒ [Valichain](#Valichain)
Defines a custom validation function for this instance of Valichain.
If you want a validation function which works for several instances,
consider register one using [register](#Valichain.register).
**Kind**: instance method of [Valichain](#Valichain)
**Returns**: [Valichain](#Valichain) - this - the same validator instance for chaining.
| Param | Type | Description |
| --- | --- | --- |
| fn | function | the function which receives an value of any type. The return value must be a boolean for validation functions, or any for sanitization functions. |
| type | string | "validator" or "sanitizer". |
| [name] | string | the function name. If not given, fn.name is used. If fn.name also is undefined uses an auto-generated name. |
### Valichain.$ : object
Valichain utility functions
**Kind**: static namespace of [Valichain](#Valichain)
**Summary**: Valichain utility functions
* [.$](#Valichain.$) : object
* [.isNilOrEmptyString(v)](#Valichain.$.isNilOrEmptyString) ⇒ boolean
* [.isNullOrEmptyString(v)](#Valichain.$.isNullOrEmptyString) ⇒ boolean
* [.isNotNilPrimitive(v)](#Valichain.$.isNotNilPrimitive) ⇒ boolean
#### $.isNilOrEmptyString(v) ⇒ boolean
Returns true if argument is null, undefined or empty string.
**Kind**: static method of [$](#Valichain.$)
**Returns**: boolean - true if nil or "", false otherwise.
| Param | Type | Description |
| --- | --- | --- |
| v | \* | the value to be tested |
#### $.isNullOrEmptyString(v) ⇒ boolean
Returns true if argument is null or empty string.
**Kind**: static method of [$](#Valichain.$)
**Returns**: boolean - true if null or "", false otherwise.
| Param | Type | Description |
| --- | --- | --- |
| v | \* | the value to be tested |
#### $.isNotNilPrimitive(v) ⇒ boolean
Returns true if argument is not nil primitive. A not nil primitive is
a boolean, number or string, but neither null, undefined nor object.
**Kind**: static method of [$](#Valichain.$)
**Returns**: boolean - true if boolean, number or string, false otherwise.
| Param | Type | Description |
| --- | --- | --- |
| v | \* | the value to be tested |
### Valichain.register(context, scope, prefix, fn, type)
Registers a function to be used by any new instance.
The registered function will be accessed as $
**Kind**: static method of [Valichain](#Valichain)
| Param | Type | Description |
| --- | --- | --- |
| context | \* | object which will be the this reference when fn is called. If null, the this object will the current value. |
| scope | string | a string which scope the function name. |
| prefix | string | function name prefix. |
| fn | function | the function wihch will be called. Should have a fn.name property. |
| type | string | "validator" or "sanitizer". |
### Valichain.validate(rules, data) ⇒ map
Validate a data map against a rules map.
Both have to match the keys and have no nested rules.
**Kind**: static method of [Valichain](#Valichain)
**Returns**: map - map containing a valid (boolean) field and a values (map) field.
| Param | Type | Description |
| --- | --- | --- |
| rules | object | The rules map. |
| data | object | The data set to be validated. |
### Valichain.extract(v) ⇒ map
Extracts and returns the final values of an validated rules map,
or null if the given input was not valid.
**Kind**: static method of [Valichain](#Valichain)
**Returns**: map - null if v is invalid, or a map with param names as keys and final values as values.
| Param | Type | Description |
| --- | --- | --- |
| v | object | object returned by [validate](#Valichain.validate). |