Hyperlinkv0.8.0-beta.28

SchemaTransformation

SchemaTransformation.fromJsonStringconsteffect/SchemaTransformation.ts:1618
Transformation<unknown, string, never, never>

Decodes a JSON string with JSON.parse and encodes a value with JSON.stringify.

When to use

Use when you need a schema transformation to decode JSON stored or transmitted as a string, usually before composing with another schema that validates the parsed structure.

Details

Decode fails with InvalidValue for invalid JSON, and encode can fail with InvalidValue when JSON.stringify cannot serialize the value.

Example (Parsing JSON)

import { Schema, SchemaTransformation } from "effect"

const schema = Schema.String.pipe(
  Schema.decodeTo(Schema.Unknown, SchemaTransformation.fromJsonString)
)
export const fromJsonString = new Transformation<unknown, string>(
  SchemaGetter.parseJson(),
  SchemaGetter.stringifyJson()
)
Referenced by 1 symbols