Skip to content

Commit

Permalink
Schema: JSONSchema - rearrange handling of surrogate annotations to o… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 21, 2024
1 parent 80271bd commit e4ba97d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-crews-train.md
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

JSONSchema: rearrange handling of surrogate annotations to occur after JSON schema annotations
8 changes: 4 additions & 4 deletions packages/schema/src/JSONSchema.ts
Expand Up @@ -278,10 +278,6 @@ export const DEFINITION_PREFIX = "#/$defs/"
const get$ref = (id: string): string => `${DEFINITION_PREFIX}${id}`

const go = (ast: AST.AST, $defs: Record<string, JsonSchema7>, handleIdentifier: boolean = true): JsonSchema7 => {
const surrogate = AST.getSurrogateAnnotation(ast)
if (Option.isSome(surrogate)) {
return go(surrogate.value, $defs, handleIdentifier)
}
const hook = AST.getJSONSchemaAnnotation(ast)
if (Option.isSome(hook)) {
const handler = hook.value as JsonSchema7
Expand All @@ -298,6 +294,10 @@ const go = (ast: AST.AST, $defs: Record<string, JsonSchema7>, handleIdentifier:
}
return handler
}
const surrogate = AST.getSurrogateAnnotation(ast)
if (Option.isSome(surrogate)) {
return go(surrogate.value, $defs, handleIdentifier)
}
if (handleIdentifier) {
const identifier = AST.getJSONIdentifier(ast)
if (Option.isSome(identifier)) {
Expand Down
30 changes: 30 additions & 0 deletions packages/schema/test/JSONSchema.test.ts
Expand Up @@ -1543,6 +1543,36 @@ describe("JSONSchema", () => {
})
})

it("should support make(S.typeSchema(Class))", () => {
class A extends S.Class<A>("A")({ a: S.String }) {}
const jsonSchema = JSONSchema.make(S.typeSchema(A))
expect(jsonSchema).toEqual({
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"a"
],
"properties": {
"a": {
"type": "string",
"description": "a string",
"title": "string"
}
},
"additionalProperties": false
})
})

it("should support make(S.typeSchema(Class)) with custom annotation", () => {
class A extends S.Class<A>("A")({ a: S.String }, {
jsonSchema: {}
}) {}
const jsonSchema = JSONSchema.make(S.typeSchema(A))
expect(jsonSchema).toEqual({
"$schema": "http://json-schema.org/draft-07/schema#"
})
})

it("should support make(S.encodedSchema(Class))", () => {
class A extends S.Class<A>("A")({ a: S.String }) {}
const jsonSchema = JSONSchema.make(S.encodedSchema(A))
Expand Down

0 comments on commit e4ba97d

Please sign in to comment.