Coder Social home page Coder Social logo

Circular references about typebox HOT 4 CLOSED

EmileSpecs avatar EmileSpecs commented on June 15, 2024
Circular references

from typebox.

Comments (4)

truumahn avatar truumahn commented on June 15, 2024 2

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.

sinclairzx81 avatar sinclairzx81 commented on June 15, 2024 1

@truumahn

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.

@EmileSpecs

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.

EmileSpecs avatar EmileSpecs commented on June 15, 2024

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.

sinclairzx81 avatar sinclairzx81 commented on June 15, 2024

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

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.