Comments (3)
Any updates here?
from typebox.
@seppelandrio Hi, Apologies for the delay (have been somewhat busy of late)
So, the issue here is that TypeBox clones schematic when you compose them. It does this to ensure manual modifications to types do not overwrite the properties of types elsewhere.
const A = Type.Number()
const T = Type.Object({
a: A // cloned / shared schematic
})
A.foo = 'hello'
T.properties.a.foo = 'world'
console.log(A) // { type: 'number', foo: 'hello', [Symbol(TypeBox.Kind)]: 'Number' }
console.log(T.properties.a) // { type: 'number', foo: 'world', [Symbol(TypeBox.Kind)]: 'Number' }
So from this, when you specify a class instance for the default
property, TypeBox will naively attempt to clone that class instance as an Object type. However when doing so, it will loose track of the instance type (so CustomClass
) and yield a empty {}
.
Unfortunately, I'm not sure there's much TypeBox can do here to ensure the class instance type is retained during composition clone. The best advise I can offer at this stage is to avoid using class instances for default values (or any schema property). Ideally, you should aim to ensure all properties passed to schematics are vanilla objects, arrays, strings, numbers, booleans, etc and avoid classes and functions being assigned. This is generally a good idea as it means your schematics can be serialized correctly (noting classes and functions do not serialize as Json).
It might be possible to implement a specialized clone for class instances (ill look into it), but I'm not sure it can be done safely without also cloning the class instance prototype. Ideally i'd like to have TypeBox's clone work a little closer to JavaScript's existing structuredClone
function, but as TypeBox does implement some extras for Symbol handling, it's not out of the realms of possibility to include something to also handle class instance cloning, it's just that additional functionality for clone isn't anticipated in the short to medium term...
I think for now I will close off this issue for the time being as class instance cloning is somewhat out of scope (at least for now) and the recommendation is just to avoid using class instances on types. Would be happy to discuss things further on this thread though (or GH discussion thread). To get movement on adding class instance cloning, would need to see a robust implementation of it first. If you wanted to submit a code snippet of it, would be happy to review and experiment for possible inclusion at some point later down the road.
Hope this helps
S
from typebox.
@sinclairzx81 Hi,
this time sorry for the delay from my side, needed to focus on different projects.
I agree that writing a class cloner will be somewhat complex and probably not worth the effort. What I had in mind was to provide either:
- modifying the TypeRegistry to be able to optionally also be able to provide either a create or a clone function
- extending the TSchema type to also support one of those functions
With those 2 approaches you could easily add custom types by just providing the functionality you need.
About which approach to choose I am quite unsure as I am not that familiar with the codebase. If you think that one of those options seems worth a try I can also try to provide a PR implementing the approach.
from typebox.
Related Issues (20)
- Reduce unnecessary cloning in `Value.Convert` HOT 2
- [typebox-codegen] Option to emit types wrapped in arrow function? HOT 1
- Type.Mapped does not handle optional properties the same as Typescript HOT 2
- Add changelog or create a new release for each tag HOT 3
- TypeCheck.Decode return type inferred incorrectly? HOT 12
- Feature request: functions as defaults HOT 3
- Composite doesn't check additionalProperties HOT 2
- Custom TypeRegistry and TypeFormat HOT 1
- [feat] describe a object with pattern key, and literal key at the same time HOT 1
- Easier/general way to implement Typescript's "key remapping via `as`" approach HOT 1
- Overriding Schema Title HOT 1
- Cannot find module "./assert/index.mjs" HOT 1
- Encode is called twice on Type.Transform when inner type is Type.Intersection HOT 1
- StaticDecode cannot handle union of literals, while Static can HOT 1
- How to validate raw json schema HOT 2
- Why is Nullable not a first class Type? HOT 4
- Value.Convert does not seem to convert sometimes HOT 3
- `Value.Default` corrupts data in unions
- Is there a way to coerce when creating a schema? HOT 2
- Avoid losing precision when converting to bigints HOT 6
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 typebox.