Issues Converting Schema to JsonSchema with Custom Annotations

👋 Hello fellow effecter ❤️,
I'm currently facing an issue related to the schema package and I'm hoping someone could provide some guidance.

Here's a brief overview of my requirements:

1. I need to convert an existing schema to a JsonSchema.
2. I want to preserve existing custom annotations during this conversion.

The problem I'm encountering is that while converting schema primitives such as Schema.String or Schema.Number with custom annotations to JsonSchema everything works perfectly, whereas the conversion of custom schema types or composed ones seems to strip away my custom annotations.

To illustrate my issue, here are some code snippets:

// work.ts
export class Role extends S.Class<Role>('Role')({
  title0: S.compose(S.Trim, S.NonEmpty).annotations({
    title: 'title0',
    description: 'title0 custom description',
    examples: ['example title0'],
  }),

  tile1: S.String.annotations({
    title: 'title1',
    description: 'title1 custom description',
    examples: ['example title1'],
  }),
  // ... other attributes omitted for brevity
}) {}


// work.spec.ts
describe('roles', () => {
  test('toJsonSchema', () => {
    const jsonSchema = JSONSchema.make(S.encodedSchema(Role))
    const serializedJsonSchema = JSON.stringify(jsonSchema, null, 2)
    expect(serializedJsonSchema).toMatchFileSnapshot('role-schema.spec.json')
  })
})


// role-schema.spec.json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
 // [...]
  "properties": {
    "tile1": {
      "type": "string",
      "description": "title1 custom description",
      "title": "title1",
      "examples": [
        "example title1"
      ]
    },
    "title0": {
      "type": "string",
      "description": "a string",
      "title": "string"
    },
  }
}


I expect the schema defined by S.compose(S.Trim, S.NonEmpty).annotations({/* ...custom annotation */ }) to respect my custom annotations. However, this is not the case.
Was this page helpful?