Skip to content

add support of property validation message - #3438

Merged
knolleary merged 3 commits into
node-red:devfrom
node-red-hitachi:property-validation-tooltip-message
Apr 26, 2022
Merged

knolleary merged 3 commits into
node-red:devfrom
node-red-hitachi:property-validation-tooltip-message

Conversation

@HiroyasuNishiyama

Copy link
Copy Markdown
Contributor
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Proposed changes

As discussed in this forum thread, the purpose of this PR is to present to the user the cause of the failure of node property verification in an easy-to-understand manner.

  • The property validation function of the property definition can return a string instead of a boolean value. This string is used as an explanation for the validation error.

スクリーンショット 2022-02-13 13 51 31

For compatibility, this function takes optional second argument which is currently empty object. So, newly developed node can check this argument and change its behavior.

// example:
defaults: {
    property_name: {
         value: ...,
         validate: function (v, opt) {
              if (check is ok) {
                  return true;
              }
              if (opt) {
                  return "error description";
              }
             return false;
         }
    }, ...
}
  • Property definition may have label property. The value s used as a display name in the validation of the required field and configuration node error.
// example:
defaults: {
    property_name: {
         value: ..., required: true, label: "meaningful name"
    }, ...
}
  • Standard validation function generation functions provided as RED.validators.* is modified to accept additional option object. This object can have a property property. property The property is used as the display name when generating an error message.
// example:
defaults: {
    property_name: {
         value: ...,
         validate: RED.validators.number(false, { 
             property: "meaningful property name"
         })
    }, ...
}
  • Show an error message using tooltip for a field that failed validation.
    • For this, added delete function to RED.popover.tooltip.
    • This feature does not work for some fields (e.g. typedInput), probably because of a conflict.

スクリーンショット 2022-02-13 13 53 35

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run grunt to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 67.313% when pulling 9f98b4b on node-red-hitachi:property-validation-tooltip-message into 5293563 on node-red:dev.

@knolleary knolleary added this to the 3.0 milestone Mar 3, 2022

@knolleary knolleary left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this.

It would have been easier to review if it did not also update all core nodes to use this new API. Because now any feedback/changes on the API has to be made across all of the nodes.

In the proposal, you have added a label property to the property definition:

// example:
defaults: {
    property_name: {
         value: ..., required: true, label: "meaningful name"
    }, ...
}

Separately, you have updated the built-in validators to take an object that also lets you provide a meaningful name:

defaults: {
    property_name: {
         value: ...,
         validate: RED.validators.number(false, { 
             property: "meaningful property name"
         })
    }, ...
}

That feels like duplication. As the editor will be calling the validator for a particular property, can it not provide the property information itself and remove the need for the user to duplicate it?

Comment on lines +159 to +162
valid = definition[property].validate.call(node,value, opt);
if ((typeof valid) === "string") {
return valid;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is possible that an existing validate function returns a String and relies on "" being false and anything else being true.

This code would then cause it to incorrectly mark the property as invalid. We discussed in the forum using the new second argument as a sign the validate function returns strings to mean invalid.

Suggested change
valid = definition[property].validate.call(node,value, opt);
if ((typeof valid) === "string") {
return valid;
}
valid = definition[property].validate.call(node,value, opt);
// If the validator takes two arguments, it is a 3.x validator that
// can return a String to mean 'invalid' and provide a reason
if (definition[property].validate.length === 2 && (typeof valid) === "string") {
return valid;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for late reply.
I have merged your suggested code.
Regarding the built-in validator, I have changed it to use the label property of property definition.

@dceejay

dceejay commented Mar 24, 2022

Copy link
Copy Markdown
Member

Are these "meaningful labels" easily translated ? by the standard this._("mynode.meaningful.label") style ?

@HiroyasuNishiyama

Copy link
Copy Markdown
Contributor Author

Are these "meaningful labels" easily translated ? by the standard this._("mynode.meaningful.label") style ?

Yes. You can define it in property definition as:

prop: {
    value: "...",
    label: RED._("mynode.meaningful.label")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

4 participants