Reflection
The Schema.reflection module provides low-level introspection and inspection utilities for working with Schemify validators.
Usage
import Schema from "@bytelab.studio/schemify";
const mySchema = Schema.any();
// later in code...
const options: Schema.AnyOptions = Schema.reflection.getOptions<Schema.AnyOptions>(mySchema);Validator Identity & Metadata
isValidator
declare function isValidator(validator: UnknownValidatorFunction, module: string, name: string): boolean;
declare function isValidator(validator: UnknownValidatorFunction, id: string): boolean;Check whether a validator matches a specific module + name, by either passing them seperatly or as id string with the format "<module>.<name>".
getValidatorModule
declare function getValidatorModule(validator: UnknownValidatorFunction): string;Returns the module the validator belongs to.
import * as Schema from "@bytelab.studio/schemify";
Schema.reflection.getValidatorModule(Schema.any()), // "primitive"
Schema.reflection.getValidatorModule(Schema.list(Schema.string())) // "complex"getValidatorName
declare function getValidatorName(validator: UnknownValidatorFunction): string;Returns the name of the validator.
import * as Schema from "@bytelab.studio/schemify";
Schema.reflection.getValidatorModule(Schema.any()), // "any"
Schema.reflection.getValidatorModule(Schema.list(Schema.string())) // "list"getValidatorId
declare function getValidatorId(validator: UnknownValidatorFunction): string;Returns the full validator ID in "<module>.<name>" format
import * as Schema from "@bytelab.studio/schemify";
Schema.reflection.getValidatorModule(Schema.any()), // "core.any"
Schema.reflection.getValidatorModule(Schema.list(Schema.string())) // "complex.list"Validator configuration
getOptions
declare function getOptions<Options extends RawOptions>(validator: ValidatorFunction<Options, unknown>): Options;Returns the configuration of the passed validator
import * as Schema from "@bytelab.studio/schemify";
const schema = Schema.string({
nullable: true
});
// Later...
const options: Schema.StringOptions = Schema.reflection.getOptions<Schema.StringOptions>(schema);AST Traversel
Schemify schemas form an abstract syntax tree (AST). You can traverse it using the functions and types below.
getChildren
declare function getChildren(validator: UnknownValidatorFunction): Generator<ASTChild>;Returns a generator that yields child nodes of a given validator, if it supports children. Each yielded item conforms to the ASTChild type, which may represent either a validator node or a primitive value.
type ASTChild = ASTValidatorChild | ASTPrimitiveChild;Represents a single child node within the AST. It can be either:
- A validator child, which wraps another validator.
- A primitive child, which wraps raw/primitive value (e.g the value from the literal validator).
enum ASTChildKind {
PROPERTY,
POSITIONAL,
INFINITY
}Describes the kind of the child's location within its parent:
PROPERTY: A named key in a object schema.POSITIONAL: An indexed position a tuple like structure.INFINITY: Represents variadic or spread-like elements e.g for infinite arrays.
interface ASTValidatorChild {
type: ASTChildType.VALIDATOR;
key: string | number;
value: UnknownValidatorFunction;
kind: ASTChildKind;
}Represents a child node that contains a nested validator.
type: AlwaysASTChildType.VALIDATOR, for distinguish.key: The key or index identifying this childvalue: The nested validator instance.kind: The kind of the child (seeASTChildKind)
interface ASTPrimitiveChild {
type: ASTChildType.PRIMITIVE;
key: string | number;
value: unknown;
kind: ASTChildKind;
}Represents a child node that contains a primitive value.
type: AwalysASTChildType.PRIMITIVEkey: The key or index identifying this childvalue: The raw primitive value.kind: The kind of the child (seeASTChildKind)