rich-param
An object with name and value which accepts pluggable methods as formatters or validators.
Install
npm install rich-param
Usage
Basic
// new Param(name, value, options)const param = 'firstName' 'Jessica' uppercase: true console // firstNameconsole // JESSICA
Setting and getting value
console // PRISCILA
Setting and getting options
console // trueconsole // false console // prisciLa
Formatting the value
param console // firstName: PRISCILA
Validating the value
param paramvalue'Jessica'console // true paramvalue'Priscila'console // false // or get the error objectparam
Validating using the built-in validators
paramparamvalue'' console // false // or get the error objectparam
Creating a custom formatter
param console // Jessica param console // JESSICA!
Creating a custom validator
param param console // Jessicaconsole // true param console // false param console // JESSICA!console // true
Reference
List of default options
Option | Type | Default value | Description |
---|---|---|---|
default | Mixed | Default param value | |
normalize | Boolean | false |
Formats Hey, jÉssiCa! to hey jessica |
lowercase | Boolean | false |
Formats HeLLo! to hello! |
uppercase | Boolean | false |
Formats HeLLo! to HELLO! |
trim | Boolean | true |
Formats HeLLo ! to HeLLo ! |
required | Boolean | false |
Invalidates empty values |
min | Number | Invalidates values lower than it | |
max | Number | Invalidates values greater than it | |
minlength | Number | Invalidates values with length lower than it | |
maxlength | Number | Invalidates values with length greater than it | |
enum | Array | Invalidates values which isn't in it | |
match | RegExp | Invalidates values which doesn't pass the RegExp | |
multiple | Boolean | false |
Make the value an array |
separator | String | , |
String to be found in the value to split into an array (if multiple = true) |
format | Function | (value, param) => value |
Function to be called with param.value() |
validate | Function | (value, param) => ({ valid: true }) |
Function to be called with param.validate() |
License
MIT © Diego Haz