Giter VIP home page Giter VIP logo

Comments (19)

mweststrate avatar mweststrate commented on May 17, 2024 3

Imho behavior shoud be:

const Item = createFactory({
  id: types.number,
  name: types.string,
})

Item.is({}) // false, missing name and id
Item.is({ id: 3}) // false, missing name
Item.is({ id: 3, name: ""}) // OK
Item.is({ id: 3, name: "", description: ""}) // OK, but description will be 'lost' upon instantation

createFactory({
  id: types.number,
  name: types.string("")
}).is({ id: 3 }) // OK, name is lacking but has a default

from mobx-state-tree.

mweststrate avatar mweststrate commented on May 17, 2024 2

Good point, I think that should not be the behavior, I was tinkering about that already.

Currently Item.is({ id: 3, name: "test", stuff: false }) will yield false, I think this behavior should change to mimic how e.g. TypeScript does this, if the required set of attributes is present, there should be a match. What do you think @mattiamanzati ?

from mobx-state-tree.

mweststrate avatar mweststrate commented on May 17, 2024 2

I think this can best and easily be fixed once #65 is done, which features many type system improvements

from mobx-state-tree.

mattiamanzati avatar mattiamanzati commented on May 17, 2024 1

@zetoke Yes indeed, it yields false atm because it checks over the count of keys

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

How I can help with this issue?

from mobx-state-tree.

mattiamanzati avatar mattiamanzati commented on May 17, 2024

Yeah, that should be correct @mwestrate! Btw is it a good thing to silently ingore those properties?

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

I started an investigation of a problem for myself. Because I need this issue resolved :D
And after some time I wrote this master...zetoke:feature/object-is
The main problem of skipping fields inside isSnapshotValid:

const Factory = createFactory({ a: number });
Factory.is({ b: string })  // true like a Factory.is({})

What do you think about this?

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

Currently Item.is({ id: 3, name: "test", stuff: false }) will yield false

@mweststrate But Item.is({ id: 4 }) will yield true. Am I right?

from mobx-state-tree.

mweststrate avatar mweststrate commented on May 17, 2024

@zetoke @mattiamanzati, eh no, should return false imho? name is missing in json but not declared maybe or withDefault

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

@mweststrate @mattiamanzati but in the latest release (without my modifications) the "same" logic:

Factory = {
  id: number,
  name: string,
}
Factory.is({ id: 5 }) // true

It's bacause .is() is about a snapshot comparsion/validation, not a validation of instance of type (as I understood).

from mobx-state-tree.

mattiamanzati avatar mattiamanzati commented on May 17, 2024

@zetoke Factory.is({ id: 5, name: "hello", additionalKey: false }) // => false, because the number of keys is > than the allowed

from mobx-state-tree.

mweststrate avatar mweststrate commented on May 17, 2024

@zetoke I think Factory.is({ id: 5}) -> false, because Factory.create({ id: 5 }) should fail for lacking a name

from mobx-state-tree.

mattiamanzati avatar mattiamanzati commented on May 17, 2024

@mweststrate Yes indeed

from mobx-state-tree.

mweststrate avatar mweststrate commented on May 17, 2024

@mattiamanzati btw, should name: string("") be a shorthand for withDefault(string, "") or literal(""). I guess the first right?

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

@zetoke Factory.is({ id: 5, name: "hello", additionalKey: false }) // => false, because the number of keys is > than the allowed

yep. But if number of keys < keys in snapshot - everything is ok for mobx-state-tree for now. That have been my point.

@zetoke I think Factory.is({ id: 5}) -> false, because Factory.create({ id: 5 }) should fail for lacking a name

Probably. What's about just {} (Factory.is({})) ?

from mobx-state-tree.

mattiamanzati avatar mattiamanzati commented on May 17, 2024

@mweststrate string("") is the same as doing "", so it will fallback to https://github.com/mobxjs/mobx-state-tree/blob/master/src/types/object.ts#L62-L66 and being a primitive!

So the only correct one is withDefault(string, "")!

With the new syntax that issue will be solved, because it will be string.create("") vs. withDefault(string, "") and that create will let you understand that you are crating an instance instead of passing a type

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

@zetoke I think Factory.is({ id: 5}) -> false, because Factory.create({ id: 5 }) should fail for lacking a name

@mweststrate The main problem is applySnapshot using .is() as a function for validating snapshot. But this one could be "incremental". Or not?

from mobx-state-tree.

mattiamanzati avatar mattiamanzati commented on May 17, 2024

Only correction is the following

createFactory({
  id: types.number,
  name: types.withDefault(types.string, "")
}).is({ id: 3 }) // OK, name is lacking but has a default

from mobx-state-tree.

zetoke avatar zetoke commented on May 17, 2024

@mweststrate yep, that make sense. I have the same picture in my head.

from mobx-state-tree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.