@utilize/json-schema
Version:
Utilities for working with JSON Schema.
73 lines (67 loc) • 2.48 kB
text/typescript
import { $Refs } from '@apidevtools/json-schema-ref-parser';
import { JSONSchema7, JSONSchema7TypeName } from 'json-schema';
interface JSONSchema extends JSONSchema7 {
discriminator?: string;
}
type JSONSchemaTypeName = JSONSchema7TypeName;
interface SchemaKeysType<Schema extends JSONSchema> {
const?: any;
enum?: any[];
$defs?: Record<string, Schema>;
definitions?: Record<string, Schema>;
properties?: Record<string, Schema>;
additionalProperties?: boolean | Schema;
patternProperties?: Record<string, Schema>;
propertyNames?: Schema;
items?: Schema | Schema[];
additionalItems?: boolean | Schema;
contains?: Schema;
dependencies?: Record<string, Array<string> | Schema>;
allOf?: Schema[];
anyOf?: Schema[];
oneOf?: Schema[];
not?: Schema;
}
type Path = (string | number)[];
declare const SchemaKeys: Record<keyof SchemaKeysType<JSONSchema>, string>;
declare const Meta: unique symbol;
type Meta = {
fileName: string;
filePath: string;
path: Path;
parent: ParsedJSONSchema | null;
reference?: ParsedJSONSchemaObject;
isCircular?: boolean;
resolvedName?: string;
};
interface ParsedJSONSchemaObject extends Omit<JSONSchema, keyof SchemaKeysType<JSONSchema>>, SchemaKeysType<ParsedJSONSchemaObject> {
[Meta]: Meta;
}
type ParsedJSONSchema = ParsedJSONSchemaObject | boolean | null;
interface LinkNodeOptions {
schema: JSONSchema;
parent: JSONSchema | null;
path: Path;
filePath: string;
$refs: $Refs;
stack: Set<JSONSchema>;
fileName: string;
}
declare function link({ schema, parent, path, $refs, stack, filePath, fileName, }: LinkNodeOptions): ParsedJSONSchemaObject;
interface ParseOptions {
cwd: string;
fileName: string;
}
declare function parse(schema: JSONSchema, options: ParseOptions): Promise<{
root: ParsedJSONSchema;
get: ($ref: string) => ParsedJSONSchema;
referencedSchemas: ParsedJSONSchema[];
}>;
/**
* Determines if a value is a plain object (i.e., created by {} or new Object()).
* This function is a replacement for lodash.isPlainObject.
*/
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
declare function assert(condition: unknown, message?: string): asserts condition;
export { Meta, SchemaKeys, assert, isPlainObject, link, parse };
export type { JSONSchema, JSONSchemaTypeName, LinkNodeOptions, ParsedJSONSchema, ParsedJSONSchemaObject, Path, SchemaKeysType };