From d050cf6f88cdbfda2c9d666a65fa317daf0a0bc1 Mon Sep 17 00:00:00 2001 From: Dimasik Kolezhniuk Date: Wed, 28 Aug 2024 10:53:20 +0200 Subject: [PATCH] Fix value array size validation (#259) * Fix value array size validation * 1.17.6 --- package-lock.json | 4 +-- package.json | 2 +- src/circuits/models.ts | 67 ++++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 42 deletions(-) diff --git a/package-lock.json b/package-lock.json index ec64ca9d..323da623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.17.5", + "version": "1.17.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@0xpolygonid/js-sdk", - "version": "1.17.5", + "version": "1.17.6", "license": "MIT or Apache-2.0", "dependencies": { "@noble/curves": "^1.4.0", diff --git a/package.json b/package.json index ef0b4923..2fef0440 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.17.5", + "version": "1.17.6", "description": "SDK to work with Polygon ID", "main": "dist/node/cjs/index.js", "module": "dist/node/esm/index.js", diff --git a/src/circuits/models.ts b/src/circuits/models.ts index ab089b1e..a0fc98e6 100644 --- a/src/circuits/models.ts +++ b/src/circuits/models.ts @@ -53,45 +53,34 @@ export class Query { } validateValueArraySize(maxArrSize: number): void { - switch (this.values.length) { - case 0: { - const zeroArrSizeOps = [Operators.NOOP, Operators.SD, Operators.NULLIFY]; - if (!zeroArrSizeOps.includes(this.operator)) { - throw new Error(CircuitError.InvalidValuesArrSize); - } - return; - } - case 1: { - const oneArrSizeOps = [ - Operators.EQ, - Operators.LT, - Operators.GT, - Operators.NE, - Operators.LTE, - Operators.GTE, - Operators.EXISTS - ]; - if (!oneArrSizeOps.includes(this.operator)) { - throw new Error(CircuitError.InvalidValuesArrSize); - } - return; - } - case 2: { - const twoArrSizeOps = [Operators.BETWEEN, Operators.NONBETWEEN]; - if (!twoArrSizeOps.includes(this.operator)) { - throw new Error(CircuitError.InvalidValuesArrSize); - } - return; - } - default: { - const maxArrSizeOps = [Operators.IN, Operators.NIN]; - if (!maxArrSizeOps.includes(this.operator)) { - throw new Error(CircuitError.InvalidOperationType); - } - if (!this.values.length || this.values.length > maxArrSize) { - throw new Error(CircuitError.InvalidValuesArrSize); - } - } + if ( + [Operators.NOOP, Operators.SD, Operators.NULLIFY].includes(this.operator) && + this.values.length !== 0 + ) { + throw new Error(CircuitError.InvalidValuesArrSize); + } else if ( + [ + Operators.EQ, + Operators.LT, + Operators.GT, + Operators.NE, + Operators.LTE, + Operators.GTE, + Operators.EXISTS + ].includes(this.operator) && + this.values.length !== 1 + ) { + throw new Error(CircuitError.InvalidValuesArrSize); + } else if ( + [Operators.BETWEEN, Operators.NONBETWEEN].includes(this.operator) && + this.values.length !== 2 + ) { + throw new Error(CircuitError.InvalidValuesArrSize); + } else if ( + [Operators.IN, Operators.NIN].includes(this.operator) && + this.values.length > maxArrSize + ) { + throw new Error(CircuitError.InvalidValuesArrSize); } } }