Skip to content

JSONSchema.make does not have same behaviour as JSONSchema.to => erasing properties & not working for some Schema types #3283

@mignotju

Description

@mignotju

What version of Effect is running?

3.5.1

What steps can reproduce the bug?

Hello,
I am trying to use JSONSchema.make and I have 2 issues according to my expectations:

  1. Json schema properties erased:
const schema = Schema.Struct({
  firstName: Schema.String.annotations({
    title: 'First name',
  }),
  lastName: Schema.Trim.pipe(Schema.nonEmpty()),
  description: Schema.Trim.pipe(Schema.nonEmpty()).annotations({
    jsonSchema: { title: 'Description' },
  }),
});

const jsonSchema = JSONSchema.make(schema);
console.log(jsonSchema);

The output is:

{
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  required: [ 'firstName', 'lastName', 'description' ],
  properties: {
    firstName: { type: 'string', description: 'a string', title: 'First name' }, // ✅ Great
    lastName: { minLength: 1 }, // ❌ I would expect more information, see below in expected behaviour section
    description: { title: 'Description' } // ❌ I would expect more information, see below in expected behaviour section
  },
  additionalProperties: false
}
  1. Json schema not working for some Schema types
    What I do:
// a. Schema.NullishOf
const schema = Schema.Struct({
  numberOfElements: Schema.NullishOr(Schema.Number),
});
const jsonSchema = JSONSchema.make(schema);
console.log(jsonSchema);

OR
// b. Schema.DateFromSelf
const schema = Schema.Struct({
  startDate: Schema.DateFromSelf
});
const jsonSchema = JSONSchema.make(schema);
console.log(jsonSchema);

The output is:

// a. Schema.NullishOf

Missing annotation 
at path: ["numberOfElements"] 
details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation 
schema (UndefinedKeyword): undefined 

OR

// b. Schema.DateFromSelf

Missing annotation 
at path: ["startDate"] 
details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation 
schema (Declaration): DateFromSelf 

What is the expected behavior?

  1. Json schema properties erased:

According to the documentation here, here is the output I would expect:

{
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  required: [ 'firstName', 'lastName', 'description' ],
  properties: {
    firstName: { type: 'string', description: 'a string', title: 'First name' }, 
    lastName: { **type: 'string'**, minLength: 1 },
    description: { **type: 'string', title: 'Description'**, minLength: 1, } 
  },
  additionalProperties: false
}
  1. Json schema not working for some Schema types
    I would expect:
// a. Schema.NullishOf

{
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  required: [], // empty
  properties: {
    numberOfElements: { type: 'number' }, 
  },
  additionalProperties: false
}

// b. Schema.DateFromSelf

{
  '$schema': 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  required: ['startDate'], 
  properties: {
    startDate: { type: 'date' }, 
  },
  additionalProperties: false
}

What do you see instead?

// Already answered above

Additional information

I would like to know why:

  1. JSON schema properties are erased, whereas it was not the case before with JSONSchema.to => the previous behaviour would be very beneficial for my need
  2. It is not working with some schema types

Thanks a lot for your time!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions