-
-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSONSchema.make does not have same behaviour as JSONSchema.to => erasing properties & not working for some Schema types #3283
Comments
That documentation is outdated and part of an archived repository. The updated documentation can be found here: https://github.com/Effect-TS/effect/tree/main/packages/schema Regarding 1. I can confirm that there's a bug. However, the expected output should be: import { JSONSchema, Schema } from "@effect/schema"
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)
/*
{
'$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' },
description: { type: 'string' }
},
additionalProperties: false
}
*/ The reason is that Given your example, it seems you mainly want to set the titles and descriptions for the fields of the struct rather than their types. Therefore, I recommend adding the annotation to the field (via the import { JSONSchema, Schema } from "@effect/schema"
const schema = Schema.Struct({
firstName: Schema.propertySignature(Schema.String).annotations({
title: "First name"
}),
lastName: Schema.propertySignature(Schema.Trim.pipe(Schema.nonEmpty())).annotations({
title: "Last Name"
}),
description: Schema.propertySignature(Schema.Trim.pipe(Schema.nonEmpty())).annotations({
title: "Description"
})
})
const jsonSchema = JSONSchema.make(schema)
console.log(jsonSchema)
/*
{
'$schema': 'http://json-schema.org/draft-07/schema#',
type: 'object',
required: [ 'firstName', 'lastName', 'description' ],
properties: {
firstName: { type: 'string', title: 'First name' },
lastName: { type: 'string', title: 'Last Name' },
description: { type: 'string', title: 'Description' }
},
additionalProperties: false
}
*/ This approach not only works around the current bug but is also more semantically correct ( Regarding 2. the problem is that {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [],
"properties": {
"numberOfElements": {
"anyOf": [
{
"type": "number"
},
{
"$ref": "#/$defs/null"
}
]
}
},
"additionalProperties": false,
"$defs": {
"null": {
"const": null
}
}
} This approach might indeed be a better default than erroring out. Is this correct? However, please note that the output won't be {
"anyOf": [
{
"type": "number"
},
{
"$ref": "#/$defs/null"
}
]
} This is because the compiler still needs to handle the Finally, the following code: import { JSONSchema, Schema } from "@effect/schema"
const schema = Schema.Struct({
startDate: Schema.DateFromSelf
})
const jsonSchema = JSONSchema.make(schema)
console.log(jsonSchema) raises an error because |
Thanks a lot for your quick answer!
About
Yes, exactly. About |
I mean that in the result you just gave, the 'type' is set in
Which is better 👍
it is not the expected behaviour. |
Oh, you're right, sorry. I saw the "type" property in the output correctly because I was testing with the development branch where I'm working on the bug fix. I'm going to release the patch so you can test it with the updated code. |
Patch released.
Exactly, the |
Thanks! |
Closing for now. Feel free to reopen if any further issues arise. |
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:The output is:
What I do:
The output is:
What is the expected behavior?
According to the documentation here, here is the output I would expect:
I would expect:
What do you see instead?
// Already answered above
Additional information
I would like to know why:
JSONSchema.to
=> the previous behaviour would be very beneficial for my needThanks a lot for your time!
The text was updated successfully, but these errors were encountered: