Function attachPropertySignature

      • Attaches a property signature with the specified key and value to the schema. This API is useful when you want to add a property to your schema which doesn't describe the shape of the input, but rather maps to another schema, for example when you want to add a discriminant to a simple union.

        Type Parameters

        Parameters

        • key: K

          The name of the property to add to the schema.

        • value: V

          The value of the property to add to the schema.

        Returns (<I, A>(schema) => Schema<I, Simplify<A & {
            readonly [k in PropertyKey]: V
        }>>)

          • <I, A>(schema): Schema<I, Simplify<A & {
                readonly [k in PropertyKey]: V
            }>>
          • Type Parameters

            • I

            • A extends object

            Parameters

            Returns Schema<I, Simplify<A & {
                readonly [k in PropertyKey]: V
            }>>

        Example

        import * as S from "@effect/schema/Schema"
        import { pipe } from "effect/Function"

        const Circle = S.struct({ radius: S.number })
        const Square = S.struct({ sideLength: S.number })
        const Shape = S.union(
        Circle.pipe(S.attachPropertySignature("kind", "circle")),
        Square.pipe(S.attachPropertySignature("kind", "square"))
        )

        assert.deepStrictEqual(S.decodeSync(Shape)({ radius: 10 }), {
        kind: "circle",
        radius: 10
        })

        Since

        1.0.0

      • Attaches a property signature with the specified key and value to the schema. This API is useful when you want to add a property to your schema which doesn't describe the shape of the input, but rather maps to another schema, for example when you want to add a discriminant to a simple union.

        Type Parameters

        Parameters

        • schema: Schema<I, A>
        • key: K

          The name of the property to add to the schema.

        • value: V

          The value of the property to add to the schema.

        Returns Schema<I, Simplify<A & {
            readonly [k in PropertyKey]: V
        }>>

        Example

        import * as S from "@effect/schema/Schema"
        import { pipe } from "effect/Function"

        const Circle = S.struct({ radius: S.number })
        const Square = S.struct({ sideLength: S.number })
        const Shape = S.union(
        Circle.pipe(S.attachPropertySignature("kind", "circle")),
        Square.pipe(S.attachPropertySignature("kind", "square"))
        )

        assert.deepStrictEqual(S.decodeSync(Shape)({ radius: 10 }), {
        kind: "circle",
        radius: 10
        })

        Since

        1.0.0