Allowing `undefined` values in array in `thinky` schema validation

375 Views Asked by At

The schema looks like this:

import Game from './Game'
import {io, thinky} from '../'

const type = thinky.type


export const playerSchema = {
  id: type.string(),
  createdAt: type.date().default(thinky.r.now()),
  modifiedAt: type.date(),
  gameId: type.string(),
  name: type.string().default('Anon'),
  socket: type.string(),
  disconnected: type.boolean().default(false),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default([])
    }).default([])
  })).default([])
}

When I try to save a player where levels has undefined values, I get:
"Cannot read property 'rounds' of undefined"

Further explanation: Sometimes a player will enter the game after the first level has been completed by other players. So, I would like the value of this index in the array to stay undefined.

Example Data (in YAML):
id: 1337
...
levels:
-
- rounds:
  - ...
    tries:
    - answer: [2, 4]
      ...

If i change playerSchema.levels to type.array(), I get:
"The element in the array [levels] (position 0) cannot be undefined"

Edit after comments:

Even with the defaults this doesn't work...

var thinky = require('thinky')({
  db: 'slam'
})
var type = thinky.type
var r = thinky.r


var playerSchema = {
  id: type.string(),
  createdAt: type.date().default(thinky.r.now()),
  modifiedAt: type.date(),
  gameId: type.string(),
  name: type.string().default('Anon'),
  socket: type.string(),
  disconnected: type.boolean().default(false),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema(type.object().schema({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema(type.object().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default({})).default([])
    }).default({})).default([])
  }).default({})).default([])
}

var Player = thinky.createModel('Player', playerSchema)

var player = new Player({
  levels: [undefined, {}]
})

player.save().then(console.log)

results in ...

/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/schema.js:92
      field = field[path[j]];
                   ^

TypeError: Cannot read property 'rounds' of undefined
    at generateDefault (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/schema.js:92:20)
    at Object.generateDefault (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/schema.js:86:11)
    at model.Document._generateDefault (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/document.js:172:16)
    at new model (/Users/arnar/git/slam-web-app/app/node_modules/thinky/lib/model.js:131:11)
    at Object.<anonymous> (/Users/arnar/git/slam-web-app/app/tests/test-player-schema.js:36:14)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

I even tried to create a pre validation hook, but that has no effect what so ever...

Player.pre('validate', function(next) {
  this.levels = this.levels.map(function(l) {
    return l != null ? l : {}
  })
  next()
})
1

There are 1 best solutions below

6
neumino On

Edit following up your comment. This is what you want (you basically need a default({}) inside the definition of the array schema:

var thinky = require('./lib/thinky.js')();
var type = thinky.type;
var r = thinky.r;


var Player = thinky.createModel('Player', {
  id: type.string(),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema(type.object({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default({})
    }).default({})).default([])
  })).default([])
})

var player = new Player({
  levels: [{}] 
});

player.save().then(console.log);

Author of thinky here. This works for me:

var thinky = require('./lib/thinky.js')();
var type = thinky.type;
var r = thinky.r;


var Player = thinky.createModel('Player', {
  id: type.string(),
  levels: type.array().schema(type.object().schema({
    rounds: type.array().schema({
      card: type.number(),
      readyForNext: type.boolean().default(false),
      readyForNextTime: type.date(),
      tries: type.array().schema({
        answer: type.any(),
        correct: type.boolean(),
        startTime: type.date(),
        hasAnswered: type.boolean().default(false),
        hasAnsweredTime: type.date().default(null),
        hasClickedRetry: type.boolean().default(false),
        hasClickedRetryTime: type.date()
      }).default([])
    }).default({})
  })).default([])
})

var player = new Player({
  levels: undefined
});

player.save().then(console.log);

On a side note, you may want to replace one default([]) with default({}) since you are setting a type object.