URLFromStringType-level representation of URLFromString.
export interface URLFromString 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<URL, String> {
readonly "Rebuild": URLFromString
}
/**
* Schema that decodes a `string` into a `URL`.
*
* **Details**
*
* Decoding:
* - A **valid** URL `string` is decoded as a `URL`
*
* Encoding:
* - A `URL` is encoded as a `string`
*
* @category URL
* @since 4.0.0
*/
export const const URLFromString: URLFromStringconst URLFromString: {
Rebuild: URLFromString;
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<globalThis.URL, readonly []>) => URLFromString;
annotateKey: (annotations: Annotations.Key<globalThis.URL>) => URLFromString;
check: (checks_0: SchemaAST.Check<globalThis.URL>, ...checks: Array<SchemaAST.Check<globalThis.URL>>) => URLFromString;
rebuild: (ast: SchemaAST.Declaration) => URLFromString;
make: (input: globalThis.URL, options?: MakeOptions) => globalThis.URL;
makeOption: (input: globalThis.URL, options?: MakeOptions) => Option_.Option<globalThis.URL>;
makeEffect: (input: globalThis.URL, options?: MakeOptions) => Effect.Effect<globalThis.URL, 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
URLFromString
.
Schema that decodes a string into a URL.
Details
Decoding:
- A valid URL
string is decoded as a URL
Encoding:
- A
URL is encoded as a string
URLFromString: URLFromString = const URLString: Stringconst URLString: {
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<string, readonly []>) => String;
annotateKey: (annotations: Annotations.Key<string>) => String;
check: (checks_0: SchemaAST.Check<string>, ...checks: Array<SchemaAST.Check<string>>) => String;
rebuild: (ast: SchemaAST.String) => String;
make: (input: string, options?: MakeOptions) => string;
makeOption: (input: string, options?: MakeOptions) => Option_.Option<string>;
makeEffect: (input: string, options?: MakeOptions) => Effect.Effect<string, 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; <…;
}
URLString.pipe(function decodeTo<URL, Constraint, never, never>(to: URL, transformation: {
readonly decode: SchemaGetter.Getter<NoInfer<globalThis.URL>, unknown, never>;
readonly encode: SchemaGetter.Getter<unknown, NoInfer<globalThis.URL>, never>;
}): (from: Constraint) => decodeTo<URL, 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(const URL: URLconst URL: {
Rebuild: URL;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Annotations.Bottom<globalThis.URL, readonly []>) => URL;
annotateKey: (annotations: Annotations.Key<globalThis.URL>) => URL;
check: (checks_0: SchemaAST.Check<globalThis.URL>, ...checks: Array<SchemaAST.Check<globalThis.URL>>) => URL;
rebuild: (ast: SchemaAST.Declaration) => URL;
make: (input: globalThis.URL, options?: MakeOptions) => globalThis.URL;
makeOption: (input: globalThis.URL, options?: MakeOptions) => Option_.Option<globalThis.URL>;
makeEffect: (input: globalThis.URL, options?: MakeOptions) => Effect.Effect<globalThis.URL, 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
URL
.
Schema for JavaScript URL objects.
Details
Default JSON serializer:
- encodes
URL as a string
URL, import SchemaTransformationSchemaTransformation.const urlFromString: Transformation<
URL,
string
>
const urlFromString: {
_tag: 'Transformation';
decode: SchemaGetter.Getter<T, E, RD>;
encode: SchemaGetter.Getter<E, T, RE>;
flip: () => SchemaTransformation.Transformation<string, globalThis.URL, never, never>;
compose: (other: SchemaTransformation.Transformation<T2, globalThis.URL, RD2, RE2>) => SchemaTransformation.Transformation<T2, string, RD2, RE2>;
}
Decodes a string into a URL and encodes a URL back to its href
string.
When to use
Use when you need a schema transformation to parse URL strings from user
input or API responses.
Details
Decoding checks URL.canParse(s) and fails with InvalidValue if the string
is not a valid URL. Encoding returns url.href.
Example (Converting a string to a URL)
import { Schema, SchemaTransformation } from "effect"
const schema = Schema.String.pipe(
Schema.decodeTo(Schema.URL, SchemaTransformation.urlFromString)
)
urlFromString))