Comparing version 1.0.0 to 1.1.0
The MIT License (MIT) | ||
Copyright (c) 2014 Alexis Jacomy, Guillaume Plique | ||
Copyright (c) 2014-2016 Alexis Jacomy, Guillaume Plique, Jérémy Lesceau | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "typology", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A data validation library for Node.js and the browser.", | ||
@@ -19,3 +19,4 @@ "main": "typology.js", | ||
"Yomguithereal", | ||
"jacomyal" | ||
"jacomyal", | ||
"jlesceau" | ||
], | ||
@@ -22,0 +23,0 @@ "license": "MIT", |
@@ -39,2 +39,3 @@ # Typology | ||
// 'null', 'undefined', 'primitive' | ||
// 'map', 'set', 'weakmap', 'weakset', 'symbol' | ||
``` | ||
@@ -93,2 +94,13 @@ | ||
*Using functions to perform ad-hoc validation* | ||
```js | ||
var myCustomType = { | ||
age: 'number', | ||
name: function(v) { | ||
return v === 'Jack' || v === 'John'; | ||
} | ||
}; | ||
``` | ||
#### Validate a variable against a custom type | ||
@@ -95,0 +107,0 @@ |
/** | ||
* typology.js - A data validation library for Node.js and the browser, | ||
* | ||
* Version: 1.0.0 | ||
* Version: 1.1.0 | ||
* Sources: http://github.com/jacomyal/typology | ||
@@ -10,3 +10,6 @@ * Doc: http://github.com/jacomyal/typology#readme | ||
* -------- | ||
* Copyright © 2014 Alexis Jacomy (@jacomyal), Guillaume Plique (@Yomguithereal) | ||
* Copyright © 2014-2016 | ||
* - Alexis Jacomy (@jacomyal) | ||
* - Guillaume Plique (@Yomguithereal) | ||
* - Jérémy Lesceau (@jlesceau) | ||
* | ||
@@ -73,3 +76,10 @@ * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
'RegExp', | ||
'Date' | ||
'Date', | ||
// ES2015 | ||
'Map', | ||
'Set', | ||
'WeakMap', | ||
'WeakSet', | ||
'Symbol' | ||
]; | ||
@@ -334,2 +344,16 @@ | ||
} else if (requiredTypeOf === 'function') { | ||
var output = { | ||
expected: type, | ||
type: typeOf, | ||
value: obj | ||
}; | ||
// Just applying a function | ||
if (!type(obj)) | ||
output.error = 'The target did not pass the given test (function).'; | ||
return output; | ||
} else if (requiredTypeOf === 'array') { | ||
@@ -632,3 +656,3 @@ if (type.length !== 1) | ||
this.add('primitive', function(v) { | ||
return !v || !(v instanceof Object || typeof v === 'object'); | ||
return v !== Object(v); | ||
}); | ||
@@ -658,3 +682,3 @@ | ||
Object.defineProperty(types, 'version', { | ||
value: '1.0.0' | ||
value: '1.1.0' | ||
}); | ||
@@ -661,0 +685,0 @@ |
/** | ||
* typology - A data validation library for Node.js and the browser. | ||
* @version v1.0.0 | ||
* @version v1.1.0 | ||
* @link https://github.com/jacomyal/typology | ||
* @license MIT | ||
*/ | ||
!function(e){"use strict";function t(e){var t=this,i={};if(this.scan=function(e,o){var a,d,u,p,c,s,f,l,h,y,g=!1,v=!1,b=null===o||void 0===o?String(o):r[Object.prototype.toString.call(o)]||"object",x=null===e||void 0===e?String(e):r[Object.prototype.toString.call(e)]||"object";if("string"===x){for(a=e.replace(/^[\?\!]/,"").split(/\|/),u=a.length,d=0;u>d;d++)if(!n[a[d]]&&"undefined"==typeof i[a[d]])throw new Error("Invalid type.");for("?"===e.charAt(0)?g=!0:"!"===e.charAt(0)&&(v=!0),u=a.length,d=0;u>d;d++)if("undefined"!=typeof i[a[d]]&&("function"==typeof i[a[d]].type?i[a[d]].type.call(t,o)===!0:!this.scan(i[a[d]].type,o).error))return v?{error:'Expected a "'+e+'" but found a "'+a[d]+'".',expected:e,type:a[d],value:o}:{expected:e,type:a[d],value:o};return null===o||void 0===o?v||g?{nully:!0,expected:e,type:b,value:o}:{error:'Expected a "'+e+'" but found a "'+b+'".',expected:e,type:b,value:o}:(h=~a.indexOf("*"),y=~a.indexOf(b),v&&(h||y)?{error:'Expected a "'+e+'" but found a "'+(y?b:"*")+'".',expected:e,type:y?b:"*",value:o}:v||h||y?{expected:e,type:b,value:o}:{error:'Expected a "'+e+'" but found a "'+b+'".',expected:e,type:b,value:o})}if("object"===x){if("object"!==b)return{error:'Expected an object but found a "'+b+'".',expected:e,type:b,value:o};for(l=Object.keys(e),u=l.length,s=0,p=0;u>p;p++){if(c=this.scan(e[l[p]],o[l[p]]),c.error)return c.path=c.path?[l[p]].concat(c.path):[l[p]],c;c.nully&&s++}if(f=Object.keys(o),f.length>u-s)for(u=f.length,p=0;u>p;p++)if("undefined"==typeof e[f[p]])return{error:'Unexpected key "'+f[p]+'".',expected:e,type:b,value:o};return{expected:e,type:b,value:o}}if("array"===x){if(1!==e.length)throw new Error("Invalid type.");if("array"!==b)return{error:'Expected an array but found a "'+b+'".',expected:e,type:b,value:o};for(u=o.length,d=0;u>d;d++)if(c=this.scan(e[0],o[d]),c.error)return c.path=c.path?[d].concat(c.path):[d],c;return{expected:e,type:b,value:o}}throw new Error("Invalid type.")},this.add=function(e,t){var r,o,a,d,u,p;if(1===arguments.length){if("object"!==this.get(e))throw new Error("If types.add is called with one argument, this one has to be an object.");r=e,d=r.id,p=r.type}else{if(2!==arguments.length)throw new Error("types.add has to be called with one or two arguments.");if("string"!=typeof e||!e)throw new Error("If types.add is called with more than one argument, the first one must be the string id.");d=e,p=t}if("string"!==this.get(d)||0===d.length)throw new Error("A type requires an string id.");if(void 0!==i[d]&&"proto"!==i[d])throw new Error('The type "'+d+'" already exists.');if(n[d])throw new Error('"'+d+'" is a reserved type name.');i[d]=1,a=(r||{}).proto||[],a=Array.isArray(a)?a:[a],u={};for(o in a)void 0===i[a[o]]&&(i[a[o]]=1,u[a[o]]=1);if("function"!==this.get(p)&&!this.isValid(p))throw new Error("A type requires a valid definition. This one can be a preexistant type or else a function testing given objects.");if(i[d]=void 0===r?{id:d,type:p}:{},void 0!==r)for(o in r)i[d][o]=r[o];for(o in u)o!==d&&delete i[o];return this},this.has=function(e){return!!i[e]},this.get=function(e){return null===e||void 0===e?String(e):r[Object.prototype.toString.call(e)]||"object"},this.check=function(e,t){return!this.scan(e,t).error},this.isValid=function(e){var t,o,a,d,u,p,c=null===e||void 0===e?String(e):r[Object.prototype.toString.call(e)]||"object";if("string"===c){for(t=e.replace(/^[\?\!]/,"").split(/\|/),u=Object.keys(t),d=u.length,a=0;d>a;a++)if(!n[t[u[a]]]&&"undefined"==typeof i[t[u[a]]])return!1;return!0}if("object"===c){for(p=Object.keys(e),d=p.length,o=0;d>o;o++)if(!this.isValid(e[p[o]]))return!1;return!0}return"array"===c&&1===e.length?this.isValid(e[0]):!1},this.add("type",function(e){return this.isValid(e)}.bind(this)),this.add("primitive",function(e){return!e||!(e instanceof Object||"object"==typeof e)}),e=e||{},"object"!==this.get(e))throw Error("Invalid argument.");for(var o in e)this.add(o,e[o])}var r={},n={"*":!0};!function(){var e,t,i=["Boolean","Number","String","Object","Array","Function","Arguments","RegExp","Date"];for(e in i)t=i[e],n[t.toLowerCase()]=!0,r["[object "+t+"]"]=t.toLowerCase()}();var i=t;t.call(i),Object.defineProperty(i,"version",{value:"1.0.0"}),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=i),exports.types=i):"function"==typeof define&&define.amd?define("typology",[],function(){return i}):e.types=i}(this); | ||
!function(e){"use strict";function t(e){var t=this,i={};if(this.scan=function(e,o){var a,d,u,p,s,c,f,l,h,y,g=!1,v=!1,b=null===o||void 0===o?String(o):r[Object.prototype.toString.call(o)]||"object",x=null===e||void 0===e?String(e):r[Object.prototype.toString.call(e)]||"object";if("string"===x){for(a=e.replace(/^[\?\!]/,"").split(/\|/),u=a.length,d=0;u>d;d++)if(!n[a[d]]&&"undefined"==typeof i[a[d]])throw new Error("Invalid type.");for("?"===e.charAt(0)?g=!0:"!"===e.charAt(0)&&(v=!0),u=a.length,d=0;u>d;d++)if("undefined"!=typeof i[a[d]]&&("function"==typeof i[a[d]].type?i[a[d]].type.call(t,o)===!0:!this.scan(i[a[d]].type,o).error))return v?{error:'Expected a "'+e+'" but found a "'+a[d]+'".',expected:e,type:a[d],value:o}:{expected:e,type:a[d],value:o};return null===o||void 0===o?v||g?{nully:!0,expected:e,type:b,value:o}:{error:'Expected a "'+e+'" but found a "'+b+'".',expected:e,type:b,value:o}:(h=~a.indexOf("*"),y=~a.indexOf(b),v&&(h||y)?{error:'Expected a "'+e+'" but found a "'+(y?b:"*")+'".',expected:e,type:y?b:"*",value:o}:v||h||y?{expected:e,type:b,value:o}:{error:'Expected a "'+e+'" but found a "'+b+'".',expected:e,type:b,value:o})}if("object"===x){if("object"!==b)return{error:'Expected an object but found a "'+b+'".',expected:e,type:b,value:o};for(l=Object.keys(e),u=l.length,c=0,p=0;u>p;p++){if(s=this.scan(e[l[p]],o[l[p]]),s.error)return s.path=s.path?[l[p]].concat(s.path):[l[p]],s;s.nully&&c++}if(f=Object.keys(o),f.length>u-c)for(u=f.length,p=0;u>p;p++)if("undefined"==typeof e[f[p]])return{error:'Unexpected key "'+f[p]+'".',expected:e,type:b,value:o};return{expected:e,type:b,value:o}}if("function"===x){var w={expected:e,type:b,value:o};return e(o)||(w.error="The target did not pass the given test (function)."),w}if("array"===x){if(1!==e.length)throw new Error("Invalid type.");if("array"!==b)return{error:'Expected an array but found a "'+b+'".',expected:e,type:b,value:o};for(u=o.length,d=0;u>d;d++)if(s=this.scan(e[0],o[d]),s.error)return s.path=s.path?[d].concat(s.path):[d],s;return{expected:e,type:b,value:o}}throw new Error("Invalid type.")},this.add=function(e,t){var r,o,a,d,u,p;if(1===arguments.length){if("object"!==this.get(e))throw new Error("If types.add is called with one argument, this one has to be an object.");r=e,d=r.id,p=r.type}else{if(2!==arguments.length)throw new Error("types.add has to be called with one or two arguments.");if("string"!=typeof e||!e)throw new Error("If types.add is called with more than one argument, the first one must be the string id.");d=e,p=t}if("string"!==this.get(d)||0===d.length)throw new Error("A type requires an string id.");if(void 0!==i[d]&&"proto"!==i[d])throw new Error('The type "'+d+'" already exists.');if(n[d])throw new Error('"'+d+'" is a reserved type name.');i[d]=1,a=(r||{}).proto||[],a=Array.isArray(a)?a:[a],u={};for(o in a)void 0===i[a[o]]&&(i[a[o]]=1,u[a[o]]=1);if("function"!==this.get(p)&&!this.isValid(p))throw new Error("A type requires a valid definition. This one can be a preexistant type or else a function testing given objects.");if(i[d]=void 0===r?{id:d,type:p}:{},void 0!==r)for(o in r)i[d][o]=r[o];for(o in u)o!==d&&delete i[o];return this},this.has=function(e){return!!i[e]},this.get=function(e){return null===e||void 0===e?String(e):r[Object.prototype.toString.call(e)]||"object"},this.check=function(e,t){return!this.scan(e,t).error},this.isValid=function(e){var t,o,a,d,u,p,s=null===e||void 0===e?String(e):r[Object.prototype.toString.call(e)]||"object";if("string"===s){for(t=e.replace(/^[\?\!]/,"").split(/\|/),u=Object.keys(t),d=u.length,a=0;d>a;a++)if(!n[t[u[a]]]&&"undefined"==typeof i[t[u[a]]])return!1;return!0}if("object"===s){for(p=Object.keys(e),d=p.length,o=0;d>o;o++)if(!this.isValid(e[p[o]]))return!1;return!0}return"array"===s&&1===e.length?this.isValid(e[0]):!1},this.add("type",function(e){return this.isValid(e)}.bind(this)),this.add("primitive",function(e){return e!==Object(e)}),e=e||{},"object"!==this.get(e))throw Error("Invalid argument.");for(var o in e)this.add(o,e[o])}var r={},n={"*":!0};!function(){var e,t,i=["Boolean","Number","String","Object","Array","Function","Arguments","RegExp","Date","Map","Set","WeakMap","WeakSet","Symbol"];for(e in i)t=i[e],n[t.toLowerCase()]=!0,r["[object "+t+"]"]=t.toLowerCase()}();var i=t;t.call(i),Object.defineProperty(i,"version",{value:"1.1.0"}),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=i),exports.types=i):"function"==typeof define&&define.amd?define("typology",[],function(){return i}):e.types=i}(this); |
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
33182
684
211