4strid / nekodb

Tiny ODM for MongoDB/NeDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

function to prevalidate before save

techo207 opened this issue · comments

necessary to check if user possibly introduced wrong data type before de save

Can you elaborate or maybe give an example? There is a prevalidate hook as well as a presave hook though I'm not sure which you need without an example.

const TypeCheckModel = ko.Model('TypeCheck', {
  field: ko.Number
})

TypeCheckModel.presave = function (model, next) {
  if (typeof model.field !== 'number') {
    return next(new Error('Invalid data type'))
  }
  next()
}

Is how you'd use a hook to perform this kind of validation, though I'm not sure why you wouldn't just use the schema itself to enforce type.