Letype
Type checker that uses native data types.
Letype is simple and small (1kB minified + gzipped) type checker library that can validate any JS data types and structures as well as any custom ones.
Features
- Uses native JS data types.
- Can validate type structures.
- Supports regex validation as a type.
- Supports custom types.
- Small in size.
Installation
To install the stable version:
npm install --save letype
This assumes you are using npm as your package manager.
If you're not, you can access these files on unpkg (letype.min.js
is the file you're probalby after), download them, or point your package manager to them.
Browser Compatibility
Letype.js currently is compatible with all modern browsers.
Example usage
; ; // -> true ; // -> false// "Type error: `1` is not of type `number`" ; // -> true ; // -> false// "Type error: `92942` is not of type `string` in `name`"// "Type error: `21` is not of type `number` in `age`"// "Type error: `role` is undefined! Required value of type `string`"// "Type error: `work` is defined as `null`! But it should not be defined at all!"
All available exports from package:
; const Any Or Undefined Custom = types;
List of available functions
Usage of functions
check()
It takes first argument as value that should be checked.
Second argument is type that the value should be checked against.
It returns boolean
(true
if valid, false
if invalid).
; // -> true; // -> false
assert()
It does exactly the same thing as check()
function, but with a little difference.
If validation fails it throws error.
; // -> true; // -> Throw
List of available types
Types are meant to be used as data types that does custom validation against given value inside assert()
or check()
functions.
letype
library:
Types from
Types from JavaScript language:
Types can also be created in structures:
Usage of types
types.Any
; // -> true; // -> true
types.Or()
; // -> true; // -> true; // -> false
types.Undefined
; // -> false; // -> true
types.Custom
Custom type is empty and does no checks against anything. It is meant for creating your own custom types.
To do that just extend Custom
class and define public parse
method that has one parameter - "value".
It is value to be checked/validated. parse
method should return boolean (true
if valid, false
if invalid).
For example lets create type that checks if value has first capital letter.
Custom { return value0 === value0; }
To use it simply pass it in any of assert()
or check()
functions.
; // -> true; // -> false
String
; // -> true; // -> false
Number
; // -> false; // -> true
Boolean
; // -> false; // -> true
Array
; // -> false; // -> true; // -> true
Function
; // -> false; // -> true
Date
; // -> false; // -> true
RegExp
; // -> false; // -> true
Typed Arrays
; // -> false; // -> false; // -> true
Typed Objects
; // -> false; // -> false; // -> true
Regular Expressions
; // -> false; // -> true
Motivation
We can get awesome type checking in JS with TS, but that only checks types in compile time.
So I wanted some kind of runtime type checking with types that are already available in JS - not using strings as a types. This feels more JS and more natural.
License
MIT licenced. Copyright © 2020-present, Marcis (Marcisbee) Bergmanis