Skip to content

Is it possible to create a recursive schema definition? #659

@timfrans

Description

@timfrans

Is it possible to create a recursive schema definition?

I stumbled on this issue when implementing a hierarchical scheme using recursion schemas.
To define our JSON schemas we use sinclair/typebox like so:

export const ArticleCategorySchemaTest = Type.Array(
  Type.Rec(
    (Self) =>
      Type.Object({
        sequenceNumber: Type.Number({ minimum: 1 }),
        subCategories: Type.Optional(Type.Array(Self)),
      }),

    { $id: 'ArticleCategoryTest' },
  ),
);

This generates following schema:

{
  kind: Symbol(ArrayKind),
  type: 'array',
  items: {
    '$id': 'ArticleCategoryTest',
    '$ref': 'ArticleCategoryTest#/$defs/self',
    '$defs': {
      self: {
        kind: Symbol(ObjectKind),
        type: 'object',
        properties: {
          sequenceNumber: { minimum: 1, kind: Symbol(NumberKind), type: 'number' },
          subCategories: {
            kind: Symbol(ArrayKind),
            type: 'array',
            items: { '$ref': 'ArticleCategoryTest#/$defs/self' },
            modifier: Symbol(OptionalModifier)
          }
        },
        required: [ 'sequenceNumber' ]
      }
    }
  }
}

When starting up the fastify server i get following error:

Failed building the serialization schema for ..., due to error Maximum call stack size exceeded

I've seen following issues:

I'm just not sure what casues this error.

Your Environment

  • node version: 16.13.0
  • fastify version: 3.23.0
  • os: Mac
  • fast-json-stringify: 2.7.13
  • sinclair/typebox: 0.21.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions