fromURLSearchParams<S>Schema for decoding URLSearchParams through a bracket-notation tree.
When to use
Use to decode query parameters into a structured schema value.
Details
The decoding process has two steps:
- Parse
URLSearchParamsinto a nested tree record. - Decode the parsed value with the given schema.
You can express nested values using bracket notation.
If you want to decode values that are not strings, use
Schema.toCodecStringTree. This serializer preserves values such as
numbers when compatible with the schema.
Example (Decoding a flat structure)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String
})
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1"})Example (Decoding nested fields)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String,
b: Schema.Struct({
c: Schema.String,
d: Schema.String
})
})
)
const urlSearchParams = new URLSearchParams("a=1&b[c]=2&b[d]=3")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1","b":{"c":"2","d":"3"}})Example (Parsing non-string values)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.toCodecStringTree(
Schema.Struct({
a: Schema.Int
})
)
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":1}) // Note: the value is a numberexport interface interface fromURLSearchParams<S extends Constraint>Schema for decoding URLSearchParams through a bracket-notation tree.
When to use
Use to decode query parameters into a structured schema value.
Details
The decoding process has two steps:
- Parse
URLSearchParams into a nested tree record.
- Decode the parsed value with the given schema.
You can express nested values using bracket notation.
If you want to decode values that are not strings, use
Schema.toCodecStringTree. This serializer preserves values such as
numbers when compatible with the schema.
Example (Decoding a flat structure)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String
})
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1"})
Example (Decoding nested fields)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String,
b: Schema.Struct({
c: Schema.String,
d: Schema.String
})
})
)
const urlSearchParams = new URLSearchParams("a=1&b[c]=2&b[d]=3")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1","b":{"c":"2","d":"3"}})
Example (Parsing non-string values)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.toCodecStringTree(
Schema.Struct({
a: Schema.Int
})
)
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":1}) // Note: the value is a number
Type-level representation returned by
fromURLSearchParams
.
fromURLSearchParams<function (type parameter) S in fromURLSearchParams<S extends Constraint>S extends Constraint> extends interface decodeTo<To extends Constraint, From extends Constraint, RD = never, RE = never>Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
Type-level representation returned by
decodeTo
.
decodeTo<function (type parameter) S in fromURLSearchParams<S extends Constraint>S, URLSearchParams> {
readonly "Rebuild": interface fromURLSearchParams<S extends Constraint>Schema for decoding URLSearchParams through a bracket-notation tree.
When to use
Use to decode query parameters into a structured schema value.
Details
The decoding process has two steps:
- Parse
URLSearchParams into a nested tree record.
- Decode the parsed value with the given schema.
You can express nested values using bracket notation.
If you want to decode values that are not strings, use
Schema.toCodecStringTree. This serializer preserves values such as
numbers when compatible with the schema.
Example (Decoding a flat structure)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String
})
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1"})
Example (Decoding nested fields)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String,
b: Schema.Struct({
c: Schema.String,
d: Schema.String
})
})
)
const urlSearchParams = new URLSearchParams("a=1&b[c]=2&b[d]=3")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1","b":{"c":"2","d":"3"}})
Example (Parsing non-string values)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.toCodecStringTree(
Schema.Struct({
a: Schema.Int
})
)
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":1}) // Note: the value is a number
Type-level representation returned by
fromURLSearchParams
.
fromURLSearchParams<function (type parameter) S in fromURLSearchParams<S extends Constraint>S>
}
/**
* Schema for decoding `URLSearchParams` through a bracket-notation tree.
*
* **When to use**
*
* Use to decode query parameters into a structured schema value.
*
* **Details**
*
* The decoding process has two steps:
*
* 1. Parse `URLSearchParams` into a nested tree record.
* 2. Decode the parsed value with the given schema.
*
* You can express nested values using bracket notation.
*
* If you want to decode values that are not strings, use
* `Schema.toCodecStringTree`. This serializer preserves values such as
* numbers when compatible with the schema.
*
* **Example** (Decoding a flat structure)
*
* ```ts
* import { Schema } from "effect"
*
* const schema = Schema.fromURLSearchParams(
* Schema.Struct({
* a: Schema.String
* })
* )
*
* const urlSearchParams = new URLSearchParams("a=1&b=2")
*
* console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
* // Success({"a":"1"})
* ```
*
* **Example** (Decoding nested fields)
*
* ```ts
* import { Schema } from "effect"
*
* const schema = Schema.fromURLSearchParams(
* Schema.Struct({
* a: Schema.String,
* b: Schema.Struct({
* c: Schema.String,
* d: Schema.String
* })
* })
* )
*
* const urlSearchParams = new URLSearchParams("a=1&b[c]=2&b[d]=3")
*
* console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
* // Success({"a":"1","b":{"c":"2","d":"3"}})
* ```
*
* **Example** (Parsing non-string values)
*
* ```ts
* import { Schema } from "effect"
*
* const schema = Schema.fromURLSearchParams(
* Schema.toCodecStringTree(
* Schema.Struct({
* a: Schema.Int
* })
* )
* )
*
* const urlSearchParams = new URLSearchParams("a=1&b=2")
*
* console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
* // Success({"a":1}) // Note: the value is a number
* ```
*
* @category decoding
* @since 4.0.0
*/
export function function fromURLSearchParams<
S extends Constraint
>(schema: S): fromURLSearchParams<S>
Schema for decoding URLSearchParams through a bracket-notation tree.
When to use
Use to decode query parameters into a structured schema value.
Details
The decoding process has two steps:
- Parse
URLSearchParams into a nested tree record.
- Decode the parsed value with the given schema.
You can express nested values using bracket notation.
If you want to decode values that are not strings, use
Schema.toCodecStringTree. This serializer preserves values such as
numbers when compatible with the schema.
Example (Decoding a flat structure)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String
})
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1"})
Example (Decoding nested fields)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String,
b: Schema.Struct({
c: Schema.String,
d: Schema.String
})
})
)
const urlSearchParams = new URLSearchParams("a=1&b[c]=2&b[d]=3")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1","b":{"c":"2","d":"3"}})
Example (Parsing non-string values)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.toCodecStringTree(
Schema.Struct({
a: Schema.Int
})
)
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":1}) // Note: the value is a number
fromURLSearchParams<function (type parameter) S in fromURLSearchParams<S extends Constraint>(schema: S): fromURLSearchParams<S>S extends Constraint>(schema: S extends Constraintschema: function (type parameter) S in fromURLSearchParams<S extends Constraint>(schema: S): fromURLSearchParams<S>S): interface fromURLSearchParams<S extends Constraint>Schema for decoding URLSearchParams through a bracket-notation tree.
When to use
Use to decode query parameters into a structured schema value.
Details
The decoding process has two steps:
- Parse
URLSearchParams into a nested tree record.
- Decode the parsed value with the given schema.
You can express nested values using bracket notation.
If you want to decode values that are not strings, use
Schema.toCodecStringTree. This serializer preserves values such as
numbers when compatible with the schema.
Example (Decoding a flat structure)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String
})
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1"})
Example (Decoding nested fields)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.Struct({
a: Schema.String,
b: Schema.Struct({
c: Schema.String,
d: Schema.String
})
})
)
const urlSearchParams = new URLSearchParams("a=1&b[c]=2&b[d]=3")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":"1","b":{"c":"2","d":"3"}})
Example (Parsing non-string values)
import { Schema } from "effect"
const schema = Schema.fromURLSearchParams(
Schema.toCodecStringTree(
Schema.Struct({
a: Schema.Int
})
)
)
const urlSearchParams = new URLSearchParams("a=1&b=2")
console.log(String(Schema.decodeUnknownExit(schema)(urlSearchParams)))
// Success({"a":1}) // Note: the value is a number
Type-level representation returned by
fromURLSearchParams
.
fromURLSearchParams<function (type parameter) S in fromURLSearchParams<S extends Constraint>(schema: S): fromURLSearchParams<S>S> {
return const URLSearchParams: URLSearchParamsconst URLSearchParams: {
Rebuild: URLSearchParams;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.URLSearchParams, readonly []>) => URLSearchParams;
annotateKey: (annotations: Annotations.Key<globalThis.URLSearchParams>) => URLSearchParams;
check: (checks_0: SchemaAST.Check<globalThis.URLSearchParams>, ...checks: Array<SchemaAST.Check<globalThis.URLSearchParams>>) => URLSearchParams;
rebuild: (ast: SchemaAST.Declaration) => URLSearchParams;
make: (input: globalThis.URLSearchParams, options?: MakeOptions) => globalThis.URLSearchParams;
makeOption: (input: globalThis.URLSearchParams, options?: MakeOptions) => Option_.Option<globalThis.URLSearchParams>;
makeEffect: (input: globalThis.URLSearchParams, options?: MakeOptions) => Effect.Effect<globalThis.URLSearchParams, 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
URLSearchParams
.
Schema for JavaScript URLSearchParams objects.
Details
The default JSON serializer encodes a URLSearchParams as a query string.
URLSearchParams.pipe(function decodeTo<S, Constraint, never, never>(to: S, transformation: {
readonly decode: SchemaGetter.Getter<NoInfer<S["Encoded"]>, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, NoInfer<S["Encoded"]>, never>;
}): (from: Constraint) => decodeTo<S, Constraint, never, never> (+1 overload)
Creates a schema that transforms from a source schema to a target schema.
When to use
Use when decoding should change the schema's decoded type or encoded shape,
with an optional custom bidirectional transformation.
Details
Call it with the target schema to and then pipe the source schema from
into the returned function. The resulting schema decodes from
From["Encoded"] to To["Type"] and encodes from To["Type"] back to
From["Encoded"].
When no transformation is provided, SchemaTransformation.passthrough() is
used, so From["Type"] must already be compatible with To["Encoded"].
The resulting schema combines decoding and encoding services from both
schemas and any custom transformation.
Gotchas
In a custom transformation, decode maps From["Type"] to To["Encoded"]
and is used on the encoding path, while encode maps To["Encoded"] to
From["Type"] and is used on the decoding path.
Example (Transforming strings to numbers with a schema transformation)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(
Schema.Number,
{
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
}
)
)
const result = Schema.decodeUnknownSync(NumberFromString)("123")
// result: 123
decodeTo(schema: S extends Constraintschema, import SchemaTransformationSchemaTransformation.const fromURLSearchParams: SchemaTransformation.Transformation<
unknown,
globalThis.URLSearchParams,
never,
never
>
const fromURLSearchParams: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<globalThis.URLSearchParams, unknown, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, unknown, RD2, RE2>) => SchemaTransformation.Transformation<T2, globalThis.URLSearchParams, RD2, RE2>;
}
Decodes URLSearchParams into a nested record using bracket-path keys and
encodes object-like values back into URLSearchParams.
When to use
Use when you need a schema transformation for query strings whose keys, such
as filter[name] or items[0], should become nested data.
Details
Decode produces string leaves. Encode flattens nested objects and arrays into
bracket-path entries and returns empty URLSearchParams for non-object
inputs.
Example (Decoding URLSearchParams)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.instanceOf(URLSearchParams).pipe(
Schema.decodeTo(Schema.Unknown, SchemaTransformation.fromURLSearchParams)
)
fromURLSearchParams))
}