-
Notifications
You must be signed in to change notification settings - Fork 4
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: add basic field type runtime validators #113
Conversation
1cd1a35
to
edb6f8d
Compare
Codecov Report
@@ Coverage Diff @@
## master #113 +/- ##
==========================================
+ Coverage 94.96% 95.08% +0.12%
==========================================
Files 66 67 +1
Lines 1627 1689 +62
Branches 193 202 +9
==========================================
+ Hits 1545 1606 +61
- Misses 80 81 +1
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
@@ -1,3 +1,5 @@ | |||
import { validate as validateUUID } from 'uuid'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, didn't know this existed, we should use it in more places where we currently write out the regex ourselves
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @ide about the AggregateError.
It'd make sense to throw an InvalidFieldValueError
if www
is not able to fix or detect the error without making a trip to Postgres. An example of this is corrupt data in postgres itself.
But in this case, it should be the responsibility of www
to make sure the field inputs are correct. The entity framework should throw an error without wrapping it in a result, and only request data from the DB when it knows all the inputs are well formed.
We also discussed this on zoom, and found out dataloaders has this behaviour, so this will be a happy case of accidental consistency lol
@quinlanj and I talked a bit about this and think that throwing independently of Result actually makes the most sense for two reasons:
I'll update the PR to reflect this. |
Why
We're seeing a decent number of bugs/crashes surrounding invalid types of values being set in mutators and invalid types of load-by values being used in loaders.
These bugs manifest themselves in obvious ways sometimes, and other times less obvious.
select * from "table" where "id" = ANY($1) - invalid input syntax for uuid: "c6cef326-2a93-4477-8e46-7faae8e1684"
TypeError: Cannot read property 'push' of undefined
The write example is a fairly clear error already, but this PR preempts the round trip to the DB by detecting a query that will most likely fail anyways ahead of time.
For the read example, the error is more cryptic and is what triggered the investigation and this PR as the result. The issue is that when a load comes in for a UUID that postgres thinks is valid, but postgres returns the UUID in the normalized format in the result set, we don't know to create a map entry at https://github.com/expo/entity/blob/master/packages/entity/src/EntityDatabaseAdapter.ts#L123 so the
push
call a few lines later will fail. Concretely, the trace is:a0eebc999c0b4ef8bb6d6bb9bd380a11
which postgres thinks is valid.a0eebc999c0b4ef8bb6d6bb9bd380a11
.a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
.Note that this postgres behavior is documented at https://www.postgresql.org/docs/9.1/datatype-uuid.html
How
Add field value validation to the following calls:
EntityMutator.createAsync/updateAsync
EntityLoader.loadByFieldValueAsync
and similar methods.Test Plan
Run tests, including new ones to test behavior and validation correctness.