-
Notifications
You must be signed in to change notification settings - Fork 49
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
Conversation
This pull request is automatically being deployed by Amplify Hosting (learn more). |
!schema || | ||
!schema.items || | ||
schema.items.length === 0 || | ||
!data || | ||
data.length === 0 |
There was a problem hiding this comment.
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 = ( |
There was a problem hiding this comment.
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 validateHttpsUrl = ( |
} | ||
|
||
const errors = checkRequiredProperties(json, schema); | ||
if (errors.length > 0) { |
There was a problem hiding this comment.
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) {
....
const generateErrorMessage = ( | ||
property: string, | ||
warning: boolean = false, | ||
): string => `Property "${property}" is ${warning ? "missing" : "required"}.`; |
There was a problem hiding this comment.
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.
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)); | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this 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[]; |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
homepage_url
or name
is empty
Why
Currently, the
homepage_url
property ofmanifest.json
is optional. Whenkintone
users encounter trouble and nohomepage_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
How to test
Checklist
pnpm lint
andpnpm test
on the root directory.