question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for validation failures in the `resolve` prop

See original GitHub issue

Is your feature request related to a problem? Please describe. Currently, the resolve prop of the Get component is a simple function that does some transformation on the data coming from the backend. The developer using restful-react is responsible that it is correct (no runtime errors), and add additional logic on their own to handle unexpected responses from the server. In order to offer some legwork to help folks make absolutely sure that the data passing this function either fails gracefully or passes through with sound data, I’d like to suggest two features:

  • catch runtime errors in the fetch method (adding a .catch in https://github.com/contiamo/restful-react/blob/master/src/Get.tsx#L152), sending runtime errors to either the global error management of the library, or the local one if localErrorsOnly is enabled. This would help with third-party parsing/validating solutions like https://github.com/jquense/yup#mixedcastvalue-any-any, which are super powerful, but always throw runtime errors when they fail.
  • allow for the resolve prop to accept a promise which either resolves with the correctly shaped data or rejects with an error. Again, this might help with third-party validation solutions which offer some validation features only asynchronously (recursive data structures and the like).

Describe alternatives you’ve considered I cannot think of an alternative that doesn’t bypass resolve entirely, leaving validation to be performed inside the render method and killing performance.

Additional context At a high-level, the purpose of these suggestions is to allow a style of programming that does super-strict validation before data reaches the internals of a react component tree. This provides both safety and debugging convenience: there is always a failure before data even reaches presentational views, narrowing down issues to the validator logic, which can be unit-tested with an ever-broadening list of fixtures from the backend. No more looking around React code to fix backend-frontend miscommunication.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
peterszerzocommented, Sep 27, 2018

Awesome - shall I go ahead with the implementation?

1reaction
fabien0102commented, Sep 26, 2018

I like the idea, I think the simple way to do this is to try/catch the resolve part in the fetch method to permit to throw inside the user resolve method.

After we can just set the classic error props and retrieve the normal flow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve model validation failed errors in CloudFormation - AWS
When I create a resource with AWS CloudFormation, I receive a "Model Validation Failed" error in my stack events. Short description. Type, ...
Read more >
Throw error when props validation fails #11827 - vuejs/vue
Currently props validation only issues a warning in non production environment. I was trying to create a component like react's error-boundary ...
Read more >
Prop results in path failed validation error - Help - Pipedream
If I copy the actual value of the prop it works. The error implies the prop is passed literally.
Read more >
React eslint error missing in props validation - Stack Overflow
To fix this error, define your props before they are used. For example: ... Issue: 'id1' is missing in props validation, eslintreact/prop-types
Read more >
How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found