beakerbrowser / webdb

The Web is your database.
https://www.npmjs.com/package/@beaker/webdb
MIT License
399 stars 45 forks source link

Emit some information when validation fails #23

Closed jakofranko closed 6 years ago

jakofranko commented 6 years ago

I've been playing around with this trying to learn it (very exciting!!!), and was really struggling to get any records to show up in my test DB. As I drilled into things, I realized that I had a copy/paste error in my code:

await db.define('people', {
      // validate required attributes before indexing
      validate(record) {
        assert(record.name && typeof record.name === 'string')
        assert(record.bio && typeof record.bio === 'string')
        return true
      },
    ...

assert is not defined and thus a Reference Error was getting thrown, but the try/catch block in indexer.js just sets isValid = false; and ditches the error, so I had no idea what the problem was. What are your thoughts of throwing the error if it catches one? It'll be a simple enough fix, so you may see a PR from me later 😬 .

Here's the snippet from indexer.js I'm talking about:

async function readAndIndexFile (db, archive, filepath, version=false) {
     ...
    // index on the first matching table
    for (var i = 0; i < tables.length; i++) {
      let table = tables[i]
      if (table.isRecordFile(filepath)) {
        // validate
        let isValid = true
        if (table.schema.validate) {
          try { isValid = table.schema.validate(record) }
          catch (e) { isValid = false } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        }
pfrazee commented 6 years ago

Ah right. I was suppressing that because I wouldn't want a bunch of failed validations to clog the log (since it can just mean bad user-input). But I think it hurts debugging more to not say anything, so let's do two things: emit an event, and console.debug/log it. We can consider a flag to disable that logging too