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

fix(plugin-manifest-validator): return warning message if homepage_url or name is empty #2776

Merged
merged 12 commits into from
May 28, 2024

Conversation

hung-cybo
Copy link
Contributor

@hung-cybo hung-cybo commented May 22, 2024

Why

Currently, the homepage_url property of manifest.json is optional. When kintone users encounter trouble and no homepage_url in the plugin, users tend to contact Cybozu support.

Therefore, in this PR, warning messages will be returned if the name of the specific language is specified, but the homepage_url for that language is empty, and vice versa.

What

  • Return warning messages if the name of the specific language is specified, but the homepage_url for that language is empty, and vice versa.

How to test

pnpm test

Checklist

  • Read CONTRIBUTING.md
  • Updated documentation if it is required.
  • Added tests if it is required.
  • Passed pnpm lint and pnpm test on the root directory.

@github-actions github-actions bot added pkg: plugin-manifest-validator @kintone/plugin-manifest-validator pkg: plugin-packer @kintone/plugin-packer labels May 22, 2024
Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-2776.d1ite8zvzg8c5i.amplifyapp.com

@hung-cybo hung-cybo changed the title fix(plugin-manifest-validator): show warning message if homepage_url … fix(plugin-manifest-validator): show warning message if homepage_url or name is empty May 23, 2024
@hung-cybo hung-cybo changed the title fix(plugin-manifest-validator): show warning message if homepage_url or name is empty fix(plugin-manifest-validator): return warning message if homepage_url or name is empty May 23, 2024
@hung-cybo hung-cybo marked this pull request as ready for review May 23, 2024 04:52
@hung-cybo hung-cybo requested review from a team, minh-nguyen1985 and tuanphamcybozu and removed request for a team and minh-nguyen1985 May 23, 2024 04:52
Comment on lines 149 to 153
!schema ||
!schema.items ||
schema.items.length === 0 ||
!data ||
data.length === 0
Copy link
Contributor

Choose a reason for hiding this comment

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

@hung-cybo
Split this condition into a seperate function can be:

  • validateRequiredProperties is shorten
  • easy to understand context
  • easy for refactoring and writing unit test for the condition

@@ -162,3 +209,44 @@ const transformErrors = (
// shallow copy
return errors.slice();
};

export const checkRequiredProperties = (
Copy link
Contributor

Choose a reason for hiding this comment

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

@hung-cybo
is it better to move this function to another place?
Similar as:

}

const errors = checkRequiredProperties(json, schema);
if (errors.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@hung-cybo
Reversing this condition to make an early return, and avoid a nested if.

if (errors.length === 0) {
    return true;
}

if (schema.warn) {
....

Comment on lines 222 to 225
const generateErrorMessage = (
property: string,
warning: boolean = false,
): string => `Property "${property}" is ${warning ? "missing" : "required"}.`;
Copy link
Contributor

Choose a reason for hiding this comment

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

@hung-cybo
Splitting this function to the outside of checkRequiredProperties function can improve code readability, reusability, and testing. Further more, this makes checkRequiredProperties shorter.

Comment on lines 12 to 34
for (let i = 0; i < schema.items.length; i++) {
const item = schema.items[i];
if (typeof item === "object") {
for (const property in item) {
if (
!item[property].properties ||
item[property].properties.length === 0
) {
continue;
}

item[property].properties.forEach((prop: string) => {
if (!json[property] || !json[property][prop]) {
errors.push(
generateErrorMessage(`${property}.${prop}`, schema.warn),
);
}
});
}
} else if (!json[item]) {
errors.push(generateErrorMessage(item, schema.warn));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@hung-cybo
How about splitting into several functions?
Such as

....
....
  schema.items.forEach(item => {
      if (typeof item === "object") {
          validateObjectItem(item, json, errors, schema.warn);
      } else {
          validatePrimitiveItem(item, json, errors, schema.warn);
      }
 });

const validateObjectItem = (item, json, errors, warn) => {
    Object.keys(item).forEach(property => {
    if (!item[property].properties || item[property].properties.length === 0) {
      return;
    }

    validateProperty(item[property], property, json, errors, warn);
  });
}

const validateProperty =  (propertyDetail, propertyName, json, errors, warn) => {
    propertyDetail.properties.forEach(prop => {
    if (!json[propertyName] || !json[propertyName][prop]) {
      errors.push(generateErrorMessage(`${propertyName}.${prop}`, warn));
    }
  });
}

const validatePrimitiveItem = (item, json, errors, warn) => {
    if (!json[item]) {
    errors.push(generateErrorMessage(item, warn));
  }
}

P/s: sorry this snippet code doesn't have type and test.

Copy link
Contributor

@tuanphamcybozu tuanphamcybozu left a comment

Choose a reason for hiding this comment

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

LGTM


type ValidateResult = {
valid: boolean | PromiseLike<any>;
errors: null | ErrorObject[];
warnings: null | string[];
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it better to create a warning object like the following? (for future expansion)

type WarningObject {
  message: string
}

Copy link
Member

@tasshi-me tasshi-me left a comment

Choose a reason for hiding this comment

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

LGTM!

@tasshi-me tasshi-me changed the title fix(plugin-manifest-validator): return warning message if homepage_url or name is empty fix(plugin-manifest-validator): return warning message if homepage_url or name is empty May 28, 2024
@tasshi-me tasshi-me merged commit af3533b into main May 28, 2024
17 checks passed
@tasshi-me tasshi-me deleted the fix/show-warning-message-if-empty branch May 28, 2024 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: plugin-manifest-validator @kintone/plugin-manifest-validator pkg: plugin-packer @kintone/plugin-packer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants