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

Audit fields' behavior when setting invalid values #2462

Closed
11 tasks
RoboErikG opened this issue May 14, 2019 · 4 comments
Closed
11 tasks

Audit fields' behavior when setting invalid values #2462

RoboErikG opened this issue May 14, 2019 · 4 comments
Labels
component: fields help wanted External contributions actively solicited type: cleanup

Comments

@RoboErikG
Copy link
Contributor

RoboErikG commented May 14, 2019

Problem statement

In general, trying to set a field with an invalid value should not change the current value of the field. There are several fields that instead fall back to the default if they can't interpret the value passed in.

There are some fields where invalid values should cause errors, such as field images. Please add comments to this issue to clarify any field specific bad value handling.

Fields to review:

  • angle
  • checkbox
  • colour
  • date
  • dropdown
  • image
  • label
  • label serializable
  • number
  • text input
  • variable
@RoboErikG
Copy link
Contributor Author

RoboErikG commented May 14, 2019

Image specific behavior

Images should throw an error if the width or height are invalid. The src is allowed to be invalid as the browser will handle it as a broken image.

@RoboErikG RoboErikG added component: fields help wanted External contributions actively solicited type: cleanup labels May 14, 2019
@RoboErikG RoboErikG added this to the Bug Bash Backlog milestone May 14, 2019
@RoboErikG RoboErikG mentioned this issue May 14, 2019
3 tasks
@BeksOmega
Copy link
Collaborator

So after #2441 all fields should work, but I think the code can be reorganized to make it better.

Almost all field constructors look like this:

  opt_value = this.doClassValidation_(opt_value);
  if (opt_value === null) {
    opt_value = 0;
  }
  Blockly.FieldAngle.superClass_.constructor.call(
      this, opt_value, opt_validator);

The problem with this is that the super-constructor calls setValue which calls doClassValidation_ which means doClassValidation_ is getting called twice.

Proposal

Inside setValue, if doClassValidation_ returns null, and the current value is also null, use a getClassDefaultValue function to set the value. This way (almost) all field constructors would just look like this:

  Blockly.FieldAngle.superClass_.constructor.call(
      this, opt_value, opt_validator);

Making the code more organized for external developers, and more efficient.

Blockly.Field.prototype.setValue = function(newValue) {
  var oldValue = this.getValue();
  if (newValue === null) {
    // Not a valid value to check.
    return;
  }

  newValue = this.doClassValidation_(newValue);
  if (newValue === null) {
    if (oldValue === null) {
      // The field's start value isn't valid, use the default.
      newValue = this.getClassDefaultValue();
    } else {
      this.doValueInvalid_();
      if (this.isDirty_) {
        this.forceRerender();
      }
      return;
    }
  }
  // etc...
}

Oh and also any newValue === null checks could be removed from class validators.

Additional Information

This may be overkill hehe.

@rachel-fenichel
Copy link
Collaborator

@BeksOmega can you check off any of these that you've already fixed, and comment on any that you know have weird behaviour?

@BeksOmega
Copy link
Collaborator

@rachel-fenichel As far as I know, all fields are functioning correctly. Each field type has a test suite for "Value -> New Value" which covers this issue (I went back and checked). All of those tests pass.

Sadly I cannot check off the boxes because I do not have perms :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: fields help wanted External contributions actively solicited type: cleanup
Projects
None yet
Development

No branches or pull requests

3 participants