![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
indicative
Advanced tools
Indicative is a beautiful Laravel inspired validation library for Nodejs with powerful promises. A took out a day to write this library as i was not able to find a simple and easy to use library which is also extendable.
After using Laravel and ROR it was very frustating by not getting any powerful and promises based validation module for nodejs.
Time and water does not wait for anyone , nor does javascript. Javascript asynchronous nature is great in many ways but at times makes it harder to work with loops specially when you are doing heavy operations.
This is where promises comes into picture, unlike time and water when you make a promise with someone in real life, they will wait for you until you fulfill or break your promise. That what promises are in javascript.
Indicative returns and accept promises only, every time you validate or extend indicative validator it accepts and returns a promise and to do it makes use of https://www.npmjs.org/package/q .
It is very simple to use indicative
var validator = require('indicative');
validator.initialize();
var rules = {
'name' : 'required|alpha'
};
var values = {
'name': 'johny123'
};
validator.validate(rules,values).then(function(success){
console.log(success);
}).catch(function(err){
console.log(err);
}).done();
Expected output
{ name: [ { rule: 'alpha', message: 'name should be alpha only' } ] }
What you just did is
Above error message doesn't seems to be good , and can be more descriptive. Let's make it so.
var validator = require('indicative');
validator.initialize();
var rules = {
'name' : 'required|alpha'
};
var values = {
'name': 'johny123'
};
var messages = {
'alpha': 'Name should only contain letters , numbers and special characters are not allowed'
};
// re call the same method
validator.validate(rules,values,messages).then(function(success){
console.log(success);
}).catch(function(err){
console.log(err);
}).done();
Expected output
{ name: [ { rule: 'alpha', message: 'Name should only contain letters , numbers and special characters are not allowed' } ] }
Now you can see your custom message getting printed instead of a system generated one. But there is one problem , above error is not personalized enough as it contains a word called Name , which makes it non usable for other fields like username , lastname and so on.
Indicative also allows templating which means you can also access field name, values , rules and arguments inside your custom message , here is an example.
var messages = {
'alpha': '%field% should only contain letters , numbers and special characters are not allowed'
};
You can access following
You can also define messages for different fields.
var validator = require('indicative');
validator.initialize();
var rules = {
'name' : 'required|alpha',
'lastname': 'required|alpha'
};
var values = {
'name': 'johny123',
'lastname': 'english80'
};
var messages = {
'name':{
'alpha': '%field% should only contain letters , numbers and special characters are not allowed'
},
'lastname':{
'alpha': 'Hope your lastname is simple and does not have weird special characters or numbers'
}
};
validator.validate(rules,values,messages).then(function(success){
console.log(success);
}).catch(function(err){
console.log(err);
}).done();
Expected output
{ name:
[ { rule: 'alpha',
message: 'name should only contain letters , numbers and special characters are not allowed' } ],
lastname:
[ { rule: 'alpha',
message: 'Hope your lastname is simple and does not have weird special characters or numbers' } ] }
Cool so far right ? let's make it even better.
I have covered plenty of usual validation rules.
The field under validation must be present and should contain some value
The field under validation must be formatted as an email-address.
The field under validation must be after a given date. Example after:2014-10-20
The field under validation must be letters only.
The field under validation can contain letters, numbers for combination of both.
The field under validation must be a valid array.
The field under validation must be before a given date. Example after:2014-10-20
The field under validation must be a valid boolean, true
, false
, 0
and 1
is treated as valid
array.
The field under validation must be present and should not be emptfy if field
field is equal to value
.
The field under validation must be present and should not be empty if any of the other fields are present.
The field under validation must be present and should not be empty if all of the other fields are present.
The field under validation must be present and should not be empty if any of the other fields are not present.
The field under validation must be present and should not be empty if all of the other fields are not present.
The field under validation must watch the given field
The field must be in one of the given list of values.
The field must not be in one of the given list of values.
The field under validation must be a valid date, MM/DD/YYYY, MM-DD-YYYY, YYYY-MM-DD,YYYY/MM/DD date formats are supported by default.
The field under validation must be a valid date and should match specified date format.
The field under validation should be different from the given field
The field under validation should be equal to given value
The field under validation must be a valid url. http
, https
and ftp
is supported.
The field under validation must be a valid ip address.
The field under validation must be under defined range. Range must be integer or float.
The field under validation must be a valid integer.
The field under validation must be greater or equals to defined length
The field under validation must be less or equals to defined length
It is really easy to extend and add your own validation to indicative. Make sure your custom methods should return promises , otherwise a new error will be thrown.
var phone_no = function(value,args,message){
var deferred = Q.defer();
var phoneRe =/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/;
if(phoneRe.test(value)){
deferred.resolve();
}else{
deferred.reject(message);
}
return deferred.promise;
}
validator.extend('phone_no','Enter valid phone no',phone_no);
Let's discuss what happened this time
phone_no
which takes value, args and error message as parameters.phone_no
and passed our custom message.Here is the full example
var validator = require('../indicative');
var Q = require('q');
validator.initialize();
var rules = {
'username': 'required|alpha|min:4',
'email': 'required|email',
'contact_no': 'phone_no',
'gender': 'in:Male,Female,Other',
'age':'range:18,70',
'password':'required|min:4'
};
var values = {
'username': 'nu',
'contact_no': '1201abs',
'email': 'nu@example.com',
'gender': 'M',
'age': 22
};
var phone_no = function(value,args,message){
var deferred = Q.defer();
var phoneRe =/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/;
if(phoneRe.test(value)){
deferred.resolve();
}else{
deferred.reject(message);
}
return deferred.promise;
}
validator.extend('phone_no','Enter valid phone no',phone_no);
validator.validate(rules,values).then(function(success){
console.log(success);
}).catch(function(err){
console.log(err);
}).done();
You can access examples inside examples folder of this repo.
Contribution is always welcomed, fork this repo to get started.
FAQs
Concise data validation library for Node.js and browsers
The npm package indicative receives a total of 14,239 weekly downloads. As such, indicative popularity was classified as popular.
We found that indicative demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.