Comparing version 0.0.5 to 0.0.6
@@ -143,3 +143,3 @@ 'use strict'; | ||
function isString(x) { | ||
return Object.prototype.toString.call(x) === "[object String]"; | ||
return typeof x === "string"; | ||
} | ||
@@ -152,47 +152,43 @@ function arrayDifference(arr1, arr2) { | ||
function checkObjectSchema(schema, obj) { | ||
return _traverseObjects(schema, obj); | ||
} // similar to VueJS traverse https://github.com/vuejs/vue/blob/52719ccab8fccffbdf497b96d3731dc86f04c1ce/src/core/observer/traverse.js#L19 | ||
// JSON data won't be cyclical so we don't need to do id checking | ||
function _traverseObjects(val1, val2) { | ||
var isO1 = isPlainObject(val1); | ||
var isO2 = isPlainObject(val2); | ||
var isA1 = Array.isArray(val1); | ||
var isA2 = Array.isArray(val2); | ||
var i, keys1, keys2; | ||
function traverseObjects(schema, data) { | ||
var isSchemaAnObject = isPlainObject(schema); | ||
var isDataAnObject = isPlainObject(data); | ||
var isSchemaAnArray = Array.isArray(schema); | ||
var isDataAnArray = Array.isArray(data); | ||
if (isA1) { | ||
if (!isA2) { | ||
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(val2))); | ||
if (isSchemaAnArray) { | ||
if (!isDataAnArray) { | ||
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(data))); | ||
} | ||
i = val2.length; | ||
var i = data.length; | ||
while (i--) { | ||
_traverseObjects(val1[0], val2[i]); | ||
traverseObjects(schema[0], data[i]); | ||
} | ||
} else if (isO1) { | ||
if (!isO2) { | ||
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(val2))); | ||
} else if (isSchemaAnObject) { | ||
if (isDataAnArray || !isDataAnObject) { | ||
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(data))); | ||
} //loop object | ||
keys1 = Object.keys(val1).sort(); | ||
keys2 = Object.keys(val2).sort(); | ||
var schemaKeys = Object.keys(schema).sort(); | ||
var dataKeys = Object.keys(data).sort(); | ||
if (!looseEqual(keys1, keys2)) { | ||
throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(arrayDifference(keys1, keys2)))); | ||
if (!looseEqual(schemaKeys, dataKeys)) { | ||
throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(arrayDifference(schemaKeys, dataKeys)))); | ||
} | ||
i = keys1.length; | ||
var _i = schemaKeys.length; | ||
while (i--) { | ||
_traverseObjects(val1[keys1[i]], val2[keys2[i]]); | ||
while (_i--) { | ||
traverseObjects(schema[schemaKeys[_i]], data[dataKeys[_i]]); | ||
} | ||
} else { | ||
switch (val1) { | ||
switch (schema) { | ||
case "string": | ||
if (!isString(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a string.")); | ||
if (!isString(data)) { | ||
throw Error("\"".concat(data, "\" is not a string.")); | ||
} | ||
@@ -202,5 +198,5 @@ | ||
case 0: | ||
if (!isNumber(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a number.")); | ||
case "number": | ||
if (!isNumber(data)) { | ||
throw Error("\"".concat(data, "\" is not a number.")); | ||
} | ||
@@ -210,5 +206,5 @@ | ||
case true: | ||
if (!isBoolean(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a boolean.")); | ||
case "boolean": | ||
if (!isBoolean(data)) { | ||
throw Error("\"".concat(data, "\" is not a boolean.")); | ||
} | ||
@@ -228,5 +224,5 @@ | ||
**/ | ||
return checkObjectSchema(schema, data); | ||
return traverseObjects(schema, data); | ||
} | ||
module.exports = index; |
@@ -141,3 +141,3 @@ function _typeof(obj) { | ||
function isString(x) { | ||
return Object.prototype.toString.call(x) === "[object String]"; | ||
return typeof x === "string"; | ||
} | ||
@@ -150,47 +150,43 @@ function arrayDifference(arr1, arr2) { | ||
function checkObjectSchema(schema, obj) { | ||
return _traverseObjects(schema, obj); | ||
} // similar to VueJS traverse https://github.com/vuejs/vue/blob/52719ccab8fccffbdf497b96d3731dc86f04c1ce/src/core/observer/traverse.js#L19 | ||
// JSON data won't be cyclical so we don't need to do id checking | ||
function _traverseObjects(val1, val2) { | ||
var isO1 = isPlainObject(val1); | ||
var isO2 = isPlainObject(val2); | ||
var isA1 = Array.isArray(val1); | ||
var isA2 = Array.isArray(val2); | ||
var i, keys1, keys2; | ||
function traverseObjects(schema, data) { | ||
var isSchemaAnObject = isPlainObject(schema); | ||
var isDataAnObject = isPlainObject(data); | ||
var isSchemaAnArray = Array.isArray(schema); | ||
var isDataAnArray = Array.isArray(data); | ||
if (isA1) { | ||
if (!isA2) { | ||
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(val2))); | ||
if (isSchemaAnArray) { | ||
if (!isDataAnArray) { | ||
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(data))); | ||
} | ||
i = val2.length; | ||
var i = data.length; | ||
while (i--) { | ||
_traverseObjects(val1[0], val2[i]); | ||
traverseObjects(schema[0], data[i]); | ||
} | ||
} else if (isO1) { | ||
if (!isO2) { | ||
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(val2))); | ||
} else if (isSchemaAnObject) { | ||
if (isDataAnArray || !isDataAnObject) { | ||
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(data))); | ||
} //loop object | ||
keys1 = Object.keys(val1).sort(); | ||
keys2 = Object.keys(val2).sort(); | ||
var schemaKeys = Object.keys(schema).sort(); | ||
var dataKeys = Object.keys(data).sort(); | ||
if (!looseEqual(keys1, keys2)) { | ||
throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(arrayDifference(keys1, keys2)))); | ||
if (!looseEqual(schemaKeys, dataKeys)) { | ||
throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(arrayDifference(schemaKeys, dataKeys)))); | ||
} | ||
i = keys1.length; | ||
var _i = schemaKeys.length; | ||
while (i--) { | ||
_traverseObjects(val1[keys1[i]], val2[keys2[i]]); | ||
while (_i--) { | ||
traverseObjects(schema[schemaKeys[_i]], data[dataKeys[_i]]); | ||
} | ||
} else { | ||
switch (val1) { | ||
switch (schema) { | ||
case "string": | ||
if (!isString(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a string.")); | ||
if (!isString(data)) { | ||
throw Error("\"".concat(data, "\" is not a string.")); | ||
} | ||
@@ -200,5 +196,5 @@ | ||
case 0: | ||
if (!isNumber(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a number.")); | ||
case "number": | ||
if (!isNumber(data)) { | ||
throw Error("\"".concat(data, "\" is not a number.")); | ||
} | ||
@@ -208,5 +204,5 @@ | ||
case true: | ||
if (!isBoolean(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a boolean.")); | ||
case "boolean": | ||
if (!isBoolean(data)) { | ||
throw Error("\"".concat(data, "\" is not a boolean.")); | ||
} | ||
@@ -226,5 +222,5 @@ | ||
**/ | ||
return checkObjectSchema(schema, data); | ||
return traverseObjects(schema, data); | ||
} | ||
export default index; |
@@ -147,3 +147,3 @@ (function (global, factory) { | ||
function isString(x) { | ||
return Object.prototype.toString.call(x) === "[object String]"; | ||
return typeof x === "string"; | ||
} | ||
@@ -156,47 +156,43 @@ function arrayDifference(arr1, arr2) { | ||
function checkObjectSchema(schema, obj) { | ||
return _traverseObjects(schema, obj); | ||
} // similar to VueJS traverse https://github.com/vuejs/vue/blob/52719ccab8fccffbdf497b96d3731dc86f04c1ce/src/core/observer/traverse.js#L19 | ||
// JSON data won't be cyclical so we don't need to do id checking | ||
function _traverseObjects(val1, val2) { | ||
var isO1 = isPlainObject(val1); | ||
var isO2 = isPlainObject(val2); | ||
var isA1 = Array.isArray(val1); | ||
var isA2 = Array.isArray(val2); | ||
var i, keys1, keys2; | ||
function traverseObjects(schema, data) { | ||
var isSchemaAnObject = isPlainObject(schema); | ||
var isDataAnObject = isPlainObject(data); | ||
var isSchemaAnArray = Array.isArray(schema); | ||
var isDataAnArray = Array.isArray(data); | ||
if (isA1) { | ||
if (!isA2) { | ||
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(val2))); | ||
if (isSchemaAnArray) { | ||
if (!isDataAnArray) { | ||
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(data))); | ||
} | ||
i = val2.length; | ||
var i = data.length; | ||
while (i--) { | ||
_traverseObjects(val1[0], val2[i]); | ||
traverseObjects(schema[0], data[i]); | ||
} | ||
} else if (isO1) { | ||
if (!isO2) { | ||
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(val2))); | ||
} else if (isSchemaAnObject) { | ||
if (isDataAnArray || !isDataAnObject) { | ||
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(data))); | ||
} //loop object | ||
keys1 = Object.keys(val1).sort(); | ||
keys2 = Object.keys(val2).sort(); | ||
var schemaKeys = Object.keys(schema).sort(); | ||
var dataKeys = Object.keys(data).sort(); | ||
if (!looseEqual(keys1, keys2)) { | ||
throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(arrayDifference(keys1, keys2)))); | ||
if (!looseEqual(schemaKeys, dataKeys)) { | ||
throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(arrayDifference(schemaKeys, dataKeys)))); | ||
} | ||
i = keys1.length; | ||
var _i = schemaKeys.length; | ||
while (i--) { | ||
_traverseObjects(val1[keys1[i]], val2[keys2[i]]); | ||
while (_i--) { | ||
traverseObjects(schema[schemaKeys[_i]], data[dataKeys[_i]]); | ||
} | ||
} else { | ||
switch (val1) { | ||
switch (schema) { | ||
case "string": | ||
if (!isString(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a string.")); | ||
if (!isString(data)) { | ||
throw Error("\"".concat(data, "\" is not a string.")); | ||
} | ||
@@ -206,5 +202,5 @@ | ||
case 0: | ||
if (!isNumber(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a number.")); | ||
case "number": | ||
if (!isNumber(data)) { | ||
throw Error("\"".concat(data, "\" is not a number.")); | ||
} | ||
@@ -214,5 +210,5 @@ | ||
case true: | ||
if (!isBoolean(val2)) { | ||
throw Error("\"".concat(val2, "\" is not a boolean.")); | ||
case "boolean": | ||
if (!isBoolean(data)) { | ||
throw Error("\"".concat(data, "\" is not a boolean.")); | ||
} | ||
@@ -232,3 +228,3 @@ | ||
**/ | ||
return checkObjectSchema(schema, data); | ||
return traverseObjects(schema, data); | ||
} | ||
@@ -235,0 +231,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t=t||self).mcgorgeous=r()}(this,function(){"use strict";function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}Object.freeze({});function a(t){return null!==t&&"object"===r(t)}var e=Object.prototype.toString;function b(t){return"[object Object]"===e.call(t)}function t(t,r){for(var e=Object.create(null),n=t.split(","),o=0;o<n.length;o++)e[n[o]]=!0;return r?function(t){return e[t.toLowerCase()]}:function(t){return e[t]}}t("slot,component",!0),t("key,ref,slot,slot-scope,is");Function.prototype.bind;function g(r,e){if(r===e)return!0;var t=a(r),n=a(e);if(!t||!n)return!t&&!n&&String(r)===String(e);try{var o=Array.isArray(r),i=Array.isArray(e);if(o&&i)return r.length===e.length&&r.every(function(t,r){return g(t,e[r])});if(r instanceof Date&&e instanceof Date)return r.getTime()===e.getTime();if(o||i)return!1;var c=Object.keys(r),f=Object.keys(e);return c.length===f.length&&c.every(function(t){return g(r[t],e[t])})}catch(t){return!1}}function n(t,r){return function t(r,e){var n=b(r);var o=b(e);var i=Array.isArray(r);var c=Array.isArray(e);var f,a,u;if(i){if(!c)throw Error('Schema is looking for "array", data is '.concat(JSON.stringify(e)));for(f=e.length;f--;)t(r[0],e[f])}else if(n){if(!o)throw Error('Schema is looking for "object", data is '.concat(JSON.stringify(e)));if(a=Object.keys(r).sort(),u=Object.keys(e).sort(),!g(a,u))throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify((l=u,a.filter(function(t){return!l.includes(t)})))));for(f=a.length;f--;)t(r[a[f]],e[u[f]])}else switch(r){case"string":if(y=e,"[object String]"!==Object.prototype.toString.call(y))throw Error('"'.concat(e,'" is not a string.'));break;case 0:if("number"!=typeof e)throw Error('"'.concat(e,'" is not a number.'));break;case!0:if(!0!==(s=e)&&!1!==s)throw Error('"'.concat(e,'" is not a boolean.'))}var s;var y;var l;return!0}(t,r)}return function(t,r){return n(t,r)}}); | ||
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t=t||self).mcgorgeous=r()}(this,function(){"use strict";function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}Object.freeze({});function s(t){return null!==t&&"object"===r(t)}var n=Object.prototype.toString;function y(t){return"[object Object]"===n.call(t)}function t(t,r){for(var n=Object.create(null),e=t.split(","),o=0;o<e.length;o++)n[e[o]]=!0;return r?function(t){return n[t.toLowerCase()]}:function(t){return n[t]}}t("slot,component",!0),t("key,ref,slot,slot-scope,is");Function.prototype.bind;function l(t,r){var n=y(t),e=y(r),o=Array.isArray(t),i=Array.isArray(r);if(o){if(!i)throw Error('Schema is looking for "array", data is '.concat(JSON.stringify(r)));for(var c=r.length;c--;)l(t[0],r[c])}else if(n){if(i||!e)throw Error('Schema is looking for "object", data is '.concat(JSON.stringify(r)));var f=Object.keys(t).sort(),u=Object.keys(r).sort();if(!function n(r,e){if(r===e)return!0;var t=s(r),o=s(e);if(!t||!o)return!t&&!o&&String(r)===String(e);try{var i=Array.isArray(r),c=Array.isArray(e);if(i&&c)return r.length===e.length&&r.every(function(t,r){return n(t,e[r])});if(r instanceof Date&&e instanceof Date)return r.getTime()===e.getTime();if(i||c)return!1;var f=Object.keys(r),u=Object.keys(e);return f.length===u.length&&f.every(function(t){return n(r[t],e[t])})}catch(t){return!1}}(f,u))throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(function(t,r){return t.filter(function(t){return!r.includes(t)})}(f,u))));for(var a=f.length;a--;)l(t[f[a]],r[u[a]])}else switch(t){case"string":if(!function(t){return"string"==typeof t}(r))throw Error('"'.concat(r,'" is not a string.'));break;case"number":if(!function(t){return"number"==typeof t}(r))throw Error('"'.concat(r,'" is not a number.'));break;case"boolean":if(!function(t){return!0===t||!1===t}(r))throw Error('"'.concat(r,'" is not a boolean.'))}return!0}return function(t,r){return l(t,r)}}); |
{ | ||
"name": "mcgorgeous", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Checks schema of JSON return types", | ||
@@ -14,3 +14,3 @@ "main": "./dist/mcgorgeous.common.js", | ||
"scripts": { | ||
"lint": "prettier --write \"{./test.js,./src/*.js,./src/**/*.js}\"", | ||
"lint": "eslint src/*.js test.js && prettier --write \"{./test.js,./src/*.js,./src/**/*.js}\"", | ||
"build": "npm run lint && rollup --config", | ||
@@ -38,2 +38,4 @@ "test": "ava" | ||
"ava": "^2.1.0", | ||
"babel-eslint": "^10.0.2", | ||
"eslint": "^6.0.1", | ||
"prettier": "^1.18.2", | ||
@@ -40,0 +42,0 @@ "rollup": "^1.16.4", |
@@ -50,3 +50,3 @@ # McGorgeous | ||
```javascript | ||
[{ id: 0 }]; | ||
[{ id: "number" }]; | ||
``` | ||
@@ -56,3 +56,3 @@ | ||
```javascript | ||
[{ id: true }]; | ||
[{ id: "boolean" }]; | ||
``` | ||
@@ -59,0 +59,0 @@ |
@@ -1,3 +0,2 @@ | ||
import { isTrue } from "./util"; | ||
import { checkObjectSchema } from "./traverse"; | ||
import { traverseObjects } from "./traverse"; | ||
@@ -9,3 +8,3 @@ export default function(schema, data) { | ||
**/ | ||
return checkObjectSchema(schema, data); | ||
return traverseObjects(schema, data); | ||
} |
@@ -10,61 +10,54 @@ import { | ||
let uid = 0; | ||
export function checkObjectSchema(schema, obj) { | ||
return _traverseObjects(schema, obj); | ||
} | ||
// similar to VueJS traverse https://github.com/vuejs/vue/blob/52719ccab8fccffbdf497b96d3731dc86f04c1ce/src/core/observer/traverse.js#L19 | ||
// JSON data won't be cyclical so we don't need to do id checking | ||
function _traverseObjects(val1, val2) { | ||
const isO1 = isPlainObject(val1); | ||
const isO2 = isPlainObject(val2); | ||
const isA1 = Array.isArray(val1); | ||
const isA2 = Array.isArray(val2); | ||
let i, keys1, keys2; | ||
if (isA1) { | ||
if (!isA2) { | ||
export function traverseObjects(schema, data) { | ||
const isSchemaAnObject = isPlainObject(schema); | ||
const isDataAnObject = isPlainObject(data); | ||
const isSchemaAnArray = Array.isArray(schema); | ||
const isDataAnArray = Array.isArray(data); | ||
if (isSchemaAnArray) { | ||
if (!isDataAnArray) { | ||
throw Error( | ||
`Schema is looking for "array", data is ${JSON.stringify(val2)}` | ||
`Schema is looking for "array", data is ${JSON.stringify(data)}` | ||
); | ||
} | ||
i = val2.length; | ||
let i = data.length; | ||
while (i--) { | ||
_traverseObjects(val1[0], val2[i]); | ||
traverseObjects(schema[0], data[i]); | ||
} | ||
} else if (isO1) { | ||
if (!isO2) { | ||
} else if (isSchemaAnObject) { | ||
if (isDataAnArray || !isDataAnObject) { | ||
throw Error( | ||
`Schema is looking for "object", data is ${JSON.stringify(val2)}` | ||
`Schema is looking for "object", data is ${JSON.stringify(data)}` | ||
); | ||
} | ||
//loop object | ||
keys1 = Object.keys(val1).sort(); | ||
keys2 = Object.keys(val2).sort(); | ||
if (!looseEqual(keys1, keys2)) { | ||
const schemaKeys = Object.keys(schema).sort(); | ||
const dataKeys = Object.keys(data).sort(); | ||
if (!looseEqual(schemaKeys, dataKeys)) { | ||
throw Error( | ||
`Keys in schema don't match keys in data: ${JSON.stringify( | ||
arrayDifference(keys1, keys2) | ||
arrayDifference(schemaKeys, dataKeys) | ||
)}` | ||
); | ||
} | ||
i = keys1.length; | ||
let i = schemaKeys.length; | ||
while (i--) { | ||
_traverseObjects(val1[keys1[i]], val2[keys2[i]]); | ||
traverseObjects(schema[schemaKeys[i]], data[dataKeys[i]]); | ||
} | ||
} else { | ||
switch (val1) { | ||
switch (schema) { | ||
case "string": | ||
if (!isString(val2)) { | ||
throw Error(`"${val2}" is not a string.`); | ||
if (!isString(data)) { | ||
throw Error(`"${data}" is not a string.`); | ||
} | ||
break; | ||
case 0: | ||
if (!isNumber(val2)) { | ||
throw Error(`"${val2}" is not a number.`); | ||
case "number": | ||
if (!isNumber(data)) { | ||
throw Error(`"${data}" is not a number.`); | ||
} | ||
break; | ||
case true: | ||
if (!isBoolean(val2)) { | ||
throw Error(`"${val2}" is not a boolean.`); | ||
case "boolean": | ||
if (!isBoolean(data)) { | ||
throw Error(`"${data}" is not a boolean.`); | ||
} | ||
@@ -71,0 +64,0 @@ break; |
@@ -330,3 +330,3 @@ /* @flow */ | ||
export function isString(x) { | ||
return Object.prototype.toString.call(x) === "[object String]"; | ||
return typeof x === "string"; | ||
} | ||
@@ -333,0 +333,0 @@ |
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
32178
10
960