Sometimes, you may want to create a new schema based on an existing one, focusing specifically on either its Type or Encoded aspect. The Schema module provides several functions to make this possible.
typeSchema
The Schema.typeSchema function is used to extract the Type portion of a schema, resulting in a new schema that retains only the type-specific properties from the original schema. This excludes any initial encoding or transformation logic applied to the original schema.
Function Signature
declareconsttypeSchema: <A, I, R>(schema:Schema<A, I, R>) =>Schema<A>
Example (Extracting Only Type-Specific Properties)
This filter checks whether the provided number is greater than or equal to the specified minimum.
@since ― 3.10.0
greaterThanOrEqualTo(2))
5
})
6
7
// This creates a schema where 'quantity' is defined as a number
8
// that must be greater than or equal to 2.
9
const
constTypeSchema:Schema.SchemaClass<{
readonlyquantity:number;
}, {
readonlyquantity:number;
}, never>
TypeSchema=
import Schema
Schema.
consttypeSchema: <{
readonlyquantity:number;
}, {
readonlyquantity:string;
}, never>(schema:Schema.Schema<{
readonlyquantity:number;
}, {
readonlyquantity:string;
}, never>) =>Schema.SchemaClass<{
readonlyquantity:number;
}, {
readonlyquantity: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.
This filter checks whether the provided number is greater than or equal to the specified minimum.
@since ― 3.10.0
greaterThanOrEqualTo(2))
14
})
encodedSchema
The Schema.encodedSchema function enables you to extract the Encoded portion of a schema, creating a new schema that matches the original properties but omits any refinements or transformations applied to the schema.
// This creates a schema where 'quantity' is just a string,
8
// disregarding the minLength refinement.
9
const
constEncoded:Schema.SchemaClass<{
readonlyquantity:string;
}, {
readonlyquantity:string;
}, never>
Encoded=
import Schema
Schema.
constencodedSchema: <{
readonlyquantity:string;
}, {
readonlyquantity:string;
}, never>(schema:Schema.Schema<{
readonlyquantity:string;
}, {
readonlyquantity:string;
}, never>) =>Schema.SchemaClass<{
readonlyquantity:string;
}, {
readonlyquantity: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.
@since ― 3.10.0
encodedSchema(
constOriginal:Schema.Struct<{
quantity:Schema.filter<typeof Schema.String>;
}>
Original)
10
11
// Encoded is equivalent to:
12
const
constEncoded2:Schema.Struct<{
quantity:typeof Schema.String;
}>
Encoded2=
import Schema
Schema.
functionStruct<{
quantity:typeof Schema.String;
}>(fields: {
quantity:typeof Schema.String;
}):Schema.Struct<{
quantity:typeof Schema.String;
}> (+1overload)
@since ― 3.10.0
Struct({
13
quantity: typeof Schema.String
quantity:
import Schema
Schema.
classString
exportString
@since ― 3.10.0
String
14
})
encodedBoundSchema
The Schema.encodedBoundSchema function is similar to Schema.encodedSchema but preserves the refinements up to the first transformation point in the
original schema.
Function Signature
declareconstencodedBoundSchema: <A, I, R>(
schema:Schema<A, I, R>
) =>Schema<I>
The term “bound” in this context refers to the boundary up to which refinements are preserved when extracting the encoded form of a schema. It essentially marks the limit to which initial validations and structure are maintained before any transformations are applied.