Skip to content

Commit

Permalink
remove default json schema annotations from string, number and boolean (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Sep 24, 2024
1 parent ec37566 commit fd83d0e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 29 deletions.
47 changes: 47 additions & 0 deletions .changeset/pink-files-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
"@effect/schema": patch
---

Remove default json schema annotations from string, number and boolean.

Before

```ts
import { JSONSchema, Schema } from "@effect/schema"

const schema = Schema.String.annotations({ examples: ["a", "b"] })

console.log(JSON.stringify(JSONSchema.make(schema), null, 2))
/*
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"description": "a string",
"title": "string",
"examples": [
"a",
"b"
]
}
*/
```

After

```ts
import { JSONSchema, Schema } from "@effect/schema"

const schema = Schema.String.annotations({ examples: ["a", "b"] })

console.log(JSON.stringify(JSONSchema.make(schema), null, 2))
/*
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"examples": [
"a",
"b"
]
}
*/
```
32 changes: 20 additions & 12 deletions packages/schema/src/JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,20 @@ const isOverrideAnnotation = (jsonSchema: JsonSchema7): boolean => {
("enum" in jsonSchema) || ("$ref" in jsonSchema)
}

const removeDefaultJsonSchemaAnnotations = (out: JsonSchema7, def: AST.AST, ast: AST.AST): JsonSchema7 => {
if (def === ast) {
return out
}
const jsonSchemaAnnotations = getJsonSchemaAnnotations(ast)
if (jsonSchemaAnnotations["title"] === def.annotations[AST.TitleAnnotationId]) {
delete jsonSchemaAnnotations["title"]
}
if (jsonSchemaAnnotations["description"] === def.annotations[AST.DescriptionAnnotationId]) {
delete jsonSchemaAnnotations["description"]
}
return merge(out, jsonSchemaAnnotations)
}

const go = (
ast: AST.AST,
$defs: Record<string, JsonSchema7>,
Expand Down Expand Up @@ -371,18 +385,12 @@ const go = (
return merge(anyJsonSchema, getJsonSchemaAnnotations(ast))
case "ObjectKeyword":
return merge(objectJsonSchema, getJsonSchemaAnnotations(ast))
case "StringKeyword": {
const out: JsonSchema7 = { type: "string" }
return ast === AST.stringKeyword ? out : merge(out, getJsonSchemaAnnotations(ast))
}
case "NumberKeyword": {
const out: JsonSchema7 = { type: "number" }
return ast === AST.numberKeyword ? out : merge(out, getJsonSchemaAnnotations(ast))
}
case "BooleanKeyword": {
const out: JsonSchema7 = { type: "boolean" }
return ast === AST.booleanKeyword ? out : merge(out, getJsonSchemaAnnotations(ast))
}
case "StringKeyword":
return removeDefaultJsonSchemaAnnotations({ type: "string" }, AST.stringKeyword, ast)
case "NumberKeyword":
return removeDefaultJsonSchemaAnnotations({ type: "number" }, AST.numberKeyword, ast)
case "BooleanKeyword":
return removeDefaultJsonSchemaAnnotations({ type: "boolean" }, AST.booleanKeyword, ast)
case "BigIntKeyword":
throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast))
case "SymbolKeyword":
Expand Down
26 changes: 9 additions & 17 deletions packages/schema/test/JSONSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ schema (Declaration): DateFromSelf`
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "void"
})
expectJSONSchema(Schema.Void.annotations({}), {
"$id": "/schemas/void",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "void"
})
})

it("Object", () => {
Expand Down Expand Up @@ -215,9 +220,7 @@ schema (Declaration): DateFromSelf`
})
expectJSONSchema(Schema.String.annotations({}), {
"$schema": "http://json-schema.org/draft-07/schema#",
type: "string",
description: "a string",
title: "string"
type: "string"
})
})

Expand All @@ -228,9 +231,7 @@ schema (Declaration): DateFromSelf`
}, false)
expectJSONSchema(Schema.Number.annotations({}), {
"$schema": "http://json-schema.org/draft-07/schema#",
type: "number",
description: "a number",
title: "number"
type: "number"
}, false)
})

Expand All @@ -241,9 +242,7 @@ schema (Declaration): DateFromSelf`
})
expectJSONSchema(Schema.Boolean.annotations({}), {
"$schema": "http://json-schema.org/draft-07/schema#",
type: "boolean",
description: "a boolean",
title: "boolean"
type: "boolean"
})
})

Expand Down Expand Up @@ -544,7 +543,6 @@ schema (Declaration): DateFromSelf`
"items": [
{
"type": "string",
"title": "string",
"description": "e"
},
{
Expand Down Expand Up @@ -1442,8 +1440,6 @@ schema (Suspend): <suspended schema>`
expectJSONSchema(Schema.String.annotations({ examples: ["a", "b"] }), {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"title": "string",
"description": "a string",
"examples": ["a", "b"]
})
})
Expand All @@ -1452,8 +1448,6 @@ schema (Suspend): <suspended schema>`
expectJSONSchema(Schema.String.annotations({ default: "" }), {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"title": "string",
"description": "a string",
"default": ""
})
})
Expand Down Expand Up @@ -1572,9 +1566,7 @@ schema (Suspend): <suspended schema>`
"$ref": "#/$defs/Name",
"$defs": {
"Name": {
"type": "string",
"description": "a string",
"title": "string"
"type": "string"
}
}
})
Expand Down

0 comments on commit fd83d0e

Please sign in to comment.