lmcq / firebase-firestorm

Firebase Firestorm is an ORM for Firestore which can be used with Typescript.
MIT License
252 stars 19 forks source link

How to use for data validation #25

Open kiptoomm opened 4 years ago

kiptoomm commented 4 years ago

Hi @lmcq, this looks like a very interesting library. I was wondering how one can use it to validate the fields of a model before a write event when requests are coming from the Firebase client SDKs? I can imagine how to do so when requests are coming in from a REST API call (let's say, if we expose an endpoint using Express or using HTTP cloud functions) like so:

...
exports.createUser = functions.https.onRequest(async (request, response) => {
    const user = new User(); // => create instance of User model
    user.firstName = request.query.firstName;
    user.lastName = request.query.LastName;
    user.email = request.query.emailAddress;
    user.password = request.query.password;
    user.birthDate = request.query.dateOfBirth;

    Collection(User).create(user).then((_newUser: User) => {
        response.json(_newUser.toJSON()) // return response to caller 
    }
}
...

However, I don't know how to take advantage of this library yet because my client apps use the Firebase SDK, which write directly to the document (in other words, how to 'intercept' those requests and validate them through firebase-firestorm). Would greatly appreciate any examples. I would love to use this library to 'enforce' my schema (data validation), instead of using firestore security rules (tedious!)

ianmubangizi commented 4 years ago

Also looking for this feature