https://github.com/mattecapu/typs
An handy library for type validation in Javascript
https://github.com/mattecapu/typs
Last synced: 5 months ago
JSON representation
An handy library for type validation in Javascript
- Host: GitHub
- URL: https://github.com/mattecapu/typs
- Owner: mattecapu
- License: mit
- Created: 2014-11-09T14:15:39.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-09-06T06:21:40.000Z (almost 10 years ago)
- Last Synced: 2025-10-28T05:27:55.583Z (8 months ago)
- Language: JavaScript
- Size: 52.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```js
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
```js
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](https://github.com/mattecapu/typs/blob/master/LICENSE).