3.0.0 • Published 8 years ago
typs v3.0.0
typs
An handy library for type validation in Javascript
Just
npm install typs --save
it.
The library is still widely open to changes, so feel free to propose new features or open an issue.
What can I use it for?
Param checking
Write a sound REST API or provide strong code constraints validating your parameters
app.get('/api/v1/users/:user_id', (req) => {
var user_id = req.params.user_id;
if(typs(user_id).integer().positive().notZero()) {
throw new Error('user_id is not a valid identifier');
}
// ...
});
Input validation
Let typs handle your validation logic in an easy and semantic way
const usernameType = typs().string().notEmpty().len({min: 5, max: 15});
const uniqueUsernameType = usernameType.satisifies((username) =>
// yes, typs supports promises!
query('SELECT * FROM users WHERE username = ?', username)
.then((rows) => rows.length < 1);
);
const passwordType = typs().string().notEmpty().len({min: 8, max: 25});
const emailType = typs().string().notEmpty().regex(/* ...a long mail regex... */);
function add_user(username, password, email) {
if(typs(username).isnt(usernameType)) {
throw new Error('the username you provided is not a valid username')
}
if(typs(password).isnt(passwordType)) {
throw new Error('the password you provided is not valid');
}
if(typs(email).isnt(emailType)) {
throw new Error('the e-mail you provided is not valid');
}
typs(username).isnt(uniqueUsernameType).then((pass) => {
if (!pass) throw new Error('this username already exists, please pick another');
// ...
});
}
License
Typs comes with a good, ol' MIT license.
3.0.0
8 years ago
2.0.0
8 years ago
0.4.0
8 years ago
0.3.8
8 years ago
0.3.7
9 years ago
0.3.6
9 years ago
0.3.5
9 years ago
0.3.4
9 years ago
0.3.3
9 years ago
0.3.2
9 years ago
0.3.1
9 years ago
0.3.0
9 years ago
0.2.9
9 years ago
0.2.8
9 years ago
0.2.7
9 years ago
0.2.6
9 years ago
0.2.5
9 years ago
0.2.4
9 years ago
0.2.3
9 years ago
0.2.2
9 years ago
0.2.1
9 years ago
0.1.0
9 years ago