Simple validator, but with an fluent interface
- Validate and assign in an fluent way
- casting numeric values directly to
number
- additional static interface wich returns boolean
npm install another-fluent-validator
const FluentValidator = require('another-fluent-validator')
// example object
const myDirtyObject = {
code: 'ABC'
name: '',
value: '123'
}
let code = new FluentValidator(myDirtyObject.code, 'my code')
.isString()
.isNotEmpty()
.value
// -> wil set code to 'ABC'
let name = new FluentValidator(myDirtyObject.name, 'my name')
.isString()
.isNotEmpty()
.hasMinimumLength(3)
.value
// -> will throw Error with message: "my name should not be empty"
let value = new FluentValidator(myDirtyObject.value, 'my value')
.toInteger()
// -> checks if "isNumeric" and casts to number (value = 123)
}
Cant be use as pre-check (e.g. only validate when defined)
FluentValidator.isArray('abc') // -> returns false
https://github.com/standard/standard
Run mocha tests:
npm test
Check code coverage (creates "./coverage/index.html"):
npm run-script cover