Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mcgorgeous

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mcgorgeous - npm Package Compare versions

Comparing version 0.0.6 to 0.0.9

74

dist/mcgorgeous.common.js

@@ -19,2 +19,7 @@ 'use strict';

var emptyObject = Object.freeze({}); // These helpers produce better VM code in JS engines due to their
// explicitness and function inlining.
function isUndef(v) {
return v === undefined || v === null;
}
/**

@@ -43,2 +48,9 @@ * Quick object check - this is primarily used to tell

/**
* Convert a value to a string that is actually rendered.
*/
function toString(val) {
return val == null ? "" : Array.isArray(val) || isPlainObject(val) && val.toString === _toString ? JSON.stringify(val, null, 2) : String(val);
}
/**
* Make a map and return a function for checking if a key

@@ -155,10 +167,15 @@ * is in that map.

/**
* {prop: type} e.g.
* {name: String}
**/
function traverseObjects(schema, data) {
var isSchemaAnObject = isPlainObject(schema);
var isDataAnObject = isPlainObject(data);
var isSchemaAnArray = Array.isArray(schema);
var isDataAnArray = Array.isArray(data);
if (isSchemaAnArray) {
if (!isDataAnArray) {
// On the top level we have three "types" of cases
// - Arrays
// - Objects
// - Literals
if (Array.isArray(schema)) {
// Handling arrays
if (!Array.isArray(data)) {
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(data)));

@@ -172,4 +189,5 @@ }

}
} else if (isSchemaAnObject) {
if (isDataAnArray || !isDataAnObject) {
} else if (isPlainObject(schema)) {
// Handling object
if (Array.isArray(data) || !isPlainObject(data)) {
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(data)));

@@ -192,5 +210,18 @@ } //loop object

} else {
switch (schema) {
var schemaDefinition = toString(schema).split(" ");
schemaDefinition.push("");
var canBeNull = schemaDefinition[1] !== "notnull";
var isUndefProblem = !canBeNull && isUndef(data);
switch (schemaDefinition[0]) {
case "":
break;
case "null":
break;
case "string":
if (!isString(data)) {
if (isUndefProblem) {
throw Error("String cannot be null");
} else if (!isString(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a string."));

@@ -202,3 +233,5 @@ }

case "number":
if (!isNumber(data)) {
if (isUndefProblem) {
throw Error("Number cannot be null");
} else if (!isNumber(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a number."));

@@ -210,3 +243,5 @@ }

case "boolean":
if (!isBoolean(data)) {
if (isUndefProblem) {
throw Error("Boolean cannot be null");
} else if (!isBoolean(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a boolean."));

@@ -216,2 +251,5 @@ }

break;
default:
throw new Error("Unknown schema type: ".concat(JSON.stringify(schemaDefinition[0])));
}

@@ -223,10 +261,2 @@ }

function index (schema, data) {
/**
* {prop: type} e.g.
* {name: String}
**/
return traverseObjects(schema, data);
}
module.exports = index;
module.exports = traverseObjects;

@@ -17,2 +17,7 @@ function _typeof(obj) {

var emptyObject = Object.freeze({}); // These helpers produce better VM code in JS engines due to their
// explicitness and function inlining.
function isUndef(v) {
return v === undefined || v === null;
}
/**

@@ -41,2 +46,9 @@ * Quick object check - this is primarily used to tell

/**
* Convert a value to a string that is actually rendered.
*/
function toString(val) {
return val == null ? "" : Array.isArray(val) || isPlainObject(val) && val.toString === _toString ? JSON.stringify(val, null, 2) : String(val);
}
/**
* Make a map and return a function for checking if a key

@@ -153,10 +165,15 @@ * is in that map.

/**
* {prop: type} e.g.
* {name: String}
**/
function traverseObjects(schema, data) {
var isSchemaAnObject = isPlainObject(schema);
var isDataAnObject = isPlainObject(data);
var isSchemaAnArray = Array.isArray(schema);
var isDataAnArray = Array.isArray(data);
if (isSchemaAnArray) {
if (!isDataAnArray) {
// On the top level we have three "types" of cases
// - Arrays
// - Objects
// - Literals
if (Array.isArray(schema)) {
// Handling arrays
if (!Array.isArray(data)) {
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(data)));

@@ -170,4 +187,5 @@ }

}
} else if (isSchemaAnObject) {
if (isDataAnArray || !isDataAnObject) {
} else if (isPlainObject(schema)) {
// Handling object
if (Array.isArray(data) || !isPlainObject(data)) {
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(data)));

@@ -190,5 +208,18 @@ } //loop object

} else {
switch (schema) {
var schemaDefinition = toString(schema).split(" ");
schemaDefinition.push("");
var canBeNull = schemaDefinition[1] !== "notnull";
var isUndefProblem = !canBeNull && isUndef(data);
switch (schemaDefinition[0]) {
case "":
break;
case "null":
break;
case "string":
if (!isString(data)) {
if (isUndefProblem) {
throw Error("String cannot be null");
} else if (!isString(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a string."));

@@ -200,3 +231,5 @@ }

case "number":
if (!isNumber(data)) {
if (isUndefProblem) {
throw Error("Number cannot be null");
} else if (!isNumber(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a number."));

@@ -208,3 +241,5 @@ }

case "boolean":
if (!isBoolean(data)) {
if (isUndefProblem) {
throw Error("Boolean cannot be null");
} else if (!isBoolean(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a boolean."));

@@ -214,2 +249,5 @@ }

break;
default:
throw new Error("Unknown schema type: ".concat(JSON.stringify(schemaDefinition[0])));
}

@@ -221,10 +259,2 @@ }

function index (schema, data) {
/**
* {prop: type} e.g.
* {name: String}
**/
return traverseObjects(schema, data);
}
export default index;
export default traverseObjects;

@@ -23,2 +23,7 @@ (function (global, factory) {

var emptyObject = Object.freeze({}); // These helpers produce better VM code in JS engines due to their
// explicitness and function inlining.
function isUndef(v) {
return v === undefined || v === null;
}
/**

@@ -47,2 +52,9 @@ * Quick object check - this is primarily used to tell

/**
* Convert a value to a string that is actually rendered.
*/
function toString(val) {
return val == null ? "" : Array.isArray(val) || isPlainObject(val) && val.toString === _toString ? JSON.stringify(val, null, 2) : String(val);
}
/**
* Make a map and return a function for checking if a key

@@ -159,10 +171,15 @@ * is in that map.

/**
* {prop: type} e.g.
* {name: String}
**/
function traverseObjects(schema, data) {
var isSchemaAnObject = isPlainObject(schema);
var isDataAnObject = isPlainObject(data);
var isSchemaAnArray = Array.isArray(schema);
var isDataAnArray = Array.isArray(data);
if (isSchemaAnArray) {
if (!isDataAnArray) {
// On the top level we have three "types" of cases
// - Arrays
// - Objects
// - Literals
if (Array.isArray(schema)) {
// Handling arrays
if (!Array.isArray(data)) {
throw Error("Schema is looking for \"array\", data is ".concat(JSON.stringify(data)));

@@ -176,4 +193,5 @@ }

}
} else if (isSchemaAnObject) {
if (isDataAnArray || !isDataAnObject) {
} else if (isPlainObject(schema)) {
// Handling object
if (Array.isArray(data) || !isPlainObject(data)) {
throw Error("Schema is looking for \"object\", data is ".concat(JSON.stringify(data)));

@@ -196,5 +214,18 @@ } //loop object

} else {
switch (schema) {
var schemaDefinition = toString(schema).split(" ");
schemaDefinition.push("");
var canBeNull = schemaDefinition[1] !== "notnull";
var isUndefProblem = !canBeNull && isUndef(data);
switch (schemaDefinition[0]) {
case "":
break;
case "null":
break;
case "string":
if (!isString(data)) {
if (isUndefProblem) {
throw Error("String cannot be null");
} else if (!isString(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a string."));

@@ -206,3 +237,5 @@ }

case "number":
if (!isNumber(data)) {
if (isUndefProblem) {
throw Error("Number cannot be null");
} else if (!isNumber(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a number."));

@@ -214,3 +247,5 @@ }

case "boolean":
if (!isBoolean(data)) {
if (isUndefProblem) {
throw Error("Boolean cannot be null");
} else if (!isBoolean(data) && !isUndef(data)) {
throw Error("\"".concat(data, "\" is not a boolean."));

@@ -220,2 +255,5 @@ }

break;
default:
throw new Error("Unknown schema type: ".concat(JSON.stringify(schemaDefinition[0])));
}

@@ -227,12 +265,4 @@ }

function index (schema, data) {
/**
* {prop: type} e.g.
* {name: String}
**/
return traverseObjects(schema, data);
}
return traverseObjects;
return index;
}));

@@ -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 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)}});
!function(r,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(r=r||self).mcgorgeous=t()}(this,function(){"use strict";function t(r){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}Object.freeze({});function f(r){return null==r}function s(r){return null!==r&&"object"===t(r)}var l=Object.prototype.toString;function y(r){return"[object Object]"===l.call(r)}function r(r,t){for(var n=Object.create(null),e=r.split(","),o=0;o<e.length;o++)n[e[o]]=!0;return t?function(r){return n[r.toLowerCase()]}:function(r){return n[r]}}r("slot,component",!0),r("key,ref,slot,slot-scope,is");Function.prototype.bind;return function r(t,n){if(Array.isArray(t)){if(!Array.isArray(n))throw Error('Schema is looking for "array", data is '.concat(JSON.stringify(n)));for(var e=n.length;e--;)r(t[0],n[e])}else if(y(t)){if(Array.isArray(n)||!y(n))throw Error('Schema is looking for "object", data is '.concat(JSON.stringify(n)));var o=Object.keys(t).sort(),i=Object.keys(n).sort();if(!function n(t,e){if(t===e)return!0;var r=s(t),o=s(e);if(!r||!o)return!r&&!o&&String(t)===String(e);try{var i=Array.isArray(t),c=Array.isArray(e);if(i&&c)return t.length===e.length&&t.every(function(r,t){return n(r,e[t])});if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(i||c)return!1;var u=Object.keys(t),a=Object.keys(e);return u.length===a.length&&u.every(function(r){return n(t[r],e[r])})}catch(r){return!1}}(o,i))throw Error("Keys in schema don't match keys in data: ".concat(JSON.stringify(function(r,t){return r.filter(function(r){return!t.includes(r)})}(o,i))));for(var c=o.length;c--;)r(t[o[c]],n[i[c]])}else{var u=function(r){return null==r?"":Array.isArray(r)||y(r)&&r.toString===l?JSON.stringify(r,null,2):String(r)}(t).split(" ");u.push("");var a=!("notnull"!==u[1])&&f(n);switch(u[0]){case"":case"null":break;case"string":if(a)throw Error("String cannot be null");if(!function(r){return"string"==typeof r}(n)&&!f(n))throw Error('"'.concat(n,'" is not a string.'));break;case"number":if(a)throw Error("Number cannot be null");if(!function(r){return"number"==typeof r}(n)&&!f(n))throw Error('"'.concat(n,'" is not a number.'));break;case"boolean":if(a)throw Error("Boolean cannot be null");if(!function(r){return!0===r||!1===r}(n)&&!f(n))throw Error('"'.concat(n,'" is not a boolean.'));break;default:throw new Error("Unknown schema type: ".concat(JSON.stringify(u[0])))}}return!0}});
{
"name": "mcgorgeous",
"version": "0.0.6",
"version": "0.0.9",
"description": "Checks schema of JSON return types",

@@ -5,0 +5,0 @@ "main": "./dist/mcgorgeous.common.js",

@@ -5,2 +5,4 @@ # McGorgeous

[![CircleCI](https://circleci.com/gh/jschatz1/mcgorgeous.svg?style=svg)](https://circleci.com/gh/jschatz1/mcgorgeous)
## Usage

@@ -59,2 +61,16 @@

### Null type
```javascript
[{ id: null }];
```
Null type will be skipped. Can be `null` or `"null"`.
### Not Null
```javascript
[{ name: "string notnull" }];
[{ name: "number notnull" }];
[{ name: "boolean notnull" }];
```
Will throw an error if the strings value is null.
### Array of type

@@ -61,0 +77,0 @@ ```javascript

@@ -1,9 +0,1 @@

import { traverseObjects } from "./traverse";
export default function(schema, data) {
/**
* {prop: type} e.g.
* {name: String}
**/
return traverseObjects(schema, data);
}
export { traverseObjects as default } from "./traverse";

@@ -5,5 +5,7 @@ import {

arrayDifference,
toString,
isString,
isNumber,
isBoolean
isBoolean,
isUndef
} from "./util";

@@ -13,9 +15,15 @@

// JSON data won't be cyclical so we don't need to do id checking
/**
* {prop: type} e.g.
* {name: String}
**/
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) {
// On the top level we have three "types" of cases
// - Arrays
// - Objects
// - Literals
if (Array.isArray(schema)) {
// Handling arrays
if (!Array.isArray(data)) {
throw Error(

@@ -29,4 +37,5 @@ `Schema is looking for "array", data is ${JSON.stringify(data)}`

}
} else if (isSchemaAnObject) {
if (isDataAnArray || !isDataAnObject) {
} else if (isPlainObject(schema)) {
// Handling object
if (Array.isArray(data) || !isPlainObject(data)) {
throw Error(

@@ -36,2 +45,3 @@ `Schema is looking for "object", data is ${JSON.stringify(data)}`

}
//loop object

@@ -47,2 +57,3 @@ const schemaKeys = Object.keys(schema).sort();

}
let i = schemaKeys.length;

@@ -53,5 +64,15 @@ while (i--) {

} else {
switch (schema) {
const schemaDefinition = toString(schema).split(" ");
schemaDefinition.push("");
const canBeNull = schemaDefinition[1] !== "notnull";
const isUndefProblem = !canBeNull && isUndef(data);
switch (schemaDefinition[0]) {
case "":
break;
case "null":
break;
case "string":
if (!isString(data)) {
if (isUndefProblem) {
throw Error("String cannot be null");
} else if (!isString(data) && !isUndef(data)) {
throw Error(`"${data}" is not a string.`);

@@ -61,3 +82,5 @@ }

case "number":
if (!isNumber(data)) {
if (isUndefProblem) {
throw Error("Number cannot be null");
} else if (!isNumber(data) && !isUndef(data)) {
throw Error(`"${data}" is not a number.`);

@@ -67,9 +90,16 @@ }

case "boolean":
if (!isBoolean(data)) {
if (isUndefProblem) {
throw Error("Boolean cannot be null");
} else if (!isBoolean(data) && !isUndef(data)) {
throw Error(`"${data}" is not a boolean.`);
}
break;
default:
throw new Error(
`Unknown schema type: ${JSON.stringify(schemaDefinition[0])}`
);
}
}
return true;
}
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