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 1.0.3 to 1.0.4

2

index.js

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

"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 a(e,r,t){void 0===e&&(e=0),this.errcode=e,this.fieldName=r,this.innerError=t}return a.error=function(e,r,t){return new a(e,r,t)},a.innerError=function(e,r){for(var t=e.split("."),n=new a(r),i=t.length-1;-1<i;--i)n=new a(ValidateErrorCode.InnerError,t[i],n);return n},Object.defineProperty(a.prototype,"originalError",{get:function(){for(var e=[],r=this;;){if(!r.innerError){var t=new a(r.errcode);return t.fieldName=e.join("."),t}e.push(r.fieldName),r=r.innerError}},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"isSucc",{get:function(){return this.errcode===ValidateErrorCode.Succ},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"message",{get:function(){return(this.fieldName?this.fieldName+": ":"")+ValidateErrorCode[this.errcode]||"UnknownError"},enumerable:!0,configurable:!0}),a.success=new a(ValidateErrorCode.Succ),a}(),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}),i=void 0;if(n)i=n.type;else{if(!t.indexSignature)throw new Error("Error index: "+r.index);i=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}))&&(i={type:"Union",members:[{id:0,type:i},{id:1,type:{type:"Literal",literal:void 0}}]}),this.isTypeReference(i)?this.parseReference(i):i},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 i=this.parseReference(e[t]);if(this.isInterface(i)){var a=this.getFlatInterfaceSchema(i);if(a.properties.forEach(function(e){r.binaryInsert(e.name,!0)}),a.indexSignature){var o="[["+a.indexSignature.keyType+"]]";r.binaryInsert(o,!0)}}else if("Intersection"===i.type||"Union"===i.type)this.extendsUnionFields(r,i.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"}})},i=0,a=e;i<a.length;i++){r(a[i])}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,i=e.properties;n<i.length;n++){t[(y=i[n]).name]={optional:y.optional,type:y.type}}if(e.indexSignature&&(r=e.indexSignature),e.extends)for(var a=0,o=e.extends;a<o.length;a++){var p=o[a],s=this.parseReference(p.type);if("Interface"!==s.type)throw new Error("SchemaError: extends must from interface but from "+s.type);var l=this.getFlatInterfaceSchema(s);if(l.properties)for(var d=0,u=l.properties;d<u.length;d++){var y;t[(y=u[d]).name]||(t[y.name]={optional:y.optional,type:y.type})}l.indexSignature&&!r&&(r=l.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;this.isTypeReference(e.target)?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=[],i=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})}},a=0,o=e.keys;a<o.length;a++){i(o[a])}return{type:"Interface",properties:n}}if("Partial"===e.type){for(var p=0,s=t.properties;p<s.length;p++){s[p].optional=!0}return t}if("Omit"===e.type){for(var l=function(r){t.properties.removeOne(function(e){return e.name===r})},d=0,u=e.keys;d<u.length;d++){l(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.prototype.parsePickOmit=function(e){for(var r=[],t=e;r.push({type:t.type,keys:t.keys}),"Pick"===(t=this.parseReference(t.target)).type||"Omit"===t.type;);return{final:t,parents:r}},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"Omit":return this.validatePickOmitType(e,r);case"Partial":case"Overwrite":return this.validateInterfaceType(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"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.validatePickOmitType=function(e,r){var i=this.protoHelper.parsePickOmit(r);if("Interface"===i.final.type)return this.validateInterfaceType(e,r);if("Union"!==i.final.type)throw new Error("Invalid "+r.type+" final type: "+i.final.type);var t={type:"Union",members:i.final.members.map(function(e){for(var r=e.type,t=i.parents.length-1;-1<t;--t){var n=i.parents[t];r={type:n.type,keys:n.keys,target:r}}return{id:e.id,type:r}})};return this.validateBySchema(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,i=r.properties;n<i.length;n++){var a=i[n];if(void 0===e[a.name]){if(a.optional||this._canBeUndefined(a.type))continue;return ValidateResult.error(ValidateErrorCode.InnerError,a.name,ValidateResult.error(ValidateErrorCode.MissingRequiredMember))}if(!(o=this.validateBySchema(e[a.name],a.type)).isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,a.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 if(!this._options.skipExcessCheck){var p=r.properties.map(function(e){return e.name}),s=Object.keys(e).remove(function(e){return-1<p.indexOf(e)});if(s.length)return ValidateResult.error(ValidateErrorCode.InnerError,s[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.validateUnionType=function(e,r,t){t||this.protoHelper.extendsUnionFields(t=[],r.members.map(function(e){return e.type}));for(var n=0,i=r.members;n<i.length;n++){var a=i[n],o=this.protoHelper.isTypeReference(a.type)?this.protoHelper.parseReference(a.type):a.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,i=r.members.length;n<i;++n){var a=r.members[n].type;a=this.protoHelper.isTypeReference(a)?this.protoHelper.parseReference(a):a;var o=void 0;if(!(o=this.protoHelper.isInterface(a)?this.validateInterfaceReference(e,a,t):"Union"===a.type?this.validateUnionType(e,a,t):"Intersection"===a.type?this.validateIntersectionType(e,a,t):this.validateBySchema(e,a)).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;
"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 a(e,r,t){void 0===e&&(e=0),this.errcode=e,this.fieldName=r,this.innerError=t}return a.error=function(e,r,t){return new a(e,r,t)},a.innerError=function(e,r){for(var t=e.split("."),n=new a(r),i=t.length-1;-1<i;--i)n=new a(ValidateErrorCode.InnerError,t[i],n);return n},Object.defineProperty(a.prototype,"originalError",{get:function(){for(var e=[],r=this;;){if(!r.innerError){var t=new a(r.errcode);return t.fieldName=e.join("."),t}e.push(r.fieldName),r=r.innerError}},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"isSucc",{get:function(){return this.errcode===ValidateErrorCode.Succ},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"message",{get:function(){return(this.fieldName?this.fieldName+": ":"")+ValidateErrorCode[this.errcode]||"UnknownError"},enumerable:!0,configurable:!0}),a.success=new a(ValidateErrorCode.Succ),a}(),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}),i=void 0;if(n)i=n.type;else{if(!t.indexSignature)throw new Error("Error index: "+r.index);i=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}))&&(i={type:"Union",members:[{id:0,type:i},{id:1,type:{type:"Literal",literal:void 0}}]}),this.isTypeReference(i)?this.parseReference(i):i},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 i=this.parseReference(e[t]);if(this.isInterface(i)){var a=this.getFlatInterfaceSchema(i);if(a.properties.forEach(function(e){r.binaryInsert(e.name,!0)}),a.indexSignature){var o="[["+a.indexSignature.keyType+"]]";r.binaryInsert(o,!0)}}else if("Intersection"===i.type||"Union"===i.type)this.extendsUnionFields(r,i.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"}})},i=0,a=e;i<a.length;i++){r(a[i])}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,i=e.properties;n<i.length;n++){t[(y=i[n]).name]={optional:y.optional,type:y.type}}if(e.indexSignature&&(r=e.indexSignature),e.extends)for(var a=0,o=e.extends;a<o.length;a++){var p=o[a],s=this.parseReference(p.type);if("Interface"!==s.type)throw new Error("SchemaError: extends must from interface but from "+s.type);var l=this.getFlatInterfaceSchema(s);if(l.properties)for(var u=0,d=l.properties;u<d.length;u++){var y;t[(y=d[u]).name]||(t[y.name]={optional:y.optional,type:y.type})}l.indexSignature&&!r&&(r=l.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;this.isTypeReference(e.target)?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=[],i=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})}},a=0,o=e.keys;a<o.length;a++){i(o[a])}return{type:"Interface",properties:n}}if("Partial"===e.type){for(var p=0,s=t.properties;p<s.length;p++){s[p].optional=!0}return t}if("Omit"===e.type){for(var l=function(r){t.properties.removeOne(function(e){return e.name===r})},u=0,d=e.keys;u<d.length;u++){l(d[u])}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.prototype.parsePickOmit=function(e){for(var i=[],r=e;i.push({type:r.type,keys:r.keys}),"Pick"===(r=this.parseReference(r.target)).type||"Omit"===r.type;);if("Interface"===r.type)return r;if("Union"!==r.type)throw new Error("Unsupported pattern "+e.type+"<"+r.type+">");return{type:"Union",members:r.members.map(function(e){for(var r=e.type,t=i.length-1;-1<t;--t){var n=i[t];r={type:n.type,keys:n.keys,target:r}}return{id:e.id,type:r}})}},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"Omit":return this.validatePickOmitType(e,r);case"Partial":case"Overwrite":return this.validateInterfaceType(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"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.validatePickOmitType=function(e,r){var t=this.protoHelper.parsePickOmit(r);if("Interface"===t.type)return this.validateInterfaceType(e,r);if("Union"===t.type)return this.validateUnionType(e,t);throw new Error("Unsupported "+r.type+"<"+t.type+">")},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,i=r.properties;n<i.length;n++){var a=i[n];if(void 0===e[a.name]){if(a.optional||this._canBeUndefined(a.type))continue;return ValidateResult.error(ValidateErrorCode.InnerError,a.name,ValidateResult.error(ValidateErrorCode.MissingRequiredMember))}if(!(o=this.validateBySchema(e[a.name],a.type)).isSucc)return ValidateResult.error(ValidateErrorCode.InnerError,a.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 if(!this._options.skipExcessCheck){var p=r.properties.map(function(e){return e.name}),s=Object.keys(e).remove(function(e){return-1<p.indexOf(e)});if(s.length)return ValidateResult.error(ValidateErrorCode.InnerError,s[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.validateUnionType=function(e,r,t){t||this.protoHelper.extendsUnionFields(t=[],r.members.map(function(e){return e.type}));for(var n=0,i=r.members;n<i.length;n++){var a=i[n],o=this.protoHelper.isTypeReference(a.type)?this.protoHelper.parseReference(a.type):a.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,i=r.members.length;n<i;++n){var a=r.members[n].type;a=this.protoHelper.isTypeReference(a)?this.protoHelper.parseReference(a):a;var o=void 0;if(!(o=this.protoHelper.isInterface(a)?this.validateInterfaceReference(e,a,t):"Union"===a.type?this.validateUnionType(e,a,t):"Intersection"===a.type?this.validateIntersectionType(e,a,t):this.validateBySchema(e,a)).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;
{
"name": "tsbuffer-validator",
"version": "1.0.3",
"version": "1.0.4",
"description": "Validator for TSBuffer values",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -34,6 +34,3 @@ import { TSBufferProto, TSBufferSchema } from "tsbuffer-schema";

private _flattenMappedType;
parsePickOmit(schema: PickTypeSchema | OmitTypeSchema): {
final: Exclude<TSBufferSchema, TypeReference | PickTypeSchema | OmitTypeSchema>;
parents: (Omit<PickTypeSchema | OmitTypeSchema, 'target'>)[];
};
parsePickOmit(schema: PickTypeSchema | OmitTypeSchema): TSBufferSchema;
}

@@ -40,0 +37,0 @@ export interface FlatInterfaceTypeSchema {

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