@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
71 lines (70 loc) • 2.92 kB
TypeScript
import { AnyJson, JsonMap } from '@salesforce/ts-types';
import { Logger } from '../logger/logger';
/**
* Loads a JSON schema and performs validations against JSON objects.
*/
export declare class SchemaValidator {
private schemaPath;
private readonly schemasDir;
private readonly logger;
private schema?;
/**
* Creates a new `SchemaValidator` instance given a logger and path to a schema file.
*
* @param logger An {@link Logger} instance on which to base this class's logger.
* @param schemaPath The path to the schema file to load and use for validation.
*/
constructor(logger: Logger, schemaPath: string);
/**
* Loads a JSON schema from the `schemaPath` parameter provided at instantiation.
*/
load(): Promise<JsonMap>;
/**
* Loads a JSON schema from the `schemaPath` parameter provided at instantiation.
*/
loadSync(): JsonMap;
/**
* Performs validation of JSON data against the schema located at the `schemaPath` value provided
* at instantiation.
*
* **Throws** *{@link SfError}{ name: 'ValidationSchemaFieldError' }* If there are known validations errors.
* **Throws** *{@link SfError}{ name: 'ValidationSchemaUnknownError' }* If there are unknown validations errors.
*
* @param json A JSON value to validate against this instance's target schema.
* @returns The validated JSON data.
*/
validate(json: AnyJson): Promise<AnyJson>;
/**
* Performs validation of JSON data against the schema located at the `schemaPath` value provided
* at instantiation.
*
* **Throws** *{@link SfError}{ name: 'ValidationSchemaFieldError' }* If there are known validations errors.
* **Throws** *{@link SfError}{ name: 'ValidationSchemaUnknownError' }* If there are unknown validations errors.
*
* @param json A JSON value to validate against this instance's target schema.
* @returns The validated JSON data.
*/
validateSync<T extends AnyJson>(json: T): T;
/**
* Loads local, external schemas from URIs in the same directory as the local schema file.
* Does not support loading from remote URIs.
* Returns a map of external schema local URIs to loaded schema JSON objects.
*
* @param schema The main schema to look up references ($ref) in.
* @returns An array of found referenced schemas.
*/
private loadExternalSchemas;
/**
* Load another schema relative to the primary schema when referenced. Only supports local schema URIs.
*
* @param uri The first segment of the $ref schema.
*/
private loadExternalSchema;
/**
* Get a string representation of the schema validation errors.
* Adds additional (human friendly) information to certain errors.
*
* @param errors An array of AJV (DefinedError) objects.
*/
private getErrorsText;
}