sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
5k stars 158 forks source link

Overriding Schema Title #980

Closed chuck-flowers closed 2 months ago

chuck-flowers commented 2 months ago

I've discovered an interesting issue. It seems that once the title for a schema is set, it can't be overridden through composition function like Omit.

Steps to Reproduce

import { Type } from  '@sinclair/typebox';

const a = Type.Object({
  x: Type.Integer(),
  y: Type.Integer(),
  z: Type.Integer()
}, {
  title: 'A'
});

const b = Type.Omit(a, ['z'], {
  title: 'B'
});

console.log({ a, b });

Expected Result

{
  a: {
    title: 'A',
    type: 'object',
    properties: { x: [Object], y: [Object], z: [Object] },
    required: [ 'x', 'y', 'z' ],
    [Symbol(TypeBox.Kind)]: 'Object'
  },
  b: {
    title: 'B',
    type: 'object',
    properties: { x: [Object], y: [Object] },
    required: [ 'x', 'y' ],
    [Symbol(TypeBox.Kind)]: 'Object'
  }
}

Actual Result

{
  a: {
    title: 'A',
    type: 'object',
    properties: { x: [Object], y: [Object], z: [Object] },
    required: [ 'x', 'y', 'z' ],
    [Symbol(TypeBox.Kind)]: 'Object'
  },
  b: {
    title: 'A',
    type: 'object',
    properties: { x: [Object], y: [Object] },
    required: [ 'x', 'y' ],
    [Symbol(TypeBox.Kind)]: 'Object'
  }
}

Potential Root Cause

I suspect that the root cause is the ordering of schema and options on this line

If the ordering of the spread was reversed, I think the expected behavior would occur.

sinclairzx81 commented 2 months ago

@chuck-flowers Hi, Thanks for reporting. I have published a fix for this on 0.33.8.

The fix was also applied to Pick, Omit, Partial and Required.

Will close up this issue for now, but if you run into any problems, feel free to ping on this thread.

All the best S