Skip to content

Commit

Permalink
Avoid setting a title annotation when applying branding
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 20, 2024
1 parent db94bca commit fdfe472
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
2 changes: 0 additions & 2 deletions packages/effect/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2993,8 +2993,6 @@ export const brand = <S extends Schema.AnyNoContext, B extends string | symbol>(
const ast = AST.annotations(
self.ast,
toASTAnnotations({
title: `${String(self.ast)} & Brand<${util_.formatUnknown(brand)}>`,
description: `${String(self.ast)} & Brand<${util_.formatUnknown(brand)}>`,
[AST.BrandAnnotationId]: annotation,
...annotations
})
Expand Down
17 changes: 14 additions & 3 deletions packages/effect/src/SchemaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2925,10 +2925,21 @@ export const rename = (ast: AST, mapping: { readonly [K in PropertyKey]?: Proper

const formatKeyword = (ast: AST): string => Option.getOrElse(getExpected(ast), () => ast._tag)

function getBrands(ast: Annotated): string {
return Option.match(getBrandAnnotation(ast), {
onNone: () => "",
onSome: (brands) => brands.map((brand) => ` & Brand<${util_.formatUnknown(brand)}>`).join("")
})
}

const getExpected = (ast: Annotated): Option.Option<string> => {
return getIdentifierAnnotation(ast).pipe(
Option.orElse(() => getTitleAnnotation(ast)),
Option.orElse(() => getDescriptionAnnotation(ast)),
Option.orElse(() => getAutoTitleAnnotation(ast))
Option.orElse(() =>
getTitleAnnotation(ast).pipe(
Option.orElse(() => getDescriptionAnnotation(ast)),
Option.orElse(() => getAutoTitleAnnotation(ast)),
Option.map((s) => s + getBrands(ast))
)
)
)
}
27 changes: 12 additions & 15 deletions packages/effect/test/Schema/Schema/brand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ describe("brand", () => {
})

describe("annotations", () => {
it("toString / format", () => {
const schema = S.Number.pipe(S.brand("A"))
expect(String(schema)).toBe(`number & Brand<"A">`)
expect(S.format(schema)).toBe(`number & Brand<"A">`)
})

it("using .annotations() twice", () => {
const schema = S.Number.pipe(S.brand("A"))
const annotatedSchema = schema.annotations({
Expand Down Expand Up @@ -56,15 +50,15 @@ describe("brand", () => {
const schema = S.Number.pipe(
S.int(),
S.brand("A", {
description: "an A brand"
description: "description"
})
)
expect(String(schema)).toBe(`description & Brand<"A">`)

expect(schema.ast.annotations).toEqual({
[AST.SchemaIdAnnotationId]: S.IntSchemaId,
[AST.BrandAnnotationId]: ["A"],
[AST.TitleAnnotationId]: `an integer & Brand<"A">`,
[AST.DescriptionAnnotationId]: "an A brand",
[AST.DescriptionAnnotationId]: "description",
[AST.JSONSchemaAnnotationId]: { type: "integer" }
})
})
Expand All @@ -74,15 +68,16 @@ describe("brand", () => {
S.int(),
S.brand("A"),
S.brand("B", {
description: "a B brand"
description: "description"
})
)

expect(String(schema)).toBe(`description & Brand<"A"> & Brand<"B">`)

expect(schema.ast.annotations).toEqual({
[AST.SchemaIdAnnotationId]: S.IntSchemaId,
[AST.BrandAnnotationId]: ["A", "B"],
[AST.TitleAnnotationId]: `an integer & Brand<"A"> & Brand<"B">`,
[AST.DescriptionAnnotationId]: "a B brand",
[AST.DescriptionAnnotationId]: "description",
[AST.JSONSchemaAnnotationId]: { type: "integer" }
})
})
Expand All @@ -94,14 +89,16 @@ describe("brand", () => {
S.int(),
S.brand(A),
S.brand(B, {
description: "a B brand"
description: "description"
})
)

expect(String(schema)).toBe("description & Brand<Symbol(A)> & Brand<Symbol(B)>")

expect(schema.ast.annotations).toEqual({
[AST.SchemaIdAnnotationId]: S.IntSchemaId,
[AST.BrandAnnotationId]: [A, B],
[AST.TitleAnnotationId]: "an integer & Brand<Symbol(A)> & Brand<Symbol(B)>",
[AST.DescriptionAnnotationId]: "a B brand",
[AST.DescriptionAnnotationId]: "description",
[AST.JSONSchemaAnnotationId]: { type: "integer" }
})
})
Expand Down

0 comments on commit fdfe472

Please sign in to comment.