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

feat(ibm-use-date-based-format): introduce new validation rule #706

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dpopp07
Copy link
Member

@dpopp07 dpopp07 commented Dec 19, 2024

PR summary

This commit introduces the new 'ibm-use-date-based-format' rule,
which will heuristically verify that schemas, with either a name
or an example value indicating a date-based logical type, be
strings and use either "date" or "date-time" as the format.

PR Checklist

General checklist

Please make sure that your PR fulfills the following requirements:

  • The commit message follows the Angular Commit Message Guidelines.
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Dependencies have been updated as needed
  • .secrets.baseline has been updated as needed
  • npm run generate-utilities-docs has been run if any files in packages/utilities/src have been updated

Checklist for adding a new validation rule:

  • Added new validation rule definition (packages/ruleset/src/rules/*.js, index.js)
  • If necessary, added new validation rule implementation (packages/ruleset/src/functions/*.js, updated index.js)
  • Added new rule to default configuration (packages/ruleset/src/ibm-oas.js)
  • Added tests for new rule (packages/ruleset/test/*.test.js)
  • Added docs for new rule (docs/ibm-cloud-rules.md)
  • Added scoring rubric entry for new rule (packages/validator/src/scoring-tool/rubric.js)

The comment noted that the rule was using the resolved API definition,
when it was, in fact, using the unresolved definition. This wasn't a
bug - the rule doesn't look at any locations that might include
references - but the documentation was wrong and misleading. This
commit fixes that.

Signed-off-by: Dustin Popp <[email protected]>
@dpopp07 dpopp07 requested review from hudlow and padamstx December 19, 2024 16:24
@dpopp07
Copy link
Member Author

dpopp07 commented Dec 19, 2024

Whoops, forgot to update the docs - I will push that change soon! Everything else is ready to review

Docs are updated 👍

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

PR looks great!
I just had one small suggestion for date-based-utils.js.

This commit introduces the new 'ibm-use-date-based-format' rule,
which will heuristically verify that schemas, with either a name
or an example value indicating a date-based logical type, be
strings and use either "date" or "date-time" as the format.

Signed-off-by: Dustin Popp <[email protected]>
Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

LGTM!

packages/ruleset/src/functions/use-date-based-format.js Outdated Show resolved Hide resolved
packages/ruleset/src/functions/use-date-based-format.js Outdated Show resolved Hide resolved
* @returns an array containing the violations found or [] if no violations
*/
function checkForDateBasedFormat(s, p, apidef) {
const examples = [];
Copy link
Member

Choose a reason for hiding this comment

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

1/3: declare array for examples at all levels of the schema hierarchy.


// Either use the schema example directly or search the list of examples
// for an example object that contains a value for this property.
const exampleValue = schema.example || findExample(propertyPath, examples);
Copy link
Member

Choose a reason for hiding this comment

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

This isn't composition-aware, so it will miss examples in schemas like this:

{
  "type": "string",
  "allOf": [
    {
      "example": "2024-12-19"
    }
  ]
}

Signed-off-by: Dustin Popp <[email protected]>

Co-authored-by: Dan Hudlow <[email protected]>
expect(isDateBasedName('create_date')).toBe(true);
expect(isDateBasedName('created_time')).toBe(true);
expect(isDateBasedName('created_date')).toBe(true);
expect(isDateBasedName('modify_time')).toBe(true);
Copy link
Member Author

Choose a reason for hiding this comment

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

Something about this (and other things like running_time for a movie, or hang_time for a football) feels like it could conceivably be a duration, which makes me reluctant to flag these for integer types. That's why I currently don't check for these. What do you think @hudlow ?

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.

3 participants