Lightweight runtime types for vanilla JS and Node
npm install vanillatypeLightweight run time types for vanilla JavaScript and Node.JS.
users.js:
``javascript
import {T} from 'vanillatype';
T.def('User', {
_id: TID,ID
_owner: T,Username
username: T,Email
email: T,Email
newEmail: T.maybe(T),Integer
salt: T,Hash
passwordHash: T,GroupArray
groups: T,String
stripeCustomerID: T,Boolean
verified: T.maybe(T)
});
export default function validate(user) {
const errors = T.errors(TUser, user);
validateUsernameUniqueness(user, errors);
return errors;
}
//...
`
types.js:
`javascript
import {T} from 'vanillatype';
// regexes
const UsernameRegExp = /^[a-zA-Z][a-zA-Z0-9]{4,16}$/
const EmailRegExp = /(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?\.)+a-z0-9?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;Boolean
const HexRegExp = /^[a-f0-9]{8,100}$/i;
// common types
T.defOr('MaybeBoolean', T, TNone);String
T.defOr('ID', T, TNumber);
T.def('URL', null, {
verify: i => {
try {
return new URL(i);
} catch(e) {
return false;
}
},
help: "A valid URL"
});
// user field types
T.defCollection('GroupArray', {
container: TArray,String
member: T
});
T.def('Username', null, {
verify: i => UsernameRegExp.test(i) && i.length < 200,
help:"Alphanumeric between 5 and 16 characters"
});
T.def('Email', null, {
verify: i => EmailRegExp.test(i) && i.length < 200,
help: "A valid email address"
});
T.def('Hash', null, {
verify: i => HexRegExp.test(i) && i.length < 200,
help: "A hexadecimal hash value, between 8 and 100 characters"
});
`
Taken from servedata/_schemas/users.js and servedata/types.js
- built-in support for built-in types!
- common patterns like collection, or, maybe and enum types
- fully optional
- simple literate syntax
- custom help fields to add context to type errors
- optional partial (subset) matching
js
function defineSpecials() {
T.def(Any, null, {verify: () => true});
T.def(Some, null, {verify: i => !isUnset(i)});
T.def(None, null, {verify: i => isUnset(i)});
T.def(Function, null, {verify: i => i instanceof Function});
T.def(Integer, null, {verify: i => Number.isInteger(i)});
T.def(Array, null, {verify: i => Array.isArray(i)});
T.def(Iterable, null, {verify: i => i[Symbol.iterator] instanceof Function});
}
`Another reason I like this
Apart from writing it myself to suit my own work style, which is a great reason to like something, I like this because, if I want to change the syntax, or add some new feature that I want, it's really easy to change the library, which is only a couple hundred lines of code. If I wanted the same results from a third-party library, I'd have to wait.
More examples
For more comprehensive examples see vanillatype's test file.
getting and incorporating
You can use the template repo or import using the old name on npm (vanillatype wasn't available to me, it is now).
We use ES modules.
You can use in your client side code like:
`JavaScript
import {T} from 'https://unpkg.com/jtype-system/t.js';
`While the tests are currently only written for client side, you can use in Node.js like so:
`shell
$ npm i --save vanillatype
`or using the old name
`shell
$ npm i --save jtype-system
`Then:
`JavaScript
import {T} from 'jtype-system';
``But you'll need to be using ESM or ES Modules.
-------------
VanillaType!