charto / classy-mst

ES6-like syntax for mobx-state-tree
MIT License
91 stars 5 forks source link

Question: initialize class instance with prop validation #15

Closed mindaugasnakrosis closed 5 years ago

mindaugasnakrosis commented 5 years ago

Is it possible using class-mst to construct ES6 class instance with prop validation?

Example of desired functionality:

const TodoModel = types.model({
  name: types.string,
});

class TodoClass extends shim(TodoModel) {
  get name() {
    return this.name;
  }
  set name(name) {
      this.name = name;
  }
}
const Todo = mst(TodoClass, TodoModel),

const validInstance = Todo.create({ name: 'todoName' });
// Todo: { name: 'todoName' }
const invalidInstance = Todo.create({ name: true });
// Expected error message:
"Error while converting `{"name":true}` to `TodoModel`:
at path "/name" value `true` is not assignable to type: `string` (Value is not a string)."

@jjrv