IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

doc.update({...}) throws validation error when document has schema #17

Closed kavaro closed 6 years ago

kavaro commented 6 years ago

Example test case and proposed fix.

import {struct} from 'superstruct';
import firebase from 'firebase';
import 'firebase/firestore';
import {initFirestorter, Collection, Document} from 'firestorter';

firebase.initializeApp({/*...*/});
initFirestorter({firebase});
export const db = firebase.firestore();
const settings = {timestampsInSnapshots: true};
db.settings(settings);

const schema = struct({
    name: 'string'
});

export class Doc extends Document {
    constructor(source) {
        super(source, {schema});
    }
}

async function test() {
    const collection = new Collection('test', {DocumentClass: Doc});

    const doc = await collection.add({name: 'Old name'});

    // the update statement below throw's a validation error
    //
    // Solution:
    // in src/Document.js line 346 should be changed
    //
    // FROM
    //
    // this._validateSchema({
    //      ...this.data,
    //      fields
    // });
    //
    // TO
    //
    // this._validateSchema({
    //      ...this.data,
    //      ...fields
    // });
    await doc.update({
        name: 'New name'
    });
}

test();
IjzerenHein commented 6 years ago

Hi, thanks for reporting this @kavaro ! I've just released an update v0.11.1 which fixes this problem. Cheers!