see https://github.com/skaterdav85/validatorjs
npm i git://github.com/potentia-inc/validator.git#v1.2.xconst validator = require('validator')
const S = require('sanctuary')
const rule = { foo: 'required|string|between:3,10' }
validator (rule) ({ foo: 'abc' }) // => Right ({ foo: 'abc' })
validator (rule) ({ foo: 'a' }) // => Left ('...failure message.')
const v = validator(rule)(obj)
if (S.isLeft(v)) { console.error(v.value) /* validation failed */ }
if (S.isRight(v)) { console.log('passed!', v.value) /* validation passed */ }// validator :: Rule -> Object -> S.Either String ObjectRuleis an alias type for ordinary object.Objectis the type for input object.- When the validation is passed, the output will be a Either Right with value the same as input object.
- When the validation is failed, the output will be a Either Left with value a string with informative failure reason.
- Bitcoin:
rule = { address: `required|string|bitcoinAddress:${network}` } // network: 'mainnet' | 'testnet' - Etherem:
rule = { address: `required|string|ethereumAddress:${check}` } // check: 'checked' | 'unchecked' - Ripple:
rule = { address: `required|string|rippleAddress` }
- Ripple Tag:
rule = { tag: 'string|rippleTag' } - EOS Memo:
rule = { tag: 'string|eosMemo' }
-
Decimal Place:
rule = { amount: 'required|string|numeric|decimal:5' }{ amount: '0.0001' } // passed{ amount: '0.00001' } // passed{ amount: '0.000010000' } // passed{ amount: '0.000001' } // failed
-
Positivity:
rule = { amount: 'required|string|numeric|positive' }{ amount: '0.0001' } // passed{ amount: '+0.0001' } // passed{ amount: '-0.00001' } // failed
-
Printable:
rule = { client_id: 'required|string|isprint' }{ client_id: 'alice' } // passed{ client_id: '_bob' } // passed{ client_id: '!carol ' } // passed{ client_id: "dave\x01" } // failed
It is recommended to add a
stringrequirement to all rules when validating addresses, tags, amounts.