check-types-mini

Check the types of your options object's values after user has customised them

Table of Contents
Install
$ npm i -S check-types-mini
Idea
check-types
is good but it's too big. All I need is to throw
if somebody sets my input settings to a wrong type.
I had a working prototype of this library ages ago. Pieces of it were finding a new life (and evolving) in every new non-trivial library I created. Then I got fed up with chasing 100% code coverage each time, duplicating unit tests and decided to split it into a standalone library.
The point of check-types-mini
is to save your time creating new libraries. Every library that has options object will need some type checks if you let user tinker with it.
API
checkTypes(obj, ref[, msg, optsVarName, opts])
As a result, it throws TypeError
s for you, containing your custom message, similar to:
check-types-mini/checkTypes(): opts.mode was customised to "zzz" which is not Boolean but string
Input argument | Type | Obligatory? | Description |
---|
obj | Plain object | yes | Options object after user's customisation |
ref | Plain object | yes | Default options - used to compare the types |
msg | String | no | A message to show. I like to include the name of the calling library, parent function and numeric throw ID. |
optsVarName | String | no | How is your options variable called? It does not matter much, but it's nicer to keep references consistent with your API documentation. |
opts | Plain object | no | Optional options go here. |
Options object
options object's key | Type | Obligatory? | Default | Description |
---|
{ | | | | |
ignoreKeys | Array or String | no | [] (empty array) | Instructs to skip all and any checks on keys, specified in this array. Put them as strings. |
acceptArrays | Boolean | no | false | If it's set to true , value can be array of elements, same type as reference. |
acceptArraysIgnore | Array of strings or String | no | [] (empty array) | If you want to ignore acceptArrays on certain keys, pass them in an array here. |
enforceStrictKeyset | Boolean | no | true | If it's set to true , your object must not have any unique keys that reference object does not have. |
} | | | | |
For example
const checkTypes = require('check-types-mini')
const objectAssign = require('object-assign')
const clone = require('lodash.clonedeep')
function yourFunction (input, opts) {
var defaults = {
placeholder: false
}
opts = objectAssign(clone(defaults), opts)
checkTypes(opts, defaults, 'newLibrary/yourFunction(): [THROW_ID_01]', 'opts')
}
var res = yourFunction(1, {placeholder: 'zzz'})
If you are happy with opts
variable name, you can omit the fourth argument; it will be set to that by default.
Sometimes you want to accept either a string (or type "X") or an arrays of strings (elements of type "X"). As long as ALL the elements within the array match the reference type, it's OK. For these cases set opts.acceptArrays
to true
:
const checkTypes = require('check-types-mini')
var res = checkTypes(
{
option1: 'setting1',
option2: [true, true],
option3: false
},
{
option1: 'setting1',
option2: false,
option3: false
}
)
But this does not throw
when we allow arrays:
const checkTypes = require('check-types-mini')
var res = checkTypes(
{
option1: 'setting1',
option2: ['setting3', 'setting4'],
option3: false
},
{
option1: 'setting1',
option2: 'setting2',
option3: false
},
'check-types-mini/checkTypes(): [THROW_ID_01]',
'opts',
{
acceptArrays: true
}
)
If you want, you can blacklist certain keys of your objects so that opts.acceptArrays
will not apply to them. Just add keys into opts.acceptArraysIgnore
array.
opts.enforceStrictKeyset
When I was coding a new major version of posthtml-ast-delete-object I had to update all the unit tests too. Previously, the settings was only one argument - Boolean. I had to change it to be a plain object. I noticed that old Boolean argument was not causing problems! Then I came up with the idea to enforce the keys of the object to match the reference. This was released in v1.4.0
. It's on by default because I can't imagine how you would end up with settings object that does not match your default settings object, key-wise, but if you don't like that, feel free to turn it off. It's opts.enforceStrictKeyset
Boolean flag.
Contributing
All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js
with new unit tests covering your feature(s).
If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged as soon as possible. If you have any comments on the code, including ideas how to improve something, don't hesitate to contact me by email.
Licence
MIT License (MIT)
Copyright (c) 2017 Codsen Ltd, Roy Reveltas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.