Comments (11)
Thanks for the provided example, I'll add it to the unit tests
from objectmodel.
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.
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:
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.
Thanks for the report !
Fixed and delivered in v4.4.2
from objectmodel.
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:
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.
Possibly, what is the problem on your production code ? I need to add unit tests to cover the issue
from objectmodel.
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)
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.
Thanks for your help!
from objectmodel.
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.
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.
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)
- SyntaxError: Unexpected token 'export' HOT 2
- Optional array model cause duplication in other instances of a model with default HOT 3
- Feature Request: Code Suggestion/IntelliSense on IDE (Jetbrain Webstorm) HOT 4
- Package objectmodel has been ignored because it contains invalid configuration HOT 2
- Problem Extending Typescript Class HOT 5
- Consider creating ADL serializer to generate ObjectModel? HOT 1
- How to nest SealedModels? HOT 8
- How to add custom methods to a Model? HOT 1
- Working with VSCode Intellisense HOT 2
- Question: Is it possible to skip more detailed checks if initial type check fails? HOT 6
- [email protected]: The engine "node" is incompatible with this module HOT 3
- Default for Nested Models HOT 1
- Load Json to Object HOT 1
- Typescript expected number of arguments when using defaultTo() HOT 4
- get objectmodel added to typia benchmarks HOT 4
- Nice Project HOT 1
- How can i return a « cleaned » object ? HOT 2
- Use in Typescript ? HOT 2
- pre/post process some datas ? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objectmodel.