amilner42 / code-tidbit

Share Programming Knowledge Better
GNU General Public License v3.0
9 stars 1 forks source link

MongoStringIDSchema -> MongoIDSchema #162

Closed amilner42 closed 7 years ago

amilner42 commented 7 years ago

Once this issue is complete we should change MongoStringIDSchema -> MongoIDSchema, you can use the new WildCardSchema to ignore the type and inside the restriction use the built-in method in the mongo driver to check if something is a valid object ID. This way both strings and ObjectIDs are validated.

amilner42 commented 7 years ago

The mongoStringID kleen schema is here. You'll notice it is for strings-only: primitiveType: kleen.kindOfPrimitive.string , make it the anySchema and then instead add a restriction which uses the isValid from mongodb. That way we allow both strings and ObjectIDs.

Why is it nice to allow both?

When we receive data from the user we always get the IDs in string form, and so that kleen schema works. But sometimes we get data from the DB and then use that data to make more calls to the db, and when we get data from the DB the IDs are going to be ObjectIDs, not strings, which means that we have to manually convert the ObjectIDs -> strings to make sure our validation doesn't fail, this is annoying.

Why validate data for requests being made internally?

We won't be soon, on the QA branch for the new QA endpoints I started having validation be optional, specifically so we only validate when the user is making requests. Once I merge that QA branch, we'll have to go to the existing endpoints and also have them switch to optional validation, that issue is here.

daravi commented 7 years ago

Do we want union of string and anySchema with isValid restriction?

amilner42 commented 7 years ago

Check if ObjectID.isValid will validify a string as well. I'm pretty sure it will validify both strings and ObjectIDs, so you won't need a union.