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 param validator and test file #7268

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

sproutleaf
Copy link

@sproutleaf sproutleaf commented Sep 15, 2024

Addresses #7178

Changes:

  • Param validator now validates against real p5 objects and web API objects
  • Updated test file to include all the test cases covered in the old param validator file and some more (i.e. testing functions from classes outside of p5, testing web API objects)
  • Used the new modular syntax and dependency injection for param validator
  • Param validator now handles array-type arguments dynamically

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, this is looking good!

function validateParams(p5, fn) {
// Cache for Zod schemas
let schemaRegistry = new Map();
const arrDoc = JSON.parse(JSON.stringify(dataDoc));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this duplication because we modify the entries?

Copy link
Author

Choose a reason for hiding this comment

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

What do you mean by duplication here?

Copy link
Contributor

Choose a reason for hiding this comment

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

the JSON.parse(JSON.stringify(...)) bit, normally I see that used to make a deep copy of an object

Copy link
Member

Choose a reason for hiding this comment

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

If there is a need for duplicating object use structuredClone()

Copy link
Contributor

@davepagurek davepagurek Sep 15, 2024

Choose a reason for hiding this comment

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

We also might not need to do a clone at all here if we're only reading the data and not modifying it later

Copy link
Author

Choose a reason for hiding this comment

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

Ahh yes! I actually got this line of code from the old parameter validation and didn't think too much about it. Changed the code so that it uses dataDoc directly.

overloads = funcInfo.overloads;
// `constantsMap` maps constants to their values, e.g.
// {
// ADD: 'lighter',
Copy link
Contributor

Choose a reason for hiding this comment

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

These examples in the comments are really helpful, nice!

'Number[]': z.array(z.number()),
'Object': z.object({}),
// Allows string for any regex
'RegExp': z.string(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to also allow RegExp instances here too?

Copy link
Author

Choose a reason for hiding this comment

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

Not sure what the usage of RegExp instances was like previously, happy to add it if it's good to include!

@limzykenneth

Copy link
Member

Choose a reason for hiding this comment

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

What I can find is RegExp is used in p5.Table.matchRow(), there may be more places but not many. To check for regex properly it probably should be checking whether the argument is an instance of RegExp.

Copy link
Author

@sproutleaf sproutleaf Sep 15, 2024

Choose a reason for hiding this comment

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

Replaced string for regexp with the web API object!

// Generate a schema for a single parameter. In the case where a parameter can
// be of multiple types, `generateTypeSchema` is called for each type.
const generateParamSchema = param => {
const optional = param.endsWith('?');
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 be a good idea to handle arrays like this too when generating a param schema by checking for something that ends with [] to save having to manually handle array versions of each type

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Author

Choose a reason for hiding this comment

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

Just pushed another commit that handles [] logic dynamically!

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.

The changes look great, thanks!

@davepagurek davepagurek merged commit 989ebdd into processing:dev-2.0 Sep 17, 2024
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