a validation lib for browser environment only. https://www.npmjs.com/package/hagim
It has been a long time since I have needed to find an easy to use javascript validation lib for browser environments. Every lib I found on github is not suitable for me. So finally I decided to make a new wheel.
The most significant different between Node.js
environment and browser environment is that in the latter, numbers may be represented by a string variable(e.g: if using a Two-Way Data Binding framework). So, validating a number in the browser may be more difficult than in Node.js.
Another different is that there are more logic disjunction cases in browser environment.
To install via npm, run:
npm install hagim
To load hagim in node.js:
const Hagim = require('hagim');
let hagim = new Hagim(rules)
hagim.validate("value")
It is the basic form of usage of Hagim
.
The rules should be an array, e.g:
[{is: "number"}, {is: "integer"}]
Number
andString
can be judged by the same API- multiple rules can be gathered togather to form a complex judgement
- rules may be treated in a
and
behavior by default, but you can change this by adding a additionaltrue
literal in the rules array - rules can be nested to form a complex judgement
The elements of the array consists of a validator and an operand. e.g is
is the validator and "number"
is the operand.
Each rule will be performed one by one, follow the order of they are in the array. If one rule fails, by default the whole process will be failed.
let hagim = new Hagim([{is: "empty"}, {is: "number"}, {is: "email"}, true])
hagim.validate("") // true
hagim.validate("123.4") // true
hagim.validate("fatus@sky.com") // true
The element itself can be an array too, e.g:
let rules = new Hagim([{is: "empty"}, [{is: "number"}, {is: "integer"}]])
Rules are organized in an array, and every rule contains a validator and an operand. Operands can be an element
or an entity
or any kinds of data.
const Hagim = require('hagim');
new Hagim([{is: "number"}, {is: "integer"}]).validate(2) // true
whether the string represent a special string or number(more details on entities section)
the opposite of is
whether all the members of a string belong to a certain character set(more details on elements section)
whether the string match a certain regular express. Under the hood the function re.test()
is used
whether the string includes a certain kind of characters(more details on elements section)
the same as exist
whether the string contains a certain substring
whether the string is not empty
whether it is greater than the operand
whether it is lower than the operand
whether it is greater than or equal to the operand
whether it is lower than or equal to the operand
whether it equals the operand, under the hood ==
is used for comparing
whether the digits of decimal part is greater than the operand
whether the digits of decimal part is Lower than the operand
whether the digits of decimal part equals the operand
whether the length of the string is greater than the operand
whether the length of the string is lower than the operand
whether the length of the string equsls the operand
whether the string begin with a certain word
whether the string don't begin with a certain word
whether the first member of the string belongs to a certain kind of characters(more details on elements section)
whether the first member of the string does not
belong to a certain kind of characters(more details on elements section)
Each entity represents a certain kind of strings, like a number
or an email
. is
and isNot
can be used to judge whether the string represents a certain entity.
let hagim = new Hagim([{is: "number"}])
hagim.validate("2") // true
If there is no such a entity match you need, you can extend this lib by yourself, check plugin section for details.
whether the string represent a number
whether the string represent an integer
whether the string represent a decimal number
whether the string represent a positive number
whether the string represent a negative number
whether the string represents a email
whether the string is ""
, null
, undefined
or []
whether the string represent an ip address
whether the string represent a url
Each element represents a certain character set, like Latin letters or digits. are
can be used to judge whether all of the members of a string belong to a certain set.
whether the character belongs to latin letter set
currently the same to latin
whether the character belongs to 0-9
By default the rules in a array will be treated in an and
-like manner, but it depends on you whether to change it. You can set it by appending an additional truthy value into the rules array. e.g:
new Hagim([{is: "number"}, {is: "empty"}, true]).validate("2.3") //true
new Hagim([{is: "number"}, {is: "empty"}, true]).validate("") //true
You can specify a disjunction manner in every array, includes nested arrays, or leave it in a conjunction manner by default.
Third-party plugins are available by means of the extension API. Currently only entities
and elements
can be extended.
For instance, if you want to define a plugin which extends Hagim to have a capability to judge whether the operand is a binary number, it should be like this:
Hagim.extend("entities", {
binary: funciton(value){
return /[01]+/.test(value)
}
})