modifier

JavaScript validator / modifier

npm install modifier
2 downloads in the last week
3 downloads in the last month

modifier

description

JavaScript validator / modifier Available in modern browsers and Node.js.

API at a glance

require

var Modifier = require('modifier'); // in Node.js

make it integer

var num = Modifier.integer("10.3");
console.assert(num === 10); // to integer

make it number with conditions

var num2 = Modifier.number.where({ min: 10 })("10.3");
console.assert(num2 === 10.3); // to number, minimum 10

match?

var match = Modifier.regex.where({ pattern: /[a-z]/ })("shinout");
console.assert(match === "shinout"); // matched

error?

try {
  var int1 = Modifier.integer("xxx");
}
catch (e) {
  // get reason
  console.assert(e.reason === 'NaN');
  console.assert(e.val === 'xxx');
}

quiet

var str1 = Modifier.string.where({ quiet: true, min : 4 })("ABC");
console.assert(str1 === undefined);

strict

try {
  var str2 = Modifier.string.where({ strict: true })(1324);
}
catch (e) {
  console.assert(e.reason === 'notString');
  console.assert(e.val === 1324);
}

modifiers

name description example result
integer make values integer Modifier.integer.where({min: 0})("1.33") 1
number numberize values Modifier.integer.where({max: 10})("8.33") 8.33
string stringify values Modifier.integer.where({min: 4})("shinout") "shinout"
bool make it boolean Modifier.bool("shinout") true
array check if array or not Modifier.array.where({max: 4})([1, "foo", -3, 3]) [1, "foo", -3, 3]
regex check if given string match with the pattern Modifier.regex.where({pattern: /[a-z]+/})("foobar") [1, "foo", -3, 3]
func check if function or not Modifier.func(function(v) {}) function(v) {}
equal check equality Modifier.array.where({value: 45})("45") "45"
equal check equality (strict) Modifier.array.where({value: "45", strict: true})("45") "45"
isNull check if null or not Modifier.isNull(undefined) undefined
isUndefined check if undefined or not Modifier.isUndefined(null) null
oneof check the value is in the list Modifier.oneof.where({list: ["apple", "banana", "microsoft"]})("banana") "banana"

wheres

modifier key description example related error reason
(all types) quiet throw no errors fn = Modifier.integer.where({quiet: true})
integer min minimum allowable value fn = Modifier.integer.where({min: 10}) min
integer max maximum allowable value fn = Modifier.integer.where({max: 14}) min
integer strict type checking before cast fn = Modifier.integer.where({strict: true}) notNumber
number min minimum allowable value fn = Modifier.number.where({min: 10.33}) min
number max maximum allowable value fn = Modifier.number.where({max: 0.99}) min
number strict type checking before cast fn = Modifier.number.where({strict: true}) notNumber
string min minimum allowable length fn = Modifier.string.where({min: 7}) minLength
string max maximum allowable length fn = Modifier.string.where({max: 7}) maxLength
string strict type checking before cast fn = Modifier.string.where({strict: true}) notString
array min minimum allowable length fn = Modifier.array.where({min: 7}) minLength
array max maximum allowable length fn = Modifier.array.where({max: 7}) maxLength
bool zerostr make "0" false fn = Modifier.bool.where({zerostr: true}) -
bool strict type checking before cast fn = Modifier.bool.where({strict: true}) notBool
equal value the value to compare with fn = Modifier.equal.where({value: "xxx"}) notEqual
equal strict === fn = Modifier.equal.where({value: 1984, strict: true}) notStrictEqual
isNull strict undefined !== null fn = Modifier.isNull.where({strict: true}) notNull
isUndefined strict undefined !== null fn = Modifier.isUndefined.where({strict: true}) notUndefined
regex pattern pattern of the regular expression fn = Modifier.regex.where({pattern: /http(s):\/\//}) notMatch
oneof list list of the choices fn = Modifier.oneof.where({list: [1,2,3]}) notInList

error reasons

reason name from description example
notNumber number, integer type of the value is not number Modifier.number.where({strict: true})("123")
NaN number, integer the value is NaN after casting Modifier.number("xxx")
min number, integer the value is less than "min" value Modifier.number.where({min: 3})(2)
max number, integer the value is greater than "max" value Modifier.integer.where({max: 3})(5)
notString string type of the value is not string Modifier.string.where({strict: true})(1999)
minLength string, array the length of the value is less than "min" value Modifier.string.where({min : 5})("Shin")
maxLength string, array the length of the value is larger than "max" value Modifier.array.where({max : 2})(["A", "B", "C"])
notBool bool type of the value is not boolean Modifier.bool.where({strict: true})("TRUE")
notArray array type of the value is not array Modifier.array("aaaaaa")
notEqual equal the value is not equal to the given value Modifier.equal.where({value: "shinout"})("xxxxxxx")
notStrictEqual equal the value is not strictly equal to the given value Modifier.equal.where({value: 123})("123")
notNull isNull the value is not null Modifier.isNull("OK")
notUndefined isNull the value is not undefined Modifier.isUndefined("OK")
notMatch regex the value does not match the pattern Modifier.regex.where({pattern: /[a-zA-Z]+/})("shinout310")
notFunction func type of the value is not function Modifier.func("aaaa")
notInList oneof the value is not in the given list Modifier.oneof.where({list: [3,4,5]})(1)
noList oneof list is not given Modifier.oneof(1)

complex coditions

Modifier.some(fn1, fn2, ...)

Modifier.some(
  Modifier.string,
  Modifier.regex.where({pattern: /^get[A-Za-z0-9]+$/})
)('getConfig##')

{reason: "noneOf"} is thrown when none of the modifier match.

Modifier.every(fn1, fn2, ...)

Modifier.every(
  Modifier.string,
  Modifier.regex.where({pattern: /^get[A-Za-z0-9]+$/})
)('getConfig')

only Node.js

file

Modifier.file.where({normalize: true})("/home/shinout/node_modules/jsrel/jsrel.js")
  • normalize can be used to normalize the path.
  • notFile is the reason when it exists but not a file.
  • noSuchFileOrDirectory is the reason when no such file or directory.

dir

Modifier.dir.where({normalize: true})("/home/shinout/node_modules/jsrel/")
  • normalize can be used to normalize the path.
  • notDirectory is the reason when it exists but not a directory.
  • noSuchFileOrDirectory is the reason when no such file or directory.

path

Modifier.path.where({normalize: true})("/home/shinout/node_modules/jsrel/")
  • normalize can be used to normalize the path.
  • noSuchFileOrDirectory is the reason when no such file or directory.

shortcut

  • quiet can be used like
var fn = Modifier.integer.where({min: 0}).quiet // the same as {quiet: true}

var a = fn(-3); // undefined, throwing no error
  • strict can be used like
    var fn = Modifier.integer.where({min: 0}).strict.quiet
    
npm loves you