nekodb icon indicating copy to clipboard operation
nekodb copied to clipboard

function to prevalidate before save

Open techo207 opened this issue 6 years ago • 1 comments

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

techo207 avatar Oct 05 '19 19:10 techo207

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.

4strid avatar Oct 11 '19 05:10 4strid