Skip to content

Commit

Permalink
Fix value array size validation (#259)
Browse files Browse the repository at this point in the history
* Fix value array size validation

* 1.17.6
  • Loading branch information
Kolezhniuk authored Aug 28, 2024
1 parent 9d2fc4f commit d050cf6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
67 changes: 28 additions & 39 deletions src/circuits/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit d050cf6

Please sign in to comment.