diff --git a/.changeset/breezy-rockets-buy.md b/.changeset/breezy-rockets-buy.md new file mode 100644 index 000000000..e90fbaed7 --- /dev/null +++ b/.changeset/breezy-rockets-buy.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": patch +--- + +Schema: add jsonSchema annotation helper diff --git a/docs/modules/Schema.ts.md b/docs/modules/Schema.ts.md index 2bd90ec6a..0fb92290b 100644 --- a/docs/modules/Schema.ts.md +++ b/docs/modules/Schema.ts.md @@ -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) @@ -671,6 +672,16 @@ export declare const identifier: (identifier: AST.IdentifierAnnotation) => (self: Schema) => Schema +``` + +Added in v1.0.0 + ## message **Signature** diff --git a/src/Schema.ts b/src/Schema.ts index ef7535756..5b13fc7da 100644 --- a/src/Schema.ts +++ b/src/Schema.ts @@ -1452,6 +1452,14 @@ export const documentation = (documentation: AST.DocumentationAnnotation) => (self: Schema): Schema => make(AST.setAnnotation(self.ast, AST.DocumentationAnnotationId, documentation)) +/** + * @category annotations + * @since 1.0.0 + */ +export const jsonSchema = + (jsonSchema: AST.JSONSchemaAnnotation) => (self: Schema): Schema => + make(AST.setAnnotation(self.ast, AST.JSONSchemaAnnotationId, jsonSchema)) + // --------------------------------------------- // string filters // --------------------------------------------- diff --git a/test/Schema/annotations.test.ts b/test/Schema/annotations.test.ts index b682dffe3..90a204efa 100644 --- a/test/Schema/annotations.test.ts +++ b/test/Schema/annotations.test.ts @@ -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