Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSONPath syntactic validation #561

Merged

Conversation

andresceballosm
Copy link

@andresceballosm andresceballosm commented Jul 30, 2024

Type of PR:

  • Bugfix
  • Feature
  • Documentation
  • Other

Required reviews:

  • 1
  • 2
  • 3

What this does:

Added JSONPath syntactic validation

Related to https://github.com/nucypher/taco-web/pull/550/files#r1696888086

Issues fixed/closed:

Why it's needed:

Explain how this PR fits in the greater context of the NuCypher Network. E.g.,
if this PR address a nucypher/productdev issue, let reviewers know!

Notes for reviewers:

This depends on the PR #550
With the integrated PR #550 my approach is to use it in the following way

export const JsonApiConditionSchema = z.object({
  conditionType: z.literal(JsonApiConditionType).default(JsonApiConditionType),
  endpoint: z.string().url(),
  parameters: z.record(z.string(), z.unknown()).optional(),
  query: jsonPathSchema.optional(),
  returnValueTest: returnValueTestSchema, // Update to allow multiple return values after expanding supported methods
});

Copy link

netlify bot commented Jul 30, 2024

Deploy Preview for taco-demo ready!

Name Link
🔨 Latest commit eb78af3
🔍 Latest deploy log https://app.netlify.com/sites/taco-demo/deploys/66a94f7087382a0008a2a1e6
😎 Deploy Preview https://deploy-preview-561--taco-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jul 30, 2024

Deploy Preview for taco-nft-demo ready!

Name Link
🔨 Latest commit eb78af3
🔍 Latest deploy log https://app.netlify.com/sites/taco-nft-demo/deploys/66a94f706e363800087b3176
😎 Deploy Preview https://deploy-preview-561--taco-nft-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@derekpierre derekpierre changed the base branch from main to epic-offchain July 30, 2024 20:52
Copy link
Contributor

@theref theref left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thank you for this. I thought the jsonpath package was deprecated now though? and has been replaced by jsonpath-plus

@@ -43,11 +43,13 @@
"@nucypher/nucypher-core": "*",
"@nucypher/shared": "workspace:*",
"ethers": "^5.7.2",
"jsonpath": "^1.1.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @theref noted, this package seems to be no longer maintained. jsonpath-plus is in maintenance mode:

Please note: This project is not currently being actively maintained. We may accept well-documented PRs or some simple updates, but are not looking to make fixes or add new features ourselves.

We may want to switch to jsonpath-plus or https://www.npmjs.com/package/nimma

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the same message on the JSONPath-plus GitHub (https://github.com/JSONPath-Plus/JSONPath)

Screenshot 2024-07-31 at 14 42 17

This messaging also appears to be reiterated in this comment on May 15, 2024 JSONPath-Plus/JSONPath#173 (comment)

As mentioned on the main page of the README, I am not actively maintaining this project, though I am accepting PRs. Feel free to submit a well-documented PR if you find an issue and your question is not answered by the README.

Copy link
Member

@KPrasch KPrasch Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, since there appears to be no actively maintained jsonpath package, if the original jsonpath package is functioning as expected (demonstrated in this PR) I'm in favor of merging it as-is if we can get CI to pass.

After all, the only functionality we actually need is the syntactic check and not the evaluation. If jsonpath supports that but jsonpath-plus does not I don't think we have another way to implement this validator without building our own regular expressions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to various issues with jsonpath and jsonpath-plus not functioning as expected, I decided to implement our own JSONPath validator.

@derekpierre
Copy link
Member

derekpierre commented Jul 31, 2024

@derekpierre
Copy link
Member

@andresceballosm can you try using https://www.npmjs.com/package/@astronautlabs/jsonpath (https://github.com/astronautlabs/jsonpath) instead of jsonpath? I did a cursory check locally and it seemed to do the trick.

@andresceballosm
Copy link
Author

@andresceballosm can you try using https://www.npmjs.com/package/@astronautlabs/jsonpath (https://github.com/astronautlabs/jsonpath) instead of jsonpath? I did a cursory check locally and it seemed to do the trick.

Hey @derekpierre works great! thanks

const invalidPath = '$.store.book[?(@.price < ]';
const result = jsonPathSchema.safeParse(invalidPath);
expect(result.success).toBe(false);
if (!result.success) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these if statements necessary? If not, let's remove them. We know result.success is false from the expect in the line above. Same comment for other tests below.

Comment on lines 10 to 14
if (!result.success) {
expect(result.error.errors[0].message).toBe(
'Invalid JSONPath expression',
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not being more clear. I meant to keep the expect test but remove the if i.e. keep only

      expect(result.error.errors[0].message).toBe(
        'Invalid JSONPath expression',
      );

Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸 - nice work!

@derekpierre derekpierre merged commit d59110a into nucypher:epic-offchain Aug 2, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Syntactic validation of JSONPath queries while creating conditions
5 participants