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

tsbuffer-validator

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsbuffer-validator - npm Package Compare versions

Comparing version 0.4.3 to 0.5.0

6

index.js

@@ -1,5 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("k8w-extend-native");
var TSBufferValidator_1 = require("./src/TSBufferValidator");
exports.TSBufferValidator = TSBufferValidator_1.TSBufferValidator;
"use strict";var ValidateErrorCode;Object.defineProperty(exports,"__esModule",{value:!0}),require("k8w-extend-native"),function(e){e[e.Succ=0]="Succ",e[e.WrongType=1]="WrongType",e[e.InnerError=2]="InnerError",e[e.WrongScalarType=3]="WrongScalarType",e[e.TupleOverlength=4]="TupleOverlength",e[e.InvalidEnumValue=5]="InvalidEnumValue",e[e.AnyTypeCannotBeArrayBuffer=6]="AnyTypeCannotBeArrayBuffer",e[e.AnyTypeCannotBeTypedArray=7]="AnyTypeCannotBeTypedArray",e[e.InvalidLiteralValue=8]="InvalidLiteralValue",e[e.MissingRequiredMember=9]="MissingRequiredMember",e[e.UnexpectedField=10]="UnexpectedField",e[e.InvalidNumberKey=11]="InvalidNumberKey",e[e.ExtendsMustBeInterface=12]="ExtendsMustBeInterface",e[e.InvalidBufferArrayType=13]="InvalidBufferArrayType",e[e.SchemaError=14]="SchemaError",e[e.NonConditionMet=15]="NonConditionMet"}(ValidateErrorCode||(ValidateErrorCode={}));var ValidateResult=function(){function i(e,r,t){void 0===e&&(e=0),this.errcode=e,this.fieldName=r,this.innerError=t}return i.error=function(e,r,t){return new i(e,r,t)},i.innerError=function(e,r){for(var t=e.split("."),n=new i(r),a=t.length-1;-1<a;--a)n=new i(ValidateErrorCode.InnerError,t[a],n);return n},Object.defineProperty(i.prototype,"originalError",{get:function(){for(var e=[],r=this;;){if(!r.innerError){var t=new i(r.errcode);return t.fieldName=e.join("."),t}e.push(r.fieldName),r=r.innerError}},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"isSucc",{get:function(){return this.errcode===ValidateErrorCode.Succ},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"message",{get:function(){return(this.fieldName?this.fieldName+": ":"")+ValidateErrorCode[this.errcode]||"UnknownError"},enumerable:!0,configurable:!0}),i.success=new i(ValidateErrorCode.Succ),i}(),ProtoHelper=function(){function e(e){this._proto=e}return Object.defineProperty(e.prototype,"proto",{get:function(){return this._proto},enumerable:!0,configurable:!0}),e.prototype.parseReference=function(r){if("Reference"===r.type){var e=this._proto[r.target];if(!e)throw new Error("Cannot find reference target: "+r.target);return this.isTypeReference(e)?this.parseReference(e):e}if("IndexedAccess"!==r.type)return r;if(!this.isInterface(r.objectType))throw new Error("Error objectType: "+r.objectType.type);var t=this.getFlatInterfaceSchema(r.objectType),n=t.properties.find(function(e){return e.name===r.index}),a=void 0;if(n)a=n.type;else{if(!t.indexSignature)throw new Error("Error index: "+r.index);a=t.indexSignature.type}return n&&n.optional&&("Union"!==n.type.type||-1===n.type.members.findIndex(function(e){return"Literal"===e.type.type&&void 0===e.type.literal}))&&(a={type:"Union",members:[{id:0,type:a},{id:1,type:{type:"Literal",literal:void 0}}]}),this.isTypeReference(a)?this.parseReference(a):a},e.prototype.isInterface=function(e,r){if(void 0===r&&(r=!1),r||!this.isTypeReference(e))return"Interface"===e.type||"Pick"===e.type||"Partial"===e.type||"Omit"===e.type||"Overwrite"===e.type;var t=this.parseReference(e);return this.isInterface(t,r)},e.prototype.isTypeReference=function(e){return"Reference"===e.type||"IndexedAccess"===e.type},e.prototype.extendsUnionFields=function(r,e){for(var t=0,n=e.length;t<n;++t){var a=e[t];if(this.isTypeReference(a)&&(a=this.parseReference(a)),this.isInterface(a)){var i=this.getFlatInterfaceSchema(a);if(i.properties.forEach(function(e){-1===r.binarySearch(e.name)&&r.binaryInsert(e.name)}),i.indexSignature){var o="[["+i.indexSignature.keyType+"]]";-1===r.binarySearch(o)&&r.binaryInsert(o)}}else if("Intersection"===a.type||"Union"===a.type)this.extendsUnionFields(r,a.members.map(function(e){return e.type}))}},e.prototype.extendUnionFieldsToInterface=function(t,e){for(var n=[],r=function(r){t.properties.find(function(e){return e.name===r})||n.push({id:-1,name:r,optional:!0,type:{type:"Any"}})},a=0,i=e;a<i.length;a++){r(i[a])}n.forEach(function(e){t.properties.push(e)}),t.indexSignature||(-1<e.binarySearch("[[String]]")?t.indexSignature={keyType:"String",type:{type:"Any"}}:-1<e.binarySearch("[[Number]]")&&(t.indexSignature={keyType:"Number",type:{type:"Any"}}))},e.prototype.getFlatInterfaceSchema=function(e){if(this.isTypeReference(e)){var r=this.parseReference(e);if("Interface"!==r.type)throw new Error("Cannot flatten non interface type: "+r.type);return this.getFlatInterfaceSchema(r)}return"Interface"===e.type?this._flattenInterface(e):this._flattenMappedType(e)},e.prototype._flattenInterface=function(e){var r,t={};if(e.properties)for(var n=0,a=e.properties;n<a.length;n++){t[(y=a[n]).name]={optional:y.optional,type:y.type}}if(e.indexSignature&&(r=e.indexSignature),e.extends)for(var i=0,o=e.extends;i<o.length;i++){var p=o[i],l=this.parseReference(p.type);if("Interface"!==l.type)throw new Error("SchemaError: extends must from interface but from "+l.type);var s=this.getFlatInterfaceSchema(l);if(s.properties)for(var d=0,u=s.properties;d<u.length;d++){var y;t[(y=u[d]).name]||(t[y.name]={optional:y.optional,type:y.type})}s.indexSignature&&!r&&(r=s.indexSignature)}return{type:"Interface",properties:Object.entries(t).map(function(e,r){return{id:r,name:e[0],optional:e[1].optional,type:e[1].type}}),indexSignature:r}},e.prototype._flattenMappedType=function(e){var r,t;"Reference"===e.target.type?r=this.parseReference(e.target):r=e.target;if("Pick"===r.type||"Partial"===r.type||"Omit"===r.type||"Overwrite"===r.type)t=this._flattenMappedType(r);else{if("Interface"!==r.type)throw new Error("Invalid target.type: "+r.type);t=this._flattenInterface(r)}if("Pick"===e.type){for(var n=[],a=function(r){var e=t.properties.find(function(e){return e.name===r});if(e)n.push({id:n.length,name:r,optional:e.optional,type:e.type});else{if(!t.indexSignature)throw new Error("Cannot find pick key ["+r+"]");n.push({id:n.length,name:r,type:t.indexSignature.type})}},i=0,o=e.keys;i<o.length;i++){a(o[i])}return{type:"Interface",properties:n}}if("Partial"===e.type){for(var p=0,l=t.properties;p<l.length;p++){l[p].optional=!0}return t}if("Omit"===e.type){for(var s=function(r){t.properties.removeOne(function(e){return e.name===r})},d=0,u=e.keys;d<u.length;d++){s(u[d])}return t}if("Overwrite"!==e.type)throw new Error("Unknown type: "+e.type);var y=this.getFlatInterfaceSchema(e.overwrite);y.indexSignature&&(t.indexSignature=y.indexSignature);for(var c=function(r){t.properties.removeOne(function(e){return e.name===r.name}),t.properties.push(r)},f=0,h=y.properties;f<h.length;f++){c(h[f])}return t},e}(),typedArrays={Int8Array:Int8Array,Int16Array:Int16Array,Int32Array:Int32Array,BigInt64Array:"undefined"!=typeof BigInt64Array?BigInt64Array:void 0,Uint8Array:Uint8Array,Uint16Array:Uint16Array,Uint32Array:Uint32Array,BigUint64Array:"undefined"!=typeof BigUint64Array?BigUint64Array:void 0,Float32Array:Float32Array,Float64Array:Float64Array},TSBufferValidator=function(){function e(e,r){this._options={},this._proto=e,r&&Object.assign(this._options,r),this.protoHelper=new ProtoHelper(e)}return e.prototype.validate=function(e,r){var t=this._proto[r];if(!t)throw new Error("Cannot find schema ["+r+"]");return this.validateBySchema(e,t)},e.prototype.validateBySchema=function(e,r){switch(r.type){case"Boolean":return this.validateBooleanType(e);case"Number":return this.validateNumberType(e,r);case"String":return this.validateStringType(e);case"Array":return this.validateArrayType(e,r);case"Tuple":return this.validateTupleType(e,r);case"Enum":return this.validateEnumType(e,r);case"Any":return this.validateAnyType(e);case"Literal":return this.validateLiteralType(e,r);case"NonPrimitive":return this.validateNonPrimitiveType(e);case"Interface":return this.validateInterfaceType(e,r);case"Buffer":return this.validateBufferType(e,r);case"IndexedAccess":return this.validateIndexedAccessType(e,r);case"Reference":return this.validateReferenceType(e,r);case"Union":return this.validateUnionType(e,r);case"Intersection":return this.validateIntersectionType(e,r);case"Pick":case"Partial":case"Omit":case"Overwrite":return this.validateMappedType(e,r);default:throw new Error("Unrecognized schema type: "+r.type)}},e.prototype.validateBooleanType=function(e){return"boolean"==typeof e?ValidateResult.success:ValidateResult.error(ValidateErrorCode.WrongType)},e.prototype.validateNumberType=function(e,r){if("number"!=typeof e&&"bigint"!=typeof e)return ValidateResult.error(ValidateErrorCode.WrongType);var t=r.scalarType||"double";return"float"===t||"double"===t||"number"!=typeof e||Number.isInteger(e)?-1<t.indexOf("uint")&&e<0?ValidateResult.error(ValidateErrorCode.WrongScalarType):-1===t.indexOf("big")&&"bigint"==typeof e?ValidateResult.error(ValidateErrorCode.WrongScalarType):-1<t.indexOf("big")&&"bigint"!=typeof e?ValidateResult.error(ValidateErrorCode.WrongScalarType):ValidateResult.success:ValidateResult.error(ValidateErrorCode.WrongScalarType)},e.prototype.validateStringType=function(e){return"string"==typeof e?ValidateResult.success:ValidateResult.error(ValidateErrorCode.WrongType)},e.prototype.validateArrayType=function(e,r){if(!Array.isArray(e))return ValidateResult.error(ValidateErrorCode.WrongType);for(var t=0;t<e.length;++t){var n=this.validateBySchema(e[t],r.elementType);if(!n.isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,""+t,n)}return ValidateResult.success},e.prototype.validateTupleType=function(e,r){if(!Array.isArray(e))return ValidateResult.error(ValidateErrorCode.WrongType);if(e.length>r.elementTypes.length)return ValidateResult.error(ValidateErrorCode.TupleOverlength);for(var t=0;t<r.elementTypes.length;++t){if(void 0===e[t]){if(void 0!==r.optionalStartIndex&&t>=r.optionalStartIndex||this._canBeUndefined(r.elementTypes[t]))continue;return ValidateResult.error(ValidateErrorCode.InnerError,""+t,ValidateResult.error(ValidateErrorCode.MissingRequiredMember))}var n=this.validateBySchema(e[t],r.elementTypes[t]);if(!n.isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,""+t,n)}return ValidateResult.success},e.prototype._canBeUndefined=function(e){var r=this;return"Union"===e.type?e.members.some(function(e){return r._canBeUndefined(e.type)}):"Literal"===e.type&&void 0===e.literal},e.prototype.validateEnumType=function(r,e){return"string"!=typeof r&&"number"!=typeof r?ValidateResult.error(ValidateErrorCode.WrongType):e.members.some(function(e){return e.value===r})?ValidateResult.success:ValidateResult.error(ValidateErrorCode.InvalidEnumValue)},e.prototype.validateAnyType=function(e){return ValidateResult.success},e.prototype.validateLiteralType=function(e,r){return e===r.literal?ValidateResult.success:ValidateResult.error(ValidateErrorCode.InvalidLiteralValue)},e.prototype.validateNonPrimitiveType=function(e){return"object"==typeof e&&null!==e?ValidateResult.success:ValidateResult.error(ValidateErrorCode.WrongType)},e.prototype.validateInterfaceType=function(e,r){if("object"!=typeof e)return ValidateResult.error(ValidateErrorCode.WrongType);var t=this.protoHelper.getFlatInterfaceSchema(r);return this._validateFlatInterface(e,t)},e.prototype._validateFlatInterface=function(e,r){if(r.indexSignature&&"Number"===r.indexSignature.keyType)for(var t in e)if(!this._isNumberKey(t))return ValidateResult.error(ValidateErrorCode.InnerError,t,ValidateResult.error(ValidateErrorCode.InvalidNumberKey));if(r.properties)for(var n=0,a=r.properties;n<a.length;n++){var i=a[n];if(void 0===e[i.name]){if(i.optional||this._canBeUndefined(i.type))continue;return ValidateResult.error(ValidateErrorCode.InnerError,i.name,ValidateResult.error(ValidateErrorCode.MissingRequiredMember))}if(!(o=this.validateBySchema(e[i.name],i.type)).isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,i.name,o)}if(r.indexSignature)for(var t in e){var o;if(!(o=this.validateBySchema(e[t],r.indexSignature.type)).isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,t,o)}else{var p=r.properties.map(function(e){return e.name}),l=Object.keys(e).remove(function(e){return-1<p.indexOf(e)});if(l.length)return ValidateResult.error(ValidateErrorCode.InnerError,l[0],ValidateResult.error(ValidateErrorCode.UnexpectedField))}return ValidateResult.success},e.prototype.validateBufferType=function(e,r){if(r.arrayType){var t=typedArrays[r.arrayType];if(!t)throw new Error("Error TypedArray type: "+r.arrayType);return e instanceof t?ValidateResult.success:ValidateResult.error(ValidateErrorCode.WrongType)}return e instanceof ArrayBuffer?ValidateResult.success:ValidateResult.error(ValidateErrorCode.WrongType)},e.prototype.validateIndexedAccessType=function(e,r){return this.validateBySchema(e,this.protoHelper.parseReference(r))},e.prototype.validateReferenceType=function(e,r){return this.validateBySchema(e,this.protoHelper.parseReference(r))},e.prototype.validateMappedType=function(e,r){return this.validateInterfaceType(e,this.protoHelper.getFlatInterfaceSchema(r))},e.prototype.validateUnionType=function(e,r,t){t||this.protoHelper.extendsUnionFields(t=[],r.members.map(function(e){return e.type}));for(var n=0,a=r.members;n<a.length;n++){var i=a[n],o=this.protoHelper.isTypeReference(i.type)?this.protoHelper.parseReference(i.type):i.type;if((this.protoHelper.isInterface(o)?this.validateInterfaceReference(e,o,t):"Union"===o.type?this.validateUnionType(e,o,t):"Intersection"===o.type?this.validateIntersectionType(e,o,t):this.validateBySchema(e,o)).isSucc)return ValidateResult.success}return ValidateResult.error(ValidateErrorCode.NonConditionMet)},e.prototype.validateIntersectionType=function(e,r,t){t||this.protoHelper.extendsUnionFields(t=[],r.members.map(function(e){return e.type}));for(var n=0,a=r.members.length;n<a;++n){var i=r.members[n].type;i=this.protoHelper.isTypeReference(i)?this.protoHelper.parseReference(i):i;var o=void 0;if(!(o=this.protoHelper.isInterface(i)?this.validateInterfaceReference(e,i,t):"Union"===i.type?this.validateUnionType(e,i,t):"Intersection"===i.type?this.validateIntersectionType(e,i,t):this.validateBySchema(e,i)).isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,"<Condition"+n+">",o)}return ValidateResult.success},e.prototype._isNumberKey=function(e){var r=parseInt(e);return!(isNaN(r)||""+r!==e)},e.prototype.validateInterfaceReference=function(e,r,t){var n=this.protoHelper.getFlatInterfaceSchema(r);return t&&this.protoHelper.extendUnionFieldsToInterface(n,t),this._validateFlatInterface(e,n)},e}();exports.TSBufferValidator=TSBufferValidator;

9

package.json
{
"name": "tsbuffer-validator",
"version": "0.4.3",
"version": "0.5.0",
"description": "Validator for TSBuffer values",

@@ -10,3 +10,3 @@ "main": "index.js",

"us": "npm i tsbuffer-schema@*",
"build": "rm -rf dist && tsc -d && cp package.json LICENSE README.md dist/",
"build": "rm -rf dist && npx rollup -c && cp package.json LICENSE README.md dist/",
"bp": "npm version patch && npm run build && cd dist && npm publish & cd.."

@@ -60,5 +60,8 @@ },

"nyc": "^14.1.0",
"rollup": "^1.16.6",
"rollup-plugin-typescript2": "^0.21.2",
"rollup-plugin-uglify": "^6.0.2",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
}
}
}
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