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

feat: runtime validation #140

Merged
merged 8 commits into from
Jul 24, 2020
Merged

feat: runtime validation #140

merged 8 commits into from
Jul 24, 2020

Conversation

sarahdayan
Copy link
Member

In #138 we brought up the idea of adding a runtime validation step to verify invariants. This PR introduces one using invariant, a mirror of what Facebook uses within React.

N.B.: The React invariant module has changed since then, they also provide links to an error decoder on their site which returns the actual invariant message. I think we're okay with the generic message for now since we don't have that many invariants. We can look into it later on if we think this is an actual problem.

Summary

Instead of throwing within components when a configuration option isn't properly set, we now check them all right before running the application, via a validateConfig function. This function takes the configuration object and a set of rules. The rules are grouped within an object which keys are the top-level name of the option, and which value is an object that implements two methods: validate and errorMessage.

The validate method takes the value of the option, method performs an assertion, and returns an object that contains two properties: valid, which is the result of the assertion, and context, for returning extra information about the assertion (useful for error messages).

The errorMessage method takes a context, and returns an error message.

The validateConfig function iterates through the configuration and checks each against their rule, and throws using invariant when there's a violation.

Here's an example of what it looks like in development mode, when passing an invalid type to one of the refinements:

Capture d’écran 2020-07-24 à 14 01 15

In production, we want to trim away as many bytes as we can, so we strip away these specific messages and return a generic error message instead.

Capture d’écran 2020-07-24 à 14 02 02

Next steps

Once we have a more broken down documentation with short links (as brought up in #139) we can add links to the development errors for even better DX.

Copy link
Member

@francoischalifour francoischalifour left a comment

Choose a reason for hiding this comment

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

Really cool!

src/config/index.js Outdated Show resolved Hide resolved
@sarahdayan sarahdayan merged commit 9b64588 into next Jul 24, 2020
@sarahdayan sarahdayan deleted the feat/runtime-validation branch July 24, 2020 14:38
sarahdayan added a commit that referenced this pull request Jul 28, 2020
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

Successfully merging this pull request may close these issues.

2 participants