Comments (4)
One slightly unrelated thing is that you're using accountSchema
before it is initialized, TS throws a clear compilation error on this:
Block-scoped variable 'accountSchema' used before its declaration.ts(2448)
Instead of using the schemas directly I personally created schema builder generic functions that accepts a schema as a parameter, that way you can create recursive types.
const userSchemaBuilder = <T extends TSchema>(reference: T) =>
Type.Object({
id: Type.String({ format: "uuid" }),
email: Type.String({ format: "email" }),
account: Type.Optional(reference),
});
const accountSchemaBuilder = <T extends TSchema>(reference: T) =>
Type.Object({
id: Type.String({ format: "uuid" }),
name: Type.String(),
users: Type.Optional(Type.Array(reference)),
});
const userSchema = Type.Recursive((This) =>
userSchemaBuilder(accountSchemaBuilder(This))
);
const accountSchema = Type.Recursive((This) =>
accountSchemaBuilder(userSchemaBuilder(This))
);
type user = Static<typeof userSchema>;
type account = Static<typeof accountSchema>;
It's a little bit of a dependency injectionish' pattern, it works both for the types and the schemas and the schemas can be isolated to their own modules this way. I haven't benchmarked it so I don't know what memory or processor impact it has, because technically some schemas are duplicated.
What would be nice to figure out is to how to make the builder functions expect a certain schema shape from the parameter, because right now nothing prohibits you from passing any kind of schema.
from typebox.
It's a little bit of a dependency injectionish' pattern, it works both for the types and the schemas and the schemas can be isolated to their own modules this way. I haven't benchmarked it so I don't know what memory or processor impact it has, because technically some schemas are duplicated.
Your implementation for mutual recursive types is possibly the best representation possible today (and thanks for writing that up!). I've been searching for better patterns but generally blocked by the "order of definition" problem. In terms of overhead, there is a little bit of duplication of the schematics for either entry point, but should be relatively negligible. As for your implementation, it's a really good one and I might include in the examples
for future reference.
I don't really see any way around this, as any circular references will need to use Type.Recursive which is not going to work in feathersjs unless there are changes made there.
Feathers is on an older version of TypeBox, but the Recursive types should still work (I believe the version they are on have the current implementation of Recursive types). Internally, Feathers is using Ajv for validation, the following is a quick code snippet you can test TypeBox + Ajv in isolation.
import { Type, TSchema, Static } from '@sinclair/typebox'
import Ajv from 'ajv'
const __A = <T extends TSchema>(reference: T) => Type.Object({
b: Type.Optional(reference),
}, { additionalProperties: false })
const __B = <T extends TSchema>(reference: T) => Type.Object({
a: Type.Optional(reference),
}, { additionalProperties: false })
// ....
type A = Static<typeof A>;
type B = Static<typeof B>;
const A = Type.Recursive((This) => __A(__B(This)))
const B = Type.Recursive((This) => __B(__A(This)))
const ajv = new Ajv()
console.log(ajv.validate(A, {
b: {
a: {
b: {
a: {
b: {
// optional a
}
}
}
}
}
}))
You may be able to rig above A/B
type into feathers and test. Technically speaking, this should work. If you can do this and just let me know how things work out, that would be great (helps provide me a bit more insight into Feathers usage)
Cheers
S
from typebox.
Thanks @truumahn for the answer.
The solution however causes more downstream issues in the context I'm using it in.
I've thus also asked my question here:
feathersjs/feathers#3472
As the context is clearly important here.
I don't really see any way around this, as any circular references will need to use Type.Recursive
which is not going to work in feathersjs unless there are changes made there.
Feel free to add more if you've got ideas, thanks!
from typebox.
@EmileSpecs Heya,
Might close off this issue here and track things over at feathersjs/feathers#3473. The above solution provided by @truumahn should be supported in Ajv, so to investigate things further, would likely need to see a repro project using Feathers (as I'm not clear why the mutual type wouldn't work there). I'll be available for comment on feathersjs/feathers#3473 if I can provide assistance there, so feel free to ping me there if I can offer any insights.
Will close off for now.
All the best!
S
from typebox.
Related Issues (20)
- UUID not accpeted as string HOT 2
- How to import just Decode? HOT 4
- How to check if a schema conforms? HOT 2
- email validation doesn't work? HOT 2
- `Value.Cast` casts to wrong schema when called with union of `Refs` HOT 1
- Support for `definitions`? HOT 1
- Cannot reference a recursive type HOT 7
- Cannot use .map() inside Union with Literal HOT 2
- Nested `Type.Intersect` Errors
- Incorrect inferred type for the empty object `Type.Object({})`. HOT 3
- `unevaluatedProperties` failed verification when combined with `Type.Intersect` and `Type.Union` HOT 2
- Types for both CJS and ESM cause ambiguity and prevent correct type resolution. HOT 7
- unique id for schemas generated with Type.Object() HOT 5
- Custom paths for custom validation errors
- Composite Union type does not work HOT 2
- Type.Recursive does not work with Type.Transform for StaticDecode
- Issue regarding nested objects when using Value.Default HOT 4
- Enum type inferred as never when using Type.Mapped() HOT 2
- Should `IsValueType` guard consider `Date`?
- ESNext target HOT 1
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.