Giter VIP home page Giter VIP logo

Comments (11)

sylvainpolletvillard avatar sylvainpolletvillard commented on June 10, 2024 1

Thanks for the provided example, I'll add it to the unit tests

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on June 10, 2024

Seems to work as intended on last version:
https://codesandbox.io/p/sandbox/happy-dirac-3yh4d7?file=%2Findex.js&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A23%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A23%7D%5D

Could you provide a reproducable example in a Codesandbox/Stackblitz/other ?

from objectmodel.

juanjoseruiz avatar juanjoseruiz commented on June 10, 2024

Apologies for the confusion, I did a mistake when trying to isolate an example from the actual code.

The current model is a bit more sophisticated, and the problem seems to happen when nesting more than one Model. Here is the updated version:

https://codesandbox.io/p/sandbox/wizardly-night-hcv89r?file=%2Findex.js&selection=%5B%7B%22endColumn%22%3A11%2C%22endLineNumber%22%3A5%2C%22startColumn%22%3A11%2C%22startLineNumber%22%3A5%7D%5D

const { Model } = require("objectmodel");

const Address = new Model({
  street: String,
  number: String,
}).defaultTo({
  number: "unknown number",
});

const Person = new Model({
  name: String,
  address: Address,
}).defaultTo({
  name: "Unknown name",
});

const Register = new Model({
  person: Person,
});

const reg = new Register({ person: { address: { street: "unknown street" } } });
console.log(reg.person.address.number);
console.log(JSON.stringify(reg));

Getting an error right now:

TypeError: expecting person.address.number to be String, got undefined

If I make number property from Address optional, then it works as expected and outputs the default.

const Address = new Model({
  street: String,
  number: [String],
}).defaultTo({
  number: "unknown number",
});

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on June 10, 2024

Thanks for the report !
Fixed and delivered in v4.4.2

from objectmodel.

juanjoseruiz avatar juanjoseruiz commented on June 10, 2024

Thanks for the quick fix! I verified and I can confirm this fixes the problem with the provided example.

Unfortunately, this is still not working for the production code, which has different nested types (Objects / Arrays). I was playing with the code, and this seems to do the trick for me:

a2106ce

Could you please have a look to see if that makes sense? I'm just propagating that property to the rest of places where it may be needed, but probably I am missing something else.

Thanks!
Juanjo.

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on June 10, 2024

Possibly, what is the problem on your production code ? I need to add unit tests to cover the issue

from objectmodel.

juanjoseruiz avatar juanjoseruiz commented on June 10, 2024

The problem is the same, the defaults are not applied / validation errors happens with several Model / ArrayModel / BasicModel nested.

I've slightly modified the example using the latest version so that you can check it (just substitute a Model by ArrayModel)

https://codesandbox.io/p/sandbox/wizardly-night-hcv89r?file=%2Findex.js&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A9%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A9%7D%5D

Please, let me know if you need any further examples, but I think this one should cover the missing parts from the previous one.

Thanks and best regards,
Juanjo.

from objectmodel.

juanjoseruiz avatar juanjoseruiz commented on June 10, 2024

Thanks for your help!

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on June 10, 2024

After investigation, your second problem comes from a misunderstanding of ArrayModel#defaultTo ; defaultTo defines the default content of the array, not the defaults of the array item model.

Here is how to fix it: https://codesandbox.io/p/sandbox/quiet-http-8vszie?file=%2Findex.js&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A28%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A1%7D%5D

const Address = new Model({
  street: String,
  number: String,
}).defaultTo({
  number: "unknown number",
});

const AddressList = new ArrayModel(Address);

const Person = new Model({
  name: String,
  address: AddressList,
}).defaultTo({
  name: "Unknown name",
});

from objectmodel.

sylvainpolletvillard avatar sylvainpolletvillard commented on June 10, 2024

To prevent this kind of mistake, I added type-checking validation on the default value provided.

Now in v4.4.3, you would have this error message instead when passing a wrong value to defaultTo :

expecting Array, got Object {
	number: "unknown number" 
}

from objectmodel.

juanjoseruiz avatar juanjoseruiz commented on June 10, 2024

Yes, the example was not fully accurate. After your changes in 4.4.3 now my code is working as previously.

Thanks!

from objectmodel.

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.