@rudderstack/integrations-lib
Version:
109 lines • 3.56 kB
TypeScript
/**
* Type definition for a JSON Schema.
*/
export type JSONSchemaType = {
type?: string | string[];
properties?: {
[key: string]: JSONSchemaType;
};
items?: JSONSchemaType;
required?: string[];
enum?: any[];
anyOf?: JSONSchemaType[];
};
/**
* Options for generating JSON Schemas.
*/
export type JsonSchemaGeneratorOptions = {
/** If true, marks every property in an object as required */
requireAll?: boolean;
/** If true, attempts to detect and include enums for arrays of strings */
detectEnums?: boolean;
};
/**
* Utility for generating and validating JSON Schemas from data.
*/
export declare class JsonSchemaGenerator {
private static validator;
/**
* Validates the provided data against the given JSON Schema.
*
* @param data - The data to validate.
* @param schema - The JSON Schema to use for validation.
* @throws Error if the validation fails.
*/
static validate(data: unknown, schema: JSONSchemaType): void;
/**
* Generates a JSON Schema for the given data.
*
* @param data - The data for which to generate a JSON Schema.
* @param options - Generation options.
* @returns The generated JSON Schema.
*/
static generate(data: unknown, options?: JsonSchemaGeneratorOptions): JSONSchemaType;
/**
* Determines the primitive type of a value.
*
* @param data - The data whose type is to be inferred.
* @returns A string representing the type.
*/
private static inferType;
/**
* Recursively builds a JSON Schema from the provided data.
*
* @param data - The data to interpret.
* @param options - Generation options.
* @returns A JSON Schema object.
*/
private static buildSchema;
/**
* Checks if all types in an array are one of the primitive types:
* string, number, or boolean.
*
* @param types - An array of type strings.
* @returns True if every type is primitive.
*/
private static allTypesArePrimitive;
/**
* Generates a JSON Schema for an array.
*
* @param data - The array data.
* @param options - Generation options.
* @returns The JSON Schema corresponding to the array data.
*/
private static buildArraySchema;
/**
* Generates a JSON Schema for an array containing mixed types.
*
* @param data - The array data.
* @param options - Generation options.
* @returns The JSON Schema with an "anyOf" clause.
*/
private static buildMixedArraySchema;
/**
* Generates a JSON Schema for an object.
*
* @param data - The object data.
* @param options - Generation options.
* @returns The JSON Schema for the object.
*/
private static buildObjectSchema;
/**
* Attempts to merge two JSON Schemas. Merging is only successful if both schemas have the same type
* and there are no conflicts within defined properties.
*
* @param schemaA - The first JSON Schema.
* @param schemaB - The second JSON Schema.
* @returns The merged schema if successful; otherwise, null.
*/
private static mergeTwoSchemas;
/**
* Merges an array of JSON Schemas. Schemas with the same type and non-conflicting
* definitions are merged together.
*
* @param schemas - The list of schemas to merge.
* @returns A deduplicated list of merged schemas.
*/
private static mergeSimilarSchemas;
}
//# sourceMappingURL=json-schema-generator.d.ts.map