SchemaValidator
JSON schema validation for configuration files and runtime data, backed by z-schema.
Usage
import { SchemaValidator } from "@io-oidis-localhost/Io/Oidis/Localhost/Utils/SchemaValidator.js";
import { ISchemaError } from "@io-oidis-localhost/Io/Oidis/Localhost/Utils/SchemaValidator.js";
const validator : SchemaValidator = new SchemaValidator();
validator.Schema("/path/to/schema.json");
const errors : ISchemaError[] = validator.Validate(configObject);
if (errors.length > 0) {
throw new Error(validator.ErrorsToString(errors, true));
}
API Reference
Schema
Sets the active schema. Accepts either a file path string (loaded from disk) or a plain object. Optionally registers custom format validators.
public Schema($value? : any, $formats? : any) : any
$value— path to a JSON schema file, or a schema object. If omitted, returns the current schema.$formats— map of format name to validator function, registered globally in z-schema.
Validate
Validates a value against the current schema. Returns an array of ISchemaError — empty when validation passes.
public Validate($value : any, $parentKey : string = "#") : ISchemaError[]
$value— the object to validate.$parentKey— prefix used in error field paths (default"#").
ErrorsToString
Formats an array of schema errors into a human-readable string for logging or exception messages.
public ErrorsToString($errors : ISchemaError[], $includeValue : boolean = false) : string
$includeValue— whentrue, each error line includes the offending value.
ErrorHandler
Overrides the default error handler (which rethrows). Use to suppress or redirect schema parse errors.
public ErrorHandler($handler : ($error : Error | ErrorEvent) => void) : void
ISchemaError
| Field | Type | Description |
|---|---|---|
message | string | z-schema error message |
field | string | Dot-path to the failing field (e.g. #.domain.location) |
value | string | Serialized value at the failing field |
SchemaValidator is instantiated directly with new SchemaValidator(). It does not use the singleton pattern — each instance holds its own schema and validator state.
See also: Utilities Overview | ObjectValidator | JSONP System