-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON Schema: change default behavior for property signatures containi… (
- Loading branch information
Showing
3 changed files
with
136 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
"@effect/schema": patch | ||
--- | ||
|
||
JSON Schema: change default behavior for property signatures containing `undefined` | ||
|
||
Changed the default behavior when encountering a required property signature whose type contains `undefined`. Instead of raising an exception, `undefined` is now pruned and **the field is set as optional**. | ||
|
||
Before | ||
|
||
```ts | ||
import { JSONSchema, Schema } from "@effect/schema" | ||
|
||
const schema = Schema.Struct({ | ||
a: Schema.NullishOr(Schema.Number) | ||
}) | ||
|
||
const jsonSchema = JSONSchema.make(schema) | ||
console.log(JSON.stringify(jsonSchema, null, 2)) | ||
/* | ||
throws | ||
Error: Missing annotation | ||
at path: ["a"] | ||
details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation | ||
schema (UndefinedKeyword): undefined | ||
*/ | ||
``` | ||
|
||
Now | ||
|
||
```ts | ||
import { JSONSchema, Schema } from "@effect/schema" | ||
|
||
const schema = Schema.Struct({ | ||
a: Schema.NullishOr(Schema.Number) | ||
}) | ||
|
||
const jsonSchema = JSONSchema.make(schema) | ||
console.log(JSON.stringify(jsonSchema, null, 2)) | ||
/* | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"required": [], // <=== empty | ||
"properties": { | ||
"a": { | ||
"anyOf": [ | ||
{ | ||
"type": "number" | ||
}, | ||
{ | ||
"$ref": "#/$defs/null" | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"$defs": { | ||
"null": { | ||
"const": null | ||
} | ||
} | ||
} | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters