checktypes-js
checktypes-js is a package to help you check the types of a passed variable. It also allows you to check types of an object using an object scheme or an Array and types of elements within the Array. This package also has typescript support.
Installation
$ npm install checktypes-js --save
Introduction
Sections
Typescript Support
Use import { CheckTypesError } from 'checktype-js
for typings of the error objects. Use the package with maximum type checking advatage.. transpile time and run time type checking. What else could you ask for?
Simple Example
const checkTypes = const aNumber = 33 const error numberPassed = iferror consoleelse // do stuff eith numberPassed
Callback or return style usage
const checkTypes = /* Callback function style */ // OR /* Return value style */const error passedItem = iferror // handle error else // do stuff with passedItem
Usage
Object with Scheme type checking
You can define a scheme with types and can be nested with other Objects or Arrays.
const checkTypes = const personObject = name: "Shelby" age: 32 isMarried: true children: name: "Hannah" age: 3 name: "Billy" age: 6 address: street: number: "123" // <-- ERROR! String instead of Number name: "Main st." city: "Boston" state: "MA" country: "USA" comments: "is very nice." "has a good head on her shoulders" const personScheme = name: String age: Number isMarried: Boolean children: name: String age: Number address: street: number: Number name: String city: String state: String country: String comments: String
Simple Object with no properties's type checked
const checkTypes = const objectToCheck = username: 'hannah1010' password: 'securePass!' comments: 'Hey beautiful' 'You\'re a great human being!'
Required properties in scheme:
You can also specify required fields using $required : [{String name(s) of properties}]
const checkTypes = const scheme = username: String password: String rememberMe: Boolean $required: 'username' 'password' 'rememberMe' const objectToCheck = username: 'hannah1010' password: 'securePass!' // ERROR! missing 'rememberMe' property
Require all properties in scheme:
You also have the ability to specify all scheme properties as required. Import $required
from package.
const checkTypes = const $required = checkTypes const scheme = username: String password: String rememberMe: Boolean $required // just pass $required const objectToCheck = password: 'securePass!' rememberMe: false // ERROR! missing 'username' property
Array type checking
const checkTypes = const errors returnedArray = // can also pass [] if errors // handle errors else // do stuff with returnedArray
Array with element type checking
const checkTypes = const errors passedArray = if errors console /* [ { propertyName: '[1]', expectedType: 'Number', recievedType: 'String', required: false } ] */ else // do stuff with passedArray
String, Number, Boolean or Symbol type checking
const checkTypes = const someString = 55 const error passedItem = if error console /* [ { propertyName: '' expectedType: 'String', recievedType: 'Number', required: true } ] */ else // do stuff with passedItem
When type checking primitive types or when an Object or Array type mismatches at the parent/root level, the error object will have an empty string ''
for the propertyName as it's not a property where the error occured. Also, the parent/root level is always required. You wouldn't be type checking if you didn't need it ;)
Helper Functions
This library provides a few helper functions to help you come up with innovative ways to handle errors. User your imagination!
getVal()
Use this helper function to get a value within the passed Object or Array using propertyName String.
example:
const checkTypes = const getVal = checkTypes const scheme = request: headers: name: String value: String type: String body: String statusCode: Number const requestObject = requestDetails: headers: name: "Authentication" value: "Bearer ..." name: "Content-Type" value: "application/json" name: "Cache-Control" value: 0 // ERROR! value should be String type type: "POST" body: "..." statusCode: 200
You can use the stepBack
argument to step X properties back. So if you want to access the whole requestDetails
object in above example, stepBack
using stepBack
set to 3
. If you set stepBack farther than the number of properties that exist, function will throw an Error.
setVal()
Use this helper function to set a new value within the passed Object or Array using propertyName String.
example:
const checkTypes = const getVal setVal = checkTypes const scheme = request: headers: name: String value: String type: String body: String statusCode: Number const requestObject = requestDetails: headers: name: "Authentication" value: "Bearer ..." name: "Content-Type" value: "application/json" name: "Cache-Control" value: 0 // ERROR! value should be String type type: "POST" body: "..." statusCode: 200
You can use stepBack
argument 4th argument as a way to set value to the encompassing Object or Array. It is useful for cases as such:
const checkTypes = const getVal setVal = checkTypes const saleScheme = order: number: Number items: String customer: name: String phone: String address: street: String city: String state: String zipCode: String country: String shipment: postmarkId: Number departed: Number service: String const aSale = order: number: 3453434 items: "purse" "ring" "jacket" customer: name: "Hannah Medcraft" phone: "214-000-3443" address: street: "123 Main St" city: "Dallas" state: "TX" zipCode: 72034 country: "USA" shipment: postmarkId: 3423452456675 departed: 15604955496 service: "UPS"
getType()
Use this helper function to get the type of an item passed to it.
example:
const checkTypes = const getType = checkTypes const type = console// 'Object' // OR const type = console// 'Number'
typeToString()
Use this helper function to convert a type to a string.
example:
const checkTypes = const typeToString = checkTypes const typeAsString = console// 'Array'
stringToType()
Use this helper function to convert a string type to a Javascript type.
example:
const checkTypes = const stringToType = checkTypes const returnedType = console// true
propsFromString()
Use this helper function to convert a string type to a Javascript type.
example:
const checkTypes = const propsFromString = checkTypes const anAccessorString = "order[1]address.zipCode"const propertiesArray = console// ['order', '1', 'address', 'zipCode']
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
Support
Please open ticket, no email.