GithubHelp home page GithubHelp logo

Comments (3)

seppelandrio avatar seppelandrio commented on September 28, 2024

Any updates here?

from typebox.

sinclairzx81 avatar sinclairzx81 commented on September 28, 2024

@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.

seppelandrio avatar seppelandrio commented on September 28, 2024

@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)

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.