Hyperlinkv0.8.0-beta.28

Schema

Schema.fromJsonStringfunctioneffect/Schema.ts:11013
fromJsonString<S>

Returns a schema that decodes a JSON string and then decodes the parsed value using the given schema.

Details

This is useful when working with JSON-encoded strings where the actual structure of the value is known and described by an existing schema.

The resulting schema first parses the input string as JSON, and then runs the provided schema on the parsed result.

JSON Schema generation:

When using fromJsonString with draft-2020-12 or openApi3.1, the resulting schema will be a JSON Schema with a contentSchema property that contains the JSON Schema for the given schema.

Example (Decoding JSON strings with a schema)

import { Schema } from "effect"

const schema = Schema.Struct({ a: Schema.Number })
const schemaFromJsonString = Schema.fromJsonString(schema)

Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1 }

Example (Emitting JSON Schema for a JSON string decoder)

import { Schema } from "effect"

const original = Schema.Struct({ a: Schema.String })
const schema = Schema.fromJsonString(original)

const document = Schema.toJsonSchemaDocument(schema)

console.log(JSON.stringify(document, null, 2))
// {
//   "source": "draft-2020-12",
//   "schema": {
//     "type": "string",
//     "contentMediaType": "application/json",
//     "contentSchema": {
//       "type": "object",
//       "properties": {
//         "a": {
//           "type": "string"
//         }
//       },
//       "required": [
//         "a"
//       ],
//       "additionalProperties": false
//     }
//   },
//   "definitions": {}
// }
constructors
Source effect/Schema.ts:1101380 lines
export interface fromJsonString<S extends Constraint> extends decodeTo<S, String> {
  readonly "Rebuild": fromJsonString<S>
}

/**
 * Returns a schema that decodes a JSON string and then decodes the parsed value
 * using the given schema.
 *
 * **Details**
 *
 * This is useful when working with JSON-encoded strings where the actual
 * structure of the value is known and described by an existing schema.
 *
 * The resulting schema first parses the input string as JSON, and then runs the
 * provided schema on the parsed result.
 *
 * JSON Schema generation:
 *
 * When using `fromJsonString` with `draft-2020-12` or `openApi3.1`, the
 * resulting schema will be a JSON Schema with a `contentSchema` property that
 * contains the JSON Schema for the given schema.
 *
 * **Example** (Decoding JSON strings with a schema)
 *
 * ```ts
 * import { Schema } from "effect"
 *
 * const schema = Schema.Struct({ a: Schema.Number })
 * const schemaFromJsonString = Schema.fromJsonString(schema)
 *
 * Schema.decodeUnknownSync(schemaFromJsonString)(`{"a":1,"b":2}`)
 * // => { a: 1 }
 * ```
 *
 * **Example** (Emitting JSON Schema for a JSON string decoder)
 *
 * ```ts
 * import { Schema } from "effect"
 *
 * const original = Schema.Struct({ a: Schema.String })
 * const schema = Schema.fromJsonString(original)
 *
 * const document = Schema.toJsonSchemaDocument(schema)
 *
 * console.log(JSON.stringify(document, null, 2))
 * // {
 * //   "source": "draft-2020-12",
 * //   "schema": {
 * //     "type": "string",
 * //     "contentMediaType": "application/json",
 * //     "contentSchema": {
 * //       "type": "object",
 * //       "properties": {
 * //         "a": {
 * //           "type": "string"
 * //         }
 * //       },
 * //       "required": [
 * //         "a"
 * //       ],
 * //       "additionalProperties": false
 * //     }
 * //   },
 * //   "definitions": {}
 * // }
 * ```
 *
 * @category constructors
 * @since 4.0.0
 */
export function fromJsonString<S extends Constraint>(schema: S): fromJsonString<S> {
  const identifier = SchemaAST.resolveIdentifier(schema.ast)
  return String.annotate({
    // Give the transport wrapper its own name so the decoded payload keeps its identifier.
    identifier: identifier === undefined ? undefined : `${identifier}JsonString`,
    expected: "a string that will be decoded as JSON",
    contentMediaType: "application/json",
    contentSchema: SchemaAST.toEncoded(schema.ast)
  }).pipe(decodeTo(schema, SchemaTransformation.fromJsonString))
}
Referenced by 2 symbols