Schema投影
有时,您可能希望基于现有模式创建新模式,特别关注其Type或Encoded方面。Schema模块提供了几个函数来实现这一点。
Schema.typeSchema函数用于提取模式的Type部分,生成一个新模式,该模式仅保留原始模式中的类型特定属性。这排除了应用于原始模式的任何初始编码或转换逻辑。
函数签名
declare const typeSchema: <A, I, R>(schema: Schema<A, I, R>) => Schema<A>示例(仅提取类型特定属性)
import { import Schema
Schema } from "effect"
const const Original: Schema.Struct<{ quantity: Schema.filter<typeof Schema.NumberFromString>;}>
Original = import Schema
Schema.function Struct<{ quantity: Schema.filter<typeof Schema.NumberFromString>;}>(fields: { quantity: Schema.filter<typeof Schema.NumberFromString>;}): Schema.Struct<{ quantity: Schema.filter<typeof Schema.NumberFromString>;}> (+1 overload)
Struct({ quantity: Schema.filter<typeof Schema.NumberFromString>
quantity: import Schema
Schema.class NumberFromString
This schema transforms a string into a number by parsing the string using the parse function of the effect/Number module.
It returns an error if the value can't be converted (for example when non-numeric characters are provided).
The following special string values are supported: "NaN", "Infinity", "-Infinity".
NumberFromString.Pipeable.pipe<typeof Schema.NumberFromString, Schema.filter<typeof Schema.NumberFromString>>(this: typeof Schema.NumberFromString, ab: (_: typeof Schema.NumberFromString) => Schema.filter<typeof Schema.NumberFromString>): Schema.filter<typeof Schema.NumberFromString> (+21 overloads)
pipe(import Schema
Schema.const greaterThanOrEqualTo: <typeof Schema.NumberFromString>(minimum: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <A>(self: typeof Schema.NumberFromString & Schema.Schema<A, string, never>) => Schema.filter<typeof Schema.NumberFromString>
This filter checks whether the provided number is greater than or equal to the specified minimum.
greaterThanOrEqualTo(2))})
// 这创建了一个模式,其中'quantity'被定义为数字// 必须大于或等于2。const const TypeSchema: Schema.SchemaClass<{ readonly quantity: number;}, { readonly quantity: number;}, never>
TypeSchema = import Schema
Schema.const typeSchema: <{ readonly quantity: number;}, { readonly quantity: string;}, never>(schema: Schema.Schema<{ readonly quantity: number;}, { readonly quantity: string;}, never>) => Schema.SchemaClass<{ readonly quantity: number;}, { readonly quantity: number;}, never>
The typeSchema function allows you to extract the Type portion of a
schema, creating a new schema that conforms to the properties defined in the
original schema without considering the initial encoding or transformation
processes.
typeSchema(const Original: Schema.Struct<{ quantity: Schema.filter<typeof Schema.NumberFromString>;}>
Original)
// TypeSchema等同于:const const TypeSchema2: Schema.Struct<{ quantity: Schema.filter<typeof Schema.Number>;}>
TypeSchema2 = import Schema
Schema.function Struct<{ quantity: Schema.filter<typeof Schema.Number>;}>(fields: { quantity: Schema.filter<typeof Schema.Number>;}): Schema.Struct<{ quantity: Schema.filter<typeof Schema.Number>;}> (+1 overload)
Struct({ quantity: Schema.filter<typeof Schema.Number>
quantity: import Schema
Schema.class Numberexport Number
Number.Pipeable.pipe<typeof Schema.Number, Schema.filter<typeof Schema.Number>>(this: typeof Schema.Number, ab: (_: typeof Schema.Number) => Schema.filter<typeof Schema.Number>): Schema.filter<typeof Schema.Number> (+21 overloads)
pipe(import Schema
Schema.const greaterThanOrEqualTo: <typeof Schema.Number>(minimum: number, annotations?: Schema.Annotations.Filter<number, number> | undefined) => <A>(self: typeof Schema.Number & Schema.Schema<A, number, never>) => Schema.filter<typeof Schema.Number>
This filter checks whether the provided number is greater than or equal to the specified minimum.
greaterThanOrEqualTo(2))})Schema.encodedSchema函数使您能够提取模式的Encoded部分,创建一个与原始属性匹配但省略应用于模式的任何细化或转换的新模式。
函数签名
declare const encodedSchema: <A, I, R>( schema: Schema<A, I, R>) => Schema<I>示例(仅提取编码属性)
import { import Schema
Schema } from "effect"
const const Original: Schema.Struct<{ quantity: Schema.filter<typeof Schema.String>;}>
Original = import Schema
Schema.function Struct<{ quantity: Schema.filter<typeof Schema.String>;}>(fields: { quantity: Schema.filter<typeof Schema.String>;}): Schema.Struct<{ quantity: Schema.filter<typeof Schema.String>;}> (+1 overload)
Struct({ quantity: Schema.filter<typeof Schema.String>
quantity: import Schema
Schema.class Stringexport String
String.Pipeable.pipe<typeof Schema.String, Schema.filter<typeof Schema.String>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.filter<typeof Schema.String>): Schema.filter<typeof Schema.String> (+21 overloads)
pipe(import Schema
Schema.const minLength: <typeof Schema.String>(minLength: number, annotations?: Schema.Annotations.Filter<string, string> | undefined) => <A>(self: typeof Schema.String & Schema.Schema<A, string, never>) => Schema.filter<typeof Schema.String>
minLength(3))})
// 这创建了一个模式,其中'quantity'只是一个字符串,// 忽略minLength细化。const const Encoded: Schema.SchemaClass<{ readonly quantity: string;}, { readonly quantity: string;}, never>
Encoded = import Schema
Schema.const encodedSchema: <{ readonly quantity: string;}, { readonly quantity: string;}, never>(schema: Schema.Schema<{ readonly quantity: string;}, { readonly quantity: string;}, never>) => Schema.SchemaClass<{ readonly quantity: string;}, { readonly quantity: string;}, never>
The encodedSchema function allows you to extract the Encoded portion of a
schema, creating a new schema that conforms to the properties defined in the
original schema without retaining any refinements or transformations that
were applied previously.
encodedSchema(const Original: Schema.Struct<{ quantity: Schema.filter<typeof Schema.String>;}>
Original)
// Encoded等同于:const const Encoded2: Schema.Struct<{ quantity: typeof Schema.String;}>
Encoded2 = import Schema
Schema.function Struct<{ quantity: typeof Schema.String;}>(fields: { quantity: typeof Schema.String;}): Schema.Struct<{ quantity: typeof Schema.String;}> (+1 overload)
Struct({ quantity: typeof Schema.String
quantity: import Schema
Schema.class Stringexport String
String})Schema.encodedBoundSchema函数类似于Schema.encodedSchema,但保留原始模式中第一个转换点之前的细化。
函数签名
declare const encodedBoundSchema: <A, I, R>( schema: Schema<A, I, R>) => Schema<I>在此上下文中,术语”bound”指的是在提取模式的编码形式时保留细化的边界。它本质上标记了在应用任何转换之前维护初始验证和结构的限制。
示例(仅保留初始细化)
import { import Schema
Schema } from "effect"
const const Original: Schema.Struct<{ foo: Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>;}>
Original = import Schema
Schema.function Struct<{ foo: Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>;}>(fields: { foo: Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>;}): Schema.Struct<{ foo: Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>;}> (+1 overload)
Struct({ foo: Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>
foo: import Schema
Schema.class Stringexport String
String.Pipeable.pipe<typeof Schema.String, Schema.filter<typeof Schema.String>, Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.filter<typeof Schema.String>, bc: (_: Schema.filter<typeof Schema.String>) => Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>): Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim> (+21 overloads)
pipe( import Schema
Schema.const minLength: <typeof Schema.String>(minLength: number, annotations?: Schema.Annotations.Filter<string, string> | undefined) => <A>(self: typeof Schema.String & Schema.Schema<A, string, never>) => Schema.filter<typeof Schema.String>
minLength(3), import Schema
Schema.const compose: <typeof Schema.Trim, Schema.filter<typeof Schema.String>, string>(to: typeof Schema.Trim & Schema.Schema<string, string, never>) => (from: Schema.filter<typeof Schema.String>) => Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim> (+7 overloads)
compose(import Schema
Schema.class Trim
This schema allows removing whitespaces from the beginning and end of a string.
Trim) )})
// EncodedBoundSchema模式保留minLength(3)细化,// 确保强制执行字符串长度条件// 但省略Schema.Trim转换。const const EncodedBoundSchema: Schema.SchemaClass<{ readonly foo: string;}, { readonly foo: string;}, never>
EncodedBoundSchema = import Schema
Schema.const encodedBoundSchema: <{ readonly foo: string;}, { readonly foo: string;}, never>(schema: Schema.Schema<{ readonly foo: string;}, { readonly foo: string;}, never>) => Schema.SchemaClass<{ readonly foo: string;}, { readonly foo: string;}, never>
The encodedBoundSchema function is similar to encodedSchema but preserves
the refinements up to the first transformation point in the original schema.
encodedBoundSchema(const Original: Schema.Struct<{ foo: Schema.transform<Schema.filter<typeof Schema.String>, typeof Schema.Trim>;}>
Original)
// EncodedBoundSchema等同于:const const EncodedBoundSchema2: Schema.Struct<{ foo: Schema.filter<typeof Schema.String>;}>
EncodedBoundSchema2 = import Schema
Schema.function Struct<{ foo: Schema.filter<typeof Schema.String>;}>(fields: { foo: Schema.filter<typeof Schema.String>;}): Schema.Struct<{ foo: Schema.filter<typeof Schema.String>;}> (+1 overload)
Struct({ foo: Schema.filter<typeof Schema.String>
foo: import Schema
Schema.class Stringexport String
String.Pipeable.pipe<typeof Schema.String, Schema.filter<typeof Schema.String>>(this: typeof Schema.String, ab: (_: typeof Schema.String) => Schema.filter<typeof Schema.String>): Schema.filter<typeof Schema.String> (+21 overloads)
pipe(import Schema
Schema.const minLength: <typeof Schema.String>(minLength: number, annotations?: Schema.Annotations.Filter<string, string> | undefined) => <A>(self: typeof Schema.String & Schema.Schema<A, string, never>) => Schema.filter<typeof Schema.String>
minLength(3))})