New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

check-types-mini

Package Overview
Dependencies
Maintainers
1
Versions
198
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

check-types-mini - npm Package Compare versions

Comparing version 1.2.2 to 1.3.0

10

changelog.md

@@ -7,9 +7,14 @@ # Change Log

## [1.3.0] - 2017-05-22
### Added
- ✨ `opts.acceptArrays` will accept arrays too, if they contain only the same type elements as the one that's being checked.
- ✨ `opts.acceptArraysIgnore` - lets you ignore per-key level when `opts.acceptArrays` is on. 👍
## [1.2.0] - 2017-05-15
### Added
- opts.ignoreKeys won't throw now if input is a single string.
- `opts.ignoreKeys` won't throw now if input is a single string.
## [1.1.0] - 2017-05-15
### Added
- opts.ignoreKeys
- ✨ `opts.ignoreKeys`

@@ -22,1 +27,2 @@ ## 1.0.0 - 2017-05-15

[1.2.0]: https://github.com/codsen/check-types-mini/compare/v1.1.0...v1.2.0
[1.3.0]: https://github.com/codsen/check-types-mini/compare/v1.2.2...v1.3.0

39

index.js

@@ -8,5 +8,7 @@ 'use strict'

const includes = require('lodash.includes')
const arrayiffyIfString = require('arrayiffy-if-string')
function checkTypes (obj, ref, msg, optsVarName, opts) {
function existy (x) { return x != null }
function isBool (something) { return type(something) === 'boolean' }

@@ -34,19 +36,36 @@ if (arguments.length === 0) {

var defaults = {
ignoreKeys: []
ignoreKeys: [],
acceptArrays: false,
acceptArraysIgnore: []
}
opts = objectAssign(clone(defaults), opts)
if (typeof opts.ignoreKeys === 'string') {
if (opts.ignoreKeys.length > 0) {
opts.ignoreKeys = [opts.ignoreKeys]
} else {
opts.ignoreKeys = []
}
}
opts.ignoreKeys = arrayiffyIfString(opts.ignoreKeys)
opts.acceptArraysIgnore = arrayiffyIfString(opts.acceptArraysIgnore)
if (!isArr(opts.ignoreKeys)) {
throw new TypeError('check-types-mini/checkTypes(): [THROW_ID_03] opts.ignoreKeys should be an array, currently it\'s: ' + type(opts.ignoreKeys))
}
if (!isBool(opts.acceptArrays)) {
throw new TypeError('check-types-mini/checkTypes(): [THROW_ID_04] opts.acceptArrays should be a Boolean, currently it\'s: ' + type(opts.acceptArrays))
}
if (!isArr(opts.acceptArraysIgnore)) {
throw new TypeError('check-types-mini/checkTypes(): [THROW_ID_03] opts.acceptArraysIgnore should be an array, currently it\'s: ' + type(opts.acceptArraysIgnore))
}
Object.keys(obj).forEach(function (key) {
if (existy(ref[key]) && (type(obj[key]) !== type(ref[key])) && !includes(opts.ignoreKeys, key)) {
throw new TypeError(`${msg}${optsVarName}${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not ${type(ref[key])} but ${type(obj[key])}`)
if (
existy(ref[key]) &&
(type(obj[key]) !== type(ref[key])) &&
!includes(opts.ignoreKeys, key)
) {
if (opts.acceptArrays && isArr(obj[key]) && !includes(opts.acceptArraysIgnore, key)) {
var allMatch = obj[key].every(function (el, i) {
return type(el) === type(ref[key])
})
if (!allMatch) {
throw new TypeError(`${msg}${optsVarName}${key} was customised to be array, but not all of its elements are ${type(ref[key])}-type`)
}
} else {
throw new TypeError(`${msg}${optsVarName}${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not ${type(ref[key])} but ${type(obj[key])}`)
}
}

@@ -53,0 +72,0 @@ })

{
"name": "check-types-mini",
"version": "1.2.2",
"version": "1.3.0",
"description": "Check the types of your options object's values after user has customised them",

@@ -38,3 +38,4 @@ "main": "index.js",

"dependencies": {
"lodash.includes": "^4.3.0",
"arrayiffy-if-string": "*",
"lodash.includes": "*",
"type-detect": "^4.0.0"

@@ -44,6 +45,6 @@ },

"ava": "*",
"clear-cli": "^1.0.1",
"clear-cli": "*",
"coveralls": "*",
"husky": "*",
"nodemon": "^1.11.0",
"nodemon": "*",
"nyc": "*",

@@ -50,0 +51,0 @@ "standard": "^10.0.0"

@@ -63,3 +63,5 @@ # check-types-mini

{ | | | |
`ignoreKeys` | Array or String | no | `[]` (empty array) | Instructs to skip all and any checks on keys, specified in this array. Put them as strings.
`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.
} | | | |

@@ -93,2 +95,47 @@

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`:
```js
const checkTypes = require('check-types-mini')
var res = checkTypes(
{ // < input
option1: 'setting1',
option2: [true, true],
option3: false
},
{ // < reference
option1: 'setting1',
option2: false,
option3: false
}
)
// => Throws, because reference's `option2` is Boolean ("false") but input `option2` is array ("[true, true]").
```
But this does not `throw`:
```js
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
}
)
// => Does not throw.
```
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.
## Contributing

@@ -95,0 +142,0 @@

@@ -133,1 +133,218 @@ 'use strict'

})
// ======================
// 02. Arrays
// ======================
test('02.01 - opts.acceptArrays, strings+arrays', t => {
t.throws(function () {
checkTypes(
{
option1: 'setting1',
option2: ['setting3', 'setting4'],
option3: false
},
{
option1: 'setting1',
option2: 'setting2',
option3: false
}
)
})
t.notThrows(function () {
checkTypes(
{
option1: 'setting1',
option2: ['setting3', 'setting4'],
option3: false
},
{
option1: 'setting1',
option2: 'setting2',
option3: false
},
'message',
'varname',
{
acceptArrays: true
}
)
})
t.throws(function () {
checkTypes(
{
option1: 'setting1',
option2: ['setting3', true, 'setting4'],
option3: false
},
{
option1: 'setting1',
option2: 'setting2',
option3: false
},
'message',
'varname',
{
acceptArrays: true
}
)
})
})
test('02.02 - opts.acceptArrays, Booleans+arrays', t => {
t.throws(function () {
checkTypes(
{
option1: 'setting1',
option2: [true, true],
option3: false
},
{
option1: 'setting1',
option2: false,
option3: false
}
)
})
t.notThrows(function () {
checkTypes(
{
option1: 'setting1',
option2: [true, true],
option3: false
},
{
option1: 'setting1',
option2: false,
option3: false
},
'message',
'varname',
{
acceptArrays: true
}
)
})
t.throws(function () {
checkTypes(
{
option1: 'setting1',
option2: [true, true, 1],
option3: false
},
{
option1: 'setting1',
option2: false,
option3: false
},
'message',
'varname',
{
acceptArrays: true
}
)
})
t.throws(function () {
checkTypes(
{
option1: [1, 0, 1, 0],
option2: [true, true],
option3: false
},
{
option1: 0,
option2: false,
option3: false
},
'test: [THROW_ID_01]',
'opts',
{
acceptArrays: 'this string will cause the throw',
acceptArraysIgnore: []
}
)
})
})
test('02.03 - opts.acceptArraysIgnore', t => {
t.notThrows(function () {
checkTypes(
{
option1: [1, 0, 1, 0],
option2: [true, true],
option3: false
},
{
option1: 0,
option2: false,
option3: false
},
'test: [THROW_ID_01]',
'opts',
{
acceptArrays: true,
acceptArraysIgnore: []
}
)
})
t.throws(function () {
checkTypes(
{
option1: [1, 0, 1, 0],
option2: [true, true],
option3: false
},
{
option1: 0,
option2: false,
option3: false
},
'test: [THROW_ID_01]',
'opts',
{
acceptArrays: true,
acceptArraysIgnore: ['zzz', 'option1']
}
)
})
t.throws(function () {
checkTypes(
{
option1: [1, 0, 1, 0],
option2: [true, true],
option3: false
},
{
option1: 0,
option2: false,
option3: false
},
'test: [THROW_ID_01]',
'opts',
{
acceptArrays: false,
acceptArraysIgnore: ['zzz', 'option1']
}
)
})
t.throws(function () {
checkTypes(
{
option1: [1, 0, 1, 0],
option2: [true, true],
option3: false
},
{
option1: 0,
option2: false,
option3: false
},
'test: [THROW_ID_01]',
'opts',
{
acceptArrays: true,
acceptArraysIgnore: true
}
)
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc