Effect CommunityEC
Effect Community•2y ago•
1 reply
suddenlyGiovanni

JSON Schema Override in Attribute Object

stupid question incoming 🛬...

Is it intentional that when a new jsonSchema attribute gets attached to the attribute obj, said jsonSchema completely override the existing properties?

I have an example:

expect(
  JSONSchema.make(
    NonEmpty.annotations({
      title: 'custom title',
      description: 'custom description',
      examples: ['custom example'],
      jsonSchema: {
        foo: 'bar',
      }
    }),
  ),
).toMatchSnapshot()


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "custom description",
  "examples": ["custom example"],
- "minLength": 1,
+ "foo": "bar",
  "title": "custom title",
  "type": "string"
}


I would have expected the intersections of non-overlapping records?

for example, in the case of the minLength filter, which power the NonEmpty string constructor, it could be done like this, right?

export const minLength = <A extends string>(
  minLength: number,
  annotations?: Annotations.Filter<A>
) =>
<I, R>(self: Schema<A, I, R>): Schema<A, I, R> =>
  self.pipe(
    filter(
      (a): a is A => a.length >= minLength,
      {
        typeId: MinLengthTypeId,
        description: `a string at least ${minLength} character(s) long`,
        jsonSchema: { 
+          ...annotations?.jsonSchema, 
          minLength 
        },
        ...annotations
      }
    )
  )


But maybe the the issue lies in the export type JSONSchemaAnnotation = object , as it allows an arbitrary nested structure...
Was this page helpful?