Mobx-state-tree: Custom error message for typecheck

Created on 5 Sep 2017  Â·  5Comments  Â·  Source: mobxjs/mobx-state-tree

It would be nice to be able to supply custom messages for typecheck errors

const TaskStore = types
    .model('TaskStore', {
        status: {types.string, 'Status has to be string'},
        name: {types.refinement(types.string, value => value.length > 5), 'Min length 5'}
    })

Or is it more appropriate to have a pre-validator on the stuff you're passing to create?

const validator = (entity) {
  const errors = [];
  if (entity.name.length < 5) {
    errors.name = 'Min length 5';
  }
  return errors;
}

if (validator(input)) { TaskStore.create(input) } 

This is a bit annoying because you'll end up duplicating validation logic...

All 5 comments

At the moment is some sort of "internal" API, but you could use Type.validate(value) that returns an array of object with the invalid path and eventually the reason for the field being invalid.

IIs it possible to simply intercept validation error that might occur during Type.create()?

just wrap a function around it?

function createCar(snapshot) {
if (!Car.validate(snapshot))
throw "dude, where is my car?"
return Car.create(snapshot)
}

Op za 3 mrt. 2018 om 16:30 schreef Davit Barbakadze <
[email protected]>:

IIs it possible to simply intercept validation error that might occur
during Type.create()?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/364#issuecomment-370155841,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhA1WXrGVbQaMtkvBCM5S4fks5ATPks5tarcUgaJpZM4PM2A0
.

@mweststrate thanks for the hint. I went and actually tried to wrap Type.create() in try...catch and it seems to intercept the error nicely. I'm not sure what's the current situation around using try...catch (I bet it's a new evil), but seems fine for my needs.

try catch is fine, the only trick with try catch is that it might trigger
other errors (for example, you have valid snapshot, but a typo in a
afterCreate action. You probably don't want to catch those and report them
back as 'invalid json', because that isn't the case (In Java you could
nicely specify catch handlers only for specific errors, but here you have
to write some manual code to 'recognize' the correct exception)

Op za 3 mrt. 2018 om 20:06 schreef Davit Barbakadze <
[email protected]>:

@mweststrate https://github.com/mweststrate thanks for the hint. I went
and actually tried to wrap Type.create() in try...catch and it seems to
intercept the error nicely. I'm not sure what's the current situation
around using try...catch (I bet it's a new evil), but seems fine for my
needs.

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-state-tree/issues/364#issuecomment-370171738,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhOv1m_gN9HkxHrNqhJZjVy6GohNKks5taumNgaJpZM4PM2A0
.

Was this page helpful?
0 / 5 - 0 ratings