Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Schema: add jsonSchema annotation helper, closes #479 (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Oct 12, 2023
1 parent 56f5ac0 commit c80c94f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-rockets-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Schema: add jsonSchema annotation helper
11 changes: 11 additions & 0 deletions docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Added in v1.0.0
- [documentation](#documentation)
- [examples](#examples)
- [identifier](#identifier)
- [jsonSchema](#jsonschema)
- [message](#message)
- [title](#title)
- [bigint constructors](#bigint-constructors)
Expand Down Expand Up @@ -671,6 +672,16 @@ export declare const identifier: (identifier: AST.IdentifierAnnotation) => <I, A
Added in v1.0.0
## jsonSchema
**Signature**
```ts
export declare const jsonSchema: (jsonSchema: AST.JSONSchemaAnnotation) => <I, A>(self: Schema<I, A>) => Schema<I, A>
```
Added in v1.0.0
## message
**Signature**
Expand Down
8 changes: 8 additions & 0 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,14 @@ export const documentation =
(documentation: AST.DocumentationAnnotation) => <I, A>(self: Schema<I, A>): Schema<I, A> =>
make(AST.setAnnotation(self.ast, AST.DocumentationAnnotationId, documentation))

/**
* @category annotations
* @since 1.0.0
*/
export const jsonSchema =
(jsonSchema: AST.JSONSchemaAnnotation) => <I, A>(self: Schema<I, A>): Schema<I, A> =>
make(AST.setAnnotation(self.ast, AST.JSONSchemaAnnotationId, jsonSchema))

// ---------------------------------------------
// string filters
// ---------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions test/Schema/annotations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ describe("Schema/annotations", () => {
expect(S.isSchema(schema)).toEqual(true)
})

it("jsonSchema", () => {
const schema = S.string.pipe(S.jsonSchema({ type: "string" }))
expect(schema.ast.annotations).toEqual({
[AST.JSONSchemaAnnotationId]: { type: "string" },
[AST.TitleAnnotationId]: "string",
[AST.DescriptionAnnotationId]: "a string"
})
expect(S.isSchema(schema)).toEqual(true)
})

it("message as annotation options", async () => {
const schema =
// initial schema, a string
Expand Down

0 comments on commit c80c94f

Please sign in to comment.