UnknownFromJsonStringType-level representation of UnknownFromJsonString.
export interface UnknownFromJsonString extends interface fromJsonString<S extends Constraint>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": {}
// }
Type-level representation returned by
fromJsonString
.
fromJsonString<Unknown> {
readonly "Rebuild": UnknownFromJsonString
}
/**
* Schema that decodes a JSON-encoded string into an `unknown` value.
*
* **Details**
*
* Decoding:
* - A `string` is decoded as an `unknown` value.
* - If the string is not valid JSON, decoding fails.
*
* Encoding:
* - Any value is encoded as a JSON string using `JSON.stringify`.
* - If the value is not a valid JSON value, encoding fails.
*
* **Example** (Decoding unknown JSON strings)
*
* ```ts
* import { Schema } from "effect"
*
* Schema.decodeUnknownSync(Schema.UnknownFromJsonString)(`{"a":1,"b":2}`)
* // => { a: 1, b: 2 }
* ```
*
* @category schemas
* @since 4.0.0
*/
export const const UnknownFromJsonString: UnknownFromJsonStringconst UnknownFromJsonString: {
Rebuild: UnknownFromJsonString;
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
ast: Ast;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => UnknownFromJsonString;
annotateKey: (annotations: Annotations.Key<unknown>) => UnknownFromJsonString;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => UnknownFromJsonString;
rebuild: (ast: SchemaAST.Unknown) => UnknownFromJsonString;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
UnknownFromJsonString
.
Schema that decodes a JSON-encoded string into an unknown value.
Details
Decoding:
- A
string is decoded as an unknown value.
- If the string is not valid JSON, decoding fails.
Encoding:
- Any value is encoded as a JSON string using
JSON.stringify.
- If the value is not a valid JSON value, encoding fails.
Example (Decoding unknown JSON strings)
import { Schema } from "effect"
Schema.decodeUnknownSync(Schema.UnknownFromJsonString)(`{"a":1,"b":2}`)
// => { a: 1, b: 2 }
UnknownFromJsonString: UnknownFromJsonString = function fromJsonString<
S extends Constraint
>(schema: S): 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": {}
// }
fromJsonString(const Unknown: Unknownconst Unknown: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<unknown, readonly []>) => Unknown;
annotateKey: (annotations: Annotations.Key<unknown>) => Unknown;
check: (checks_0: SchemaAST.Check<unknown>, ...checks: Array<SchemaAST.Check<unknown>>) => Unknown;
rebuild: (ast: SchemaAST.Unknown) => Unknown;
make: (input: unknown, options?: MakeOptions) => unknown;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<unknown>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<unknown, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Type-level representation of
Unknown
.
Schema for the unknown type. Accepts any value without validation.
When to use
Use as a top schema when you need to accept any input while preserving
TypeScript's unknown safety at use sites.
Unknown)