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

Update to param validator + test file #7194

Merged
merged 6 commits into from
Aug 30, 2024

Conversation

sproutleaf
Copy link

Addresses #7178

Changes:

  • Validate against real p5 constants
  • Fully account for optional parameters
  • Add new test file for param validator

Screenshots of the change:

Ran npm run test:dev locally and all the tests now pass

Screenshot 2024-08-27 at 11 05 41 AM

Note:
I have yet to implement parameter validation that requires a browser environment (i.e. for web API objects and p5 objects), that will be done in a follow-up PR.

PR Checklist

Copy link
Contributor

@davepagurek davepagurek 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!

// ...
// }
const constantsMap = {};
for (const [key, value] of Object.entries(constants)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a thought for our module restructuring effort, @limzykenneth when we're thinking of moving constants into their relevant modules instead of one big file, will we have to do anything special to continue supporting getting a big list of all of them?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm...that's a good question. It might be that module need to specifically register things as constants but I want to avoid that where possible to simplify things.

Copy link
Member

@limzykenneth limzykenneth Aug 28, 2024

Choose a reason for hiding this comment

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

Is it possible to solely rely on the documentation parsed data to get this list? We can possibly add extra tags or into into the JSDoc comments if needed to mark something as a constant.

Copy link
Contributor

Choose a reason for hiding this comment

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

It might not be possibly to get it from the parameter data because the types only include the names of the constants, and we won't know from that what value they correspond to (like what specific symbol to check against)

Copy link
Contributor

Choose a reason for hiding this comment

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

If we switch back to using strings, I think it could be doable though?

Copy link
Member

Choose a reason for hiding this comment

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

We can possibly switch back to strings, the symbol stuff I'm not 100% sold on yet as I'm also not sure how or if it should work across multiple instances.

return z.union(types
.filter(t => {
if (!schemaMap[t]) {
console.log(`Warning: Zod schema not found for type '${t}'. Skip mapping`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should make this throw an error so that it's obvious when we add a method that doesn't have a Zod schema yet?

Copy link
Author

Choose a reason for hiding this comment

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

Yes absolutely! I used console.log right now because many objects are not supported yet. Added a TODO here for future reference!

// parameters that allow multiple types.
schema = types.every(t => /^[A-Z_]+$/.test(t))
? generateConstantSchema(types)
: generateUnionSchema(types);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this case also handle constants if they exist? e.g. something like Number|AUTO maybe? If not, maybe we can use generateUnionSchema for everything and update its map to map to a z.literal if a type is a constant.

Copy link
Author

Choose a reason for hiding this comment

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

That's a great point! I just added another commit that changes the logic of generating schema for each parameter and moved things around accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

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

nice, looks good!

@davepagurek davepagurek merged commit 914e0a5 into processing:dev-2.0 Aug 30, 2024
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants