canjs / can-fixture

Intercept and simulate AJAX requests. Works without CanJS.

Home Page:https://canjs.com/doc/can-fixture.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can-fixture should do type conversion

m-mujica opened this issue · comments

Currently, using can-fixture forces models to always use type.convert in order to avoid type errors:

class Todo extends ObservableObject {
  static props = {
    id: Number,
    name: String,
    complete: false
  };
}

const todoStore = fixture.store(
  [
    { name: "mow lawn", complete: false, id: 5 },
    { name: "dishes", complete: true, id: 6 },
    { name: "learn canjs", complete: false, id: 7 }
  ],
  Todo
);

fixture("/api/todos", todoStore);

the Todo type will throw with responses from the fixtures that have ids as strings instead of numbers:

Uncaught Error: Type value "6" is not of type Number.

The object schema should be used to ensure the response has the correct types. This other issue has to be resolved first canjs/can-type#27

Currently fixture store has a couple of signatures:

fixture.store(items, queryLogic);
fixture.store(count, makeItems, queryLogic);

The question is whether this new ability should be an extra argument or replace the queryLogic argument. Looking into it..

replace the queryLogic I think. I was thinking fixture.store(items, type.makeWholeThingConvert( Todo ) )

Or maybe event a type.all(type.convert, Thing) so you could also do type.all(type.check, Thing) or whatever you want.

So it looks like what this needs to do is create a new TypeObject that implements a getSchema that gets the parent schema and creates a new version that replaces the keys with type.convert(typeObject). I'm going to try out this implementation.

could .store() directly take a schema?

Created the proposal in canjs/can-type#31