Skip to content

Commit

Permalink
Add jsonpath package and tests for schema
Browse files Browse the repository at this point in the history
  • Loading branch information
theref committed Jul 30, 2024
1 parent be45583 commit 4546b2f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/taco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@nucypher/nucypher-core": "*",
"@nucypher/shared": "workspace:*",
"ethers": "^5.7.2",
"jsonpath-plus": "^9.0.0",
"semver": "^7.5.2",
"zod": "^3.22.4"
},
Expand Down
15 changes: 14 additions & 1 deletion packages/taco/src/conditions/base/json-api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JSONPath } from 'jsonpath-plus';
import { z } from 'zod';

import { Condition } from '../condition';
Expand All @@ -12,7 +13,19 @@ export const JsonApiConditionSchema = z.object({
conditionType: z.literal(JsonApiConditionType).default(JsonApiConditionType),
endpoint: z.string().url(),
parameters: z.record(z.string(), z.unknown()).optional(),
query: z.string().optional(),
query: z.string().refine(
(path) => {
try {
JSONPath.toPathArray(path);
return true;
} catch (error) {
return false;
}
},
{
message: "Invalid JSON path",
}
),
returnValueTest: returnValueTestSchema, // Update to allow multiple return values after expanding supported methods
});

Expand Down
17 changes: 16 additions & 1 deletion packages/taco/test/conditions/base/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,22 @@ describe('JsonApiCondition', () => {
},
});
});

it('rejects an invalid json path', () => {
const badJsonApiObj = {
...testJsonApiConditionObj,
query: '$.store.book[?(@.price < ]',
};

const result = JsonApiCondition.validate(JsonApiConditionSchema, badJsonApiObj);

expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
endpoint: {
_errors: ['Invalid JSON path'],
},
});
});
describe('parameters', () => {
it('accepts conditions without query path', () => {
const { query, ...noQueryObj} = testJsonApiConditionObj;
Expand Down
95 changes: 86 additions & 9 deletions pnpm-lock.yaml

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

0 comments on commit 4546b2f

Please sign in to comment.