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

Sum Example: Validating Stream Values #113

Closed
thurt opened this issue Apr 27, 2016 · 3 comments
Closed

Sum Example: Validating Stream Values #113

thurt opened this issue Apr 27, 2016 · 3 comments

Comments

@thurt
Copy link

thurt commented Apr 27, 2016

Hello, I just started looking at flyd so I would like to know suggestions for how to validate stream values before allowing side-effects.

For the Sum example, I was submitting values to the x and y streams through the console and noticed it wasn't validating input; inputting x('boo'), for instance, would still result in updating DOM.

I modified Sum so that it validates values before updating DOM.

      var valid_x = flyd.combine(function(x) {
        if (typeof x() === 'number') {
          return x()
        }
        else {
          console.log('Numbers only, please!');
        }
      }, [x])
      var valid_y = flyd.combine(function(y) {
        if (typeof y() === 'number') {
          return y()
        }
        else {
          console.log('Numbers only, please!');
        }
      }, [y])
      var sum = flyd.combine(function(x, y) {
          return valid_x() + valid_y();
      }, [valid_x, valid_y]);
      flyd.map(function(sum) {
        sumBox.innerHTML = sum;
      }, sum);
      flyd.map(function(valid_x) {
        xBox.innerHTML = valid_x;
      }, valid_x);
      flyd.map(function(valid_y) {
        yBox.innerHTML = valid_y;
      }, valid_y);

Would this be a typical strategy to validate values? Are there other ways?
Thanks

@c-dante
Copy link
Contributor

c-dante commented Apr 27, 2016

Handling errors and validation within a stream were discussed here: #20 , #103 , #35 , and #69, and a pull request with a possible implementation (by build Either into flyd): #104

TL;DR, seems flyd is not concerned with errors -- the way you're dealing with them here is a fine solution, and tons of other options exist (the Either monad is an example from the PR).

I don't think that the examples need to be updated for this, but an example demonstrating some error handling strategies wouldn't be a bad idea.

@thurt
Copy link
Author

thurt commented Apr 27, 2016

Thanks @c-dante I will look over the links.

an example demonstrating some error handling strategies wouldn't be a bad idea.

👍 I think that would be a "nice to have" since validating user input can be a significant part of certain projects.

@thurt
Copy link
Author

thurt commented May 10, 2016

Closing this issue. @c-dante has provided a good summary on discussions surrounding how to validate/error check while using flyd streams.

@thurt thurt closed this as completed May 10, 2016
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

No branches or pull requests

2 participants