Pretty
On this page
Generating Pretty Printers
The Pretty.make
function is designed for creating pretty printers that output a formatted string representation of values according to a given schema.
Example
ts
import {Pretty ,Schema } from "@effect/schema"constPerson =Schema .Struct ({name :Schema .String ,age :Schema .Number })constPersonPretty =Pretty .make (Person )// returns a string representation of the objectconsole .log (PersonPretty ({name : "Alice",age : 30 }))/*Output:'{ "name": "Alice", "age": 30 }'*/
ts
import {Pretty ,Schema } from "@effect/schema"constPerson =Schema .Struct ({name :Schema .String ,age :Schema .Number })constPersonPretty =Pretty .make (Person )// returns a string representation of the objectconsole .log (PersonPretty ({name : "Alice",age : 30 }))/*Output:'{ "name": "Alice", "age": 30 }'*/
Customizing Pretty Printer Generation
You can tailor the output of the pretty printer by using the pretty
annotation within your schema definitions.
Example
ts
import {Pretty ,Schema } from "@effect/schema"constschema =Schema .Number .annotations ({pretty : (/**typeParameters**/) => (value ) => `my format: ${value }`})console .log (Pretty .make (schema )(1)) // my format: 1
ts
import {Pretty ,Schema } from "@effect/schema"constschema =Schema .Number .annotations ({pretty : (/**typeParameters**/) => (value ) => `my format: ${value }`})console .log (Pretty .make (schema )(1)) // my format: 1
In this setup, the pretty
annotation takes any type parameters provided (typeParameters
) and formats the value into a string.