Skip to content

Commit

Permalink
JSONSchema: represent never as { enum: [] } (#4108)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Dec 10, 2024
1 parent c2e9a88 commit 8d914e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
38 changes: 38 additions & 0 deletions .changeset/purple-pandas-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
"effect": patch
---

JSONSchema: represent `never` as `{ enum: [] }`

Before

```ts
import { JSONSchema, Schema } from "effect"

const schema = Schema.Never

console.log(JSON.stringify(JSONSchema.make(schema), null, 2))
/*
throws:
Error: Missing annotation
details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation
schema (NeverKeyword): never
*/
```

After

```ts
import { JSONSchema, Schema } from "effect"

const schema = Schema.Never

console.log(JSON.stringify(JSONSchema.make(schema), null, 2))
/*
{
"enum": [],
"title": "never",
"$schema": "http://json-schema.org/draft-07/schema#"
}
*/
```
2 changes: 1 addition & 1 deletion packages/effect/src/JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ const go = (
case "VoidKeyword":
return { ...constVoid, ...getJsonSchemaAnnotations(ast) }
case "NeverKeyword":
throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast))
return { enum: [], ...getJsonSchemaAnnotations(ast) }
case "UnknownKeyword":
return { ...constUnknown, ...getJsonSchemaAnnotations(ast) }
case "AnyKeyword":
Expand Down
25 changes: 9 additions & 16 deletions packages/effect/test/Schema/JSONSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,6 @@ schema (UndefinedKeyword): undefined`
)
})

it("Never", () => {
expectError(
Schema.Never,
`Missing annotation
details: Generating a JSON Schema for this schema requires a "jsonSchema" annotation
schema (NeverKeyword): never`
)
})

it("Schema.Literal with a bigint literal", () => {
expectError(
Schema.Literal(1n),
Expand Down Expand Up @@ -366,6 +357,13 @@ details: Cannot encode Symbol(effect/Schema/test/a) key to JSON Schema`
})
})

it("Never", () => {
expectJsonSchemaAnnotations(Schema.Never, {
"enum": [],
"title": "never"
})
})

it("Any", () => {
expectJsonSchemaAnnotations(Schema.Any, {
"$id": "/schemas/any",
Expand All @@ -384,12 +382,8 @@ details: Cannot encode Symbol(effect/Schema/test/a) key to JSON Schema`
const jsonSchema: Root = {
"$id": "/schemas/object",
"anyOf": [
{
"type": "object"
},
{
"type": "array"
}
{ "type": "object" },
{ "type": "array" }
],
"description": "an object in the TypeScript meaning, i.e. the `object` type",
"title": "object"
Expand All @@ -409,7 +403,6 @@ details: Cannot encode Symbol(effect/Schema/test/a) key to JSON Schema`
const schema = Schema.Struct({})
const jsonSchema: Root = {
"$id": "/schemas/{}",

"anyOf": [{
"type": "object"
}, {
Expand Down

0 comments on commit 8d914e5

Please sign in to comment.