Comments (3)
@jpenna Hi,
When using Type.Ref, the schematics only hold a $ref: '<id>'
to the target schema. What this means is when you try to Decode the associated value, the Decode function has no way to locate the target schema. Because TypeBox does not track identifiers internally, you will need to track the reference type and pass it to Decode.
export const Composited = Type.Composite([
Type.Object({ token: Type.String() }),
Type.Object({ user: Type.Ref(UserPublicSchema) }),
]);
const R = Value.Decode(Composited, [UserPublicSchema], {})
// ^ pass interior referenced type here
Many of the TypeBox functions provide a secondary overload to pass references (including Encode, Decode, Check, Default, Clean, etc). It is possible to implement mechanisms to help keep track of references (if you are making heavy use of them), but TypeBox generally leaves this as a implementation detail for users.
Looking at your types though, you may not need a Type.Ref in this case, a JavaScript reference (or variable assignment) may work better and avoids the need to track a reference.
export const Composited = Type.Composite([
Type.Object({ token: Type.String() }),
Type.Object({ user: UserPublicSchema }), // using JS references may be better
]);
Hope this helps
S
from typebox.
@jpenna Hiya,
Might close off this issue as things are generally working as expected here. Just be mindful that Ref types don't store the actual schema (only references to them) and that you will need to keep track of the target schemas when passing them to functions (like Decode). In most cases, it's usually going to be easier to not use Ref (and just use JS references), and where Ref is mostly used to normalize schemas, usually for publishing them.
Will close off for now, but if you have any follow up questions, feel free to ping on this thread.
All the best
S
from typebox.
Thanks for the quick response! I understand what you are saying. The validation was done by Fastify, so I guess it wasn't passing the references over.
I changed it to use references instead. I preferred it the other way, so I'd have the "schema component" when I generated the API's client code, but that's OK.
Thanks you!
from typebox.
Related Issues (20)
- Trying to do codegen from Drizzle schemas HOT 1
- Make `default` and `examples` schema options properties type-safe HOT 2
- Indicate which files don't have "sideEffects" in package.json for improved tree shaking HOT 3
- Type.String().Optional() is accepted on ts-hint / ts-lint, and even build successfully; but of course, "Optional()" does not exists HOT 2
- Dynamic Template Literals can't be Mapped HOT 2
- How should TypeBox types be used with TS type predicates? HOT 2
- TypeCompiler.Compile incorrectly parsing `Record<any, any>` HOT 7
- Transform type keys in Record HOT 2
- Is it possible to add support to the Type.String additional properties for the enum keyword HOT 2
- Support for async validations? HOT 1
- Transform + Map/Index does not work HOT 4
- Update the error message for required properties HOT 3
- Covariance issue with `TypeCheck<TSchema>` and `TypeCheck<TObject<{}>>` HOT 1
- Default values in nested objects are not generated HOT 2
- Doubt on Record HOT 2
- TypeCompiler compile on Type.Strict(T) - Preflight validation check failed to guard for the given schema HOT 2
- Max call stack size exceeded HOT 1
- Value.Convert() doesn't work with nullable record HOT 1
- Variadic Any Typed function parameters 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 typebox.