superstruct
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -7,5 +7,5 @@ 'use strict'; | ||
var _componentType = require('component-type'); | ||
var _kindOf = require('kind-of'); | ||
var _componentType2 = _interopRequireDefault(_componentType); | ||
var _kindOf2 = _interopRequireDefault(_kindOf); | ||
@@ -15,2 +15,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Type strings. | ||
* | ||
* @type {Array} | ||
*/ | ||
const TYPES = ['arguments', 'array', 'boolean', 'buffer', 'date', 'float32array', 'float64array', 'function', 'generatorfunction', 'int16array', 'int32array', 'int8array', 'map', 'null', 'number', 'object', 'regexp', 'set', 'string', 'symbol', 'uint16array', 'uint32array', 'uint8array', 'uint8clampedarray', 'undefined', 'weakmap', 'weakset']; | ||
/** | ||
* Default types. | ||
@@ -21,16 +29,17 @@ * | ||
exports.default = { | ||
any: v => v !== undefined, | ||
array: v => (0, _componentType2.default)(v) === 'array', | ||
boolean: v => (0, _componentType2.default)(v) === 'boolean', | ||
buffer: v => (0, _componentType2.default)(v) === 'buffer', | ||
date: v => (0, _componentType2.default)(v) === 'date', | ||
error: v => (0, _componentType2.default)(v) === 'error', | ||
function: v => (0, _componentType2.default)(v) === 'function', | ||
null: v => (0, _componentType2.default)(v) === 'null', | ||
number: v => (0, _componentType2.default)(v) === 'number', | ||
object: v => (0, _componentType2.default)(v) === 'object', | ||
regexp: v => (0, _componentType2.default)(v) === 'regexp', | ||
string: v => (0, _componentType2.default)(v) === 'string', | ||
undefined: v => (0, _componentType2.default)(v) === 'undefined' | ||
}; | ||
const DEFAULT_TYPES = { | ||
any: value => value !== undefined, | ||
error: value => Object.prototype.toString.call(value) === '[object Error]' | ||
}; | ||
TYPES.forEach(type => { | ||
DEFAULT_TYPES[type] = value => (0, _kindOf2.default)(value) === type; | ||
}); | ||
/** | ||
* Export. | ||
* | ||
* @type {Object} | ||
*/ | ||
exports.default = DEFAULT_TYPES; |
@@ -9,9 +9,9 @@ 'use strict'; | ||
var _lodash = require('lodash.clonedeep'); | ||
var _cloneDeep = require('clone-deep'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _cloneDeep2 = _interopRequireDefault(_cloneDeep); | ||
var _componentType = require('component-type'); | ||
var _kindOf = require('kind-of'); | ||
var _componentType2 = _interopRequireDefault(_componentType); | ||
var _kindOf2 = _interopRequireDefault(_kindOf); | ||
@@ -45,2 +45,10 @@ var _defaultTypes = require('./default-types'); | ||
/** | ||
* The diffrent Kinds of struct schemas. | ||
* | ||
* @type {Array} | ||
*/ | ||
const STRUCT_KINDS = ['scalar', 'function', 'object', 'list']; | ||
/** | ||
* Convenience flags for the struct factory. | ||
@@ -126,3 +134,3 @@ * | ||
const { defaults } = this; | ||
return typeof defaults === 'function' ? defaults() : (0, _lodash2.default)(defaults); | ||
return typeof defaults === 'function' ? defaults() : (0, _cloneDeep2.default)(defaults); | ||
} | ||
@@ -528,5 +536,6 @@ | ||
/** | ||
* Define a struct with `schema`, `defaults` and `options`. | ||
* Create a `kind` struct with schema `definition`, `defaults` and `options`. | ||
* | ||
* @param {Function|String|Array|Object} schema | ||
* @param {String} kind | ||
* @param {Function|String|Array|Object} definition | ||
* @param {Any} defaults | ||
@@ -537,21 +546,20 @@ * @param {Object} options | ||
function struct(schema, defaults, options = {}) { | ||
if (isStruct(schema)) { | ||
return schema; | ||
function createStruct(kind, definition, defaults, options) { | ||
if (isStruct(definition)) { | ||
return definition; | ||
} | ||
const type = (0, _componentType2.default)(schema); | ||
const args = [schema, defaults, options]; | ||
let sch; | ||
const args = [definition, defaults, options]; | ||
let schema; | ||
if (type === 'function') { | ||
sch = new FunctionSchema(...args); | ||
} else if (type === 'string') { | ||
sch = new ScalarSchema(...args); | ||
} else if (type === 'array') { | ||
sch = new ListSchema(...args); | ||
} else if (type === 'object') { | ||
sch = new ObjectSchema(...args); | ||
if (kind === 'function') { | ||
schema = new FunctionSchema(...args); | ||
} else if (kind === 'scalar') { | ||
schema = new ScalarSchema(...args); | ||
} else if (kind === 'list') { | ||
schema = new ListSchema(...args); | ||
} else if (kind === 'object') { | ||
schema = new ObjectSchema(...args); | ||
} else { | ||
throw new Error(`A struct schema definition must be a string, array or object, but you passed: ${schema}`); | ||
throw new Error(`Unrecognized struct kind: ${kind}`); | ||
} | ||
@@ -565,3 +573,3 @@ | ||
return sch.assert(data); | ||
return schema.assert(data); | ||
} | ||
@@ -574,4 +582,4 @@ | ||
STRUCT_METHODS.forEach(method => { | ||
if (sch[method]) { | ||
Struct[method] = (...a) => sch[method](...a); | ||
if (schema[method]) { | ||
Struct[method] = (...a) => schema[method](...a); | ||
} | ||
@@ -583,2 +591,26 @@ }); | ||
/** | ||
* Define a struct with schema `definition`, `defaults` and `options`. | ||
* | ||
* @param {Function|String|Array|Object} definition | ||
* @param {Any} defaults | ||
* @param {Object} options | ||
* @return {Function} | ||
*/ | ||
function struct(definition, defaults, options) { | ||
if (isStruct(definition)) { | ||
return definition; | ||
} | ||
const kind = getKind(definition); | ||
const Struct = createStruct(kind, definition, defaults, options); | ||
return Struct; | ||
} | ||
// Mix in a factory for each kind of struct. | ||
STRUCT_KINDS.forEach(kind => { | ||
struct[kind] = (...args) => createStruct(kind, ...args); | ||
}); | ||
// Mix in the convenience properties for option flags. | ||
@@ -610,2 +642,26 @@ STRUCT_FLAGS.forEach(flag => { | ||
/** | ||
* Get the kind of a struct from its schema `definition`. | ||
* | ||
* @param {Any} definition | ||
* @return {String} | ||
*/ | ||
function getKind(definition) { | ||
switch ((0, _kindOf2.default)(definition)) { | ||
case 'function': | ||
return 'function'; | ||
case 'string': | ||
return 'scalar'; | ||
case 'array': | ||
return 'list'; | ||
case 'object': | ||
return 'object'; | ||
default: | ||
{ | ||
throw new Error(`A struct schema definition must be a string, array or object, but you passed: ${definition}`); | ||
} | ||
} | ||
} | ||
/** | ||
* Export. | ||
@@ -612,0 +668,0 @@ * |
{ | ||
"name": "superstruct", | ||
"description": "A simple, expressive way to validate data in Javascript.", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"license": "MIT", | ||
@@ -22,4 +22,4 @@ "repository": "git://github.com/ianstormtaylor/superstruct.git", | ||
"dependencies": { | ||
"component-type": "^1.2.1", | ||
"lodash.clonedeep": "^4.5.0" | ||
"clone-deep": "^2.0.1", | ||
"kind-of": "^6.0.1" | ||
}, | ||
@@ -26,0 +26,0 @@ "devDependencies": { |
@@ -113,3 +113,3 @@ | ||
There are lots of existing validation libraries. Some of them, like [Joi](), [`express-validator`](https://github.com/ctavan/express-validator), [`validator.js`](https://github.com/chriso/validator.js) or [`ajv`](https://github.com/epoberezkin/ajv) are decently popular. But all of them exhibit many issues that lead to hard to maintain codebases... | ||
There are lots of existing validation libraries. Some of them, like [`joi`](), [`express-validator`](https://github.com/ctavan/express-validator), [`validator.js`](https://github.com/chriso/validator.js), [`yup`](https://github.com/jquense/yup) or [`ajv`](https://github.com/epoberezkin/ajv) are decently popular. But all of them exhibit many issues that lead to hard to maintain codebases... | ||
@@ -116,0 +116,0 @@ - **They can't throw errors.** Many validators simply return `true/false` or string errors. Although helpful in the days of callbacks, not using `throw` in modern Javascript makes code much more complex. |
import typeOf from 'component-type' | ||
import typeOf from 'kind-of' | ||
/** | ||
* Type strings. | ||
* | ||
* @type {Array} | ||
*/ | ||
const TYPES = [ | ||
'arguments', | ||
'array', | ||
'boolean', | ||
'buffer', | ||
'date', | ||
'float32array', | ||
'float64array', | ||
'function', | ||
'generatorfunction', | ||
'int16array', | ||
'int32array', | ||
'int8array', | ||
'map', | ||
'null', | ||
'number', | ||
'object', | ||
'regexp', | ||
'set', | ||
'string', | ||
'symbol', | ||
'uint16array', | ||
'uint32array', | ||
'uint8array', | ||
'uint8clampedarray', | ||
'undefined', | ||
'weakmap', | ||
'weakset', | ||
] | ||
/** | ||
* Default types. | ||
@@ -10,16 +46,17 @@ * | ||
export default { | ||
any: v => v !== undefined, | ||
array: v => typeOf(v) === 'array', | ||
boolean: v => typeOf(v) === 'boolean', | ||
buffer: v => typeOf(v) === 'buffer', | ||
date: v => typeOf(v) === 'date', | ||
error: v => typeOf(v) === 'error', | ||
function: v => typeOf(v) === 'function', | ||
null: v => typeOf(v) === 'null', | ||
number: v => typeOf(v) === 'number', | ||
object: v => typeOf(v) === 'object', | ||
regexp: v => typeOf(v) === 'regexp', | ||
string: v => typeOf(v) === 'string', | ||
undefined: v => typeOf(v) === 'undefined', | ||
const DEFAULT_TYPES = { | ||
any: value => value !== undefined, | ||
error: value => Object.prototype.toString.call(value) === '[object Error]', | ||
} | ||
TYPES.forEach((type) => { | ||
DEFAULT_TYPES[type] = value => typeOf(value) === type | ||
}) | ||
/** | ||
* Export. | ||
* | ||
* @type {Object} | ||
*/ | ||
export default DEFAULT_TYPES |
import cloneDeep from 'lodash.clonedeep' | ||
import typeOf from 'component-type' | ||
import cloneDeep from 'clone-deep' | ||
import typeOf from 'kind-of' | ||
@@ -34,2 +34,15 @@ import DEFAULT_TYPES from './default-types' | ||
/** | ||
* The diffrent Kinds of struct schemas. | ||
* | ||
* @type {Array} | ||
*/ | ||
const STRUCT_KINDS = [ | ||
'scalar', | ||
'function', | ||
'object', | ||
'list', | ||
] | ||
/** | ||
* Convenience flags for the struct factory. | ||
@@ -522,5 +535,6 @@ * | ||
/** | ||
* Define a struct with `schema`, `defaults` and `options`. | ||
* Create a `kind` struct with schema `definition`, `defaults` and `options`. | ||
* | ||
* @param {Function|String|Array|Object} schema | ||
* @param {String} kind | ||
* @param {Function|String|Array|Object} definition | ||
* @param {Any} defaults | ||
@@ -531,21 +545,20 @@ * @param {Object} options | ||
function struct(schema, defaults, options = {}) { | ||
if (isStruct(schema)) { | ||
return schema | ||
function createStruct(kind, definition, defaults, options) { | ||
if (isStruct(definition)) { | ||
return definition | ||
} | ||
const type = typeOf(schema) | ||
const args = [schema, defaults, options] | ||
let sch | ||
const args = [definition, defaults, options] | ||
let schema | ||
if (type === 'function') { | ||
sch = new FunctionSchema(...args) | ||
} else if (type === 'string') { | ||
sch = new ScalarSchema(...args) | ||
} else if (type === 'array') { | ||
sch = new ListSchema(...args) | ||
} else if (type === 'object') { | ||
sch = new ObjectSchema(...args) | ||
if (kind === 'function') { | ||
schema = new FunctionSchema(...args) | ||
} else if (kind === 'scalar') { | ||
schema = new ScalarSchema(...args) | ||
} else if (kind === 'list') { | ||
schema = new ListSchema(...args) | ||
} else if (kind === 'object') { | ||
schema = new ObjectSchema(...args) | ||
} else { | ||
throw new Error(`A struct schema definition must be a string, array or object, but you passed: ${schema}`) | ||
throw new Error(`Unrecognized struct kind: ${kind}`) | ||
} | ||
@@ -559,3 +572,3 @@ | ||
return sch.assert(data) | ||
return schema.assert(data) | ||
} | ||
@@ -568,4 +581,4 @@ | ||
STRUCT_METHODS.forEach((method) => { | ||
if (sch[method]) { | ||
Struct[method] = (...a) => sch[method](...a) | ||
if (schema[method]) { | ||
Struct[method] = (...a) => schema[method](...a) | ||
} | ||
@@ -577,2 +590,26 @@ }) | ||
/** | ||
* Define a struct with schema `definition`, `defaults` and `options`. | ||
* | ||
* @param {Function|String|Array|Object} definition | ||
* @param {Any} defaults | ||
* @param {Object} options | ||
* @return {Function} | ||
*/ | ||
function struct(definition, defaults, options) { | ||
if (isStruct(definition)) { | ||
return definition | ||
} | ||
const kind = getKind(definition) | ||
const Struct = createStruct(kind, definition, defaults, options) | ||
return Struct | ||
} | ||
// Mix in a factory for each kind of struct. | ||
STRUCT_KINDS.forEach((kind) => { | ||
struct[kind] = (...args) => createStruct(kind, ...args) | ||
}) | ||
// Mix in the convenience properties for option flags. | ||
@@ -604,2 +641,21 @@ STRUCT_FLAGS.forEach((flag) => { | ||
/** | ||
* Get the kind of a struct from its schema `definition`. | ||
* | ||
* @param {Any} definition | ||
* @return {String} | ||
*/ | ||
function getKind(definition) { | ||
switch (typeOf(definition)) { | ||
case 'function': return 'function' | ||
case 'string': return 'scalar' | ||
case 'array': return 'list' | ||
case 'object': return 'object' | ||
default: { | ||
throw new Error(`A struct schema definition must be a string, array or object, but you passed: ${definition}`) | ||
} | ||
} | ||
} | ||
/** | ||
* Export. | ||
@@ -606,0 +662,0 @@ * |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
0
532667
3417
+ Addedclone-deep@^2.0.1
+ Addedkind-of@^6.0.1
+ Addedclone-deep@2.0.2(transitive)
+ Addedfor-in@0.1.81.0.2(transitive)
+ Addedfor-own@1.0.0(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedkind-of@5.1.06.0.3(transitive)
+ Addedmixin-object@2.0.1(transitive)
+ Addedshallow-clone@1.0.0(transitive)
- Removedcomponent-type@^1.2.1
- Removedlodash.clonedeep@^4.5.0
- Removedcomponent-type@1.2.2(transitive)
- Removedlodash.clonedeep@4.5.0(transitive)