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
Example (Extracting Only Type-Specific Properties)
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.
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.