import type { StandardJSONSchemaV1 } from '@standard-schema/spec'; export declare const typeCache: unique symbol; /** * Value of a JSON Schema `type` keyword. */ export type JsonSchemaType = | 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string'; export interface JsonSchema { [key: string]: unknown; [typeCache]?: { type: T }; allOf?: Omit, typeof typeCache>[]; anyOf?: Omit, typeof typeCache>[]; dependentSchemas?: Record, typeof typeCache>>; if?: JsonSchema; else?: JsonSchema; enum?: readonly unknown[]; patternProperties?: Record>; properties?: Record>; then?: JsonSchema; type?: JsonSchemaType | JsonSchemaType[]; } /** * A schema serialized with `toJSON({ openApi30: true })`. Unlike {@link JsonSchema}, sub-schemas are * never `true` or `false`, and `type` is a single type (with `nullable` for `null`), as OpenAPI 3.0 * requires. So it can be assigned to OpenAPI libraries' 3.0 schema types. * * @template T - type the schema describes */ export interface OpenApi30JsonSchema { [key: string]: unknown; [typeCache]?: { type: T }; $ref?: string; additionalProperties?: boolean | OpenApi30JsonSchema; allOf?: OpenApi30JsonSchema[]; anyOf?: OpenApi30JsonSchema[]; // Not `readonly`, which OpenAPI libraries' `enum?: any[]` rejects enum?: unknown[]; items?: OpenApi30JsonSchema; not?: OpenApi30JsonSchema; nullable?: boolean; oneOf?: OpenApi30JsonSchema[]; properties?: Record>; type?: Exclude; } export interface ToJsonParams { /** * `$ref` prefix for defined schemas (see `define`). * Defaults to `#/$defs/` (embedded as `$defs`), or `#/components/schemas/` for OpenAPI 3.0. */ definitionsPath?: string; /** * $id of schema */ id?: string; /** * Use syntax complaint with OpenAPI 3.0. */ openApi30?: boolean; /** * Include `$schema` keyword for draft 2020-12. */ schema?: boolean; } /** * Any Juniper schema describing `T`, for accepting a schema and rendering it (`toJSON`), * or passing it to `components`, `serializeSpec` and `anyOf`. * * Cannot be passed to builders that nest a schema (`properties`, `arraySchema(...)`): they need the * schema's own class type, so keep the inferred type of the schema you built. * * @template T */ export interface Schema extends StandardJSONSchemaV1 { [typeCache]?: { type: T; }; toJSON: { (params: ToJsonParams & { openApi30: true }): OpenApi30JsonSchema; (params?: ToJsonParams): JsonSchema; }; } export type SchemaType = T extends { [typeCache]?: { type: infer U; }; } ? U : never; declare const emptyObject: unique symbol; export interface EmptyIndex { [key: string]: never; [emptyObject]?: never; } /** * An object with no known keys. Unlike `{}`, rejects primitives. */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- Not an interface, which would lack the implicit index signature to extend `Record` export type EmptyObject = { [emptyObject]?: never }; /** * Flatten an intersection of object types into a single object type, so editors display * `{ a: string; b?: number }` instead of `Partial<{ ... }> & Required>`. * * Distributes over unions. Property types, modifiers and index signatures are unchanged, * so the result is mutually assignable with `T`. The `EmptyObject` marker key is dropped, * and restored as `EmptyObject` if no other keys remain. * * @template T object type to flatten */ export type FlattenObject = IsAny extends true ? T : T extends unknown ? [Exclude] extends [never] ? EmptyObject : { [K in keyof T as K extends typeof emptyObject ? never : K]: T[K] } : never; /** * Intersect two types, flattening any resulting object types (see {@link FlattenObject}). * Arrays and primitives (e.g. template literals) are intersected as is. * * Either side being `unknown` returns the other side untouched, so a lone named type * (e.g. an interface from `customSchema`) keeps its name. * * @template A type to intersect * @template B type to intersect */ export type Intersect = IsUnknown extends true ? B : IsUnknown extends true ? A : FlattenObjects; type FlattenObjects = T extends readonly unknown[] ? T : T extends object ? FlattenObject : T; // From `expect-types` type Not = T extends true ? false : true; export type IsNever = [T] extends [never] ? true : false; export type IsAny = [T] extends [typeof typeCache] ? Not> : false; export type IsUnknown = [unknown] extends [T] ? Not> : false; /** * "Clean" a type from a definition. * Generally used for "prettifying" types, not actual logic changing. * * e.g. AbstractClean => 123 * * @template Base Base type to strip out * @template Check Type to be stripped of Base * @template Stripped convenience value for caching stripped, do not provide */ export type AbstractClean< Base, Check, Stripped = Check extends Base & infer U ? U : Check, > = Stripped extends Base ? Stripped : Check; export type AbstractStrip = Base extends infer U & ToStrip ? IsUnknown extends true ? Replacement : U : Base; /** * Append `| null` to type. * `true` = add `| null`. Calling `nullable()` is NOOP. * `false` = Do not add `| null`. Calling `nullable()` should append `| null`. * `boolean` = Do not add `| null`. Calling `nullable()` should not append `| null`. * * @template T value to have `| null` attached * @template N if `true` literal, attach null */ export type Nullable = [N] extends [true] ? T | null : T; /** * Accepts `nullSchema()` in `not` only when the schema is nullable, as it would do nothing otherwise. * * @template N nullability of the schema `not` is called on */ export type NotNullParameter = [N] extends [true] ? unknown : { error: 'Error: schema is not nullable.' }; export type ConditionalNullable< Base extends boolean, If extends boolean, Then extends boolean, Else extends boolean, > = [If] extends [true] ? [Then] extends [true] ? Base : boolean : [Else] extends [true] ? Base : boolean; export type ToBaseType = T extends never ? never : T extends boolean ? boolean : T extends number ? number : T extends string ? string : T extends readonly unknown[] ? ToBaseType[] : T extends Record ? Record : T; const reservedWordsArr = [ // Generic '$schema', '$id', 'default', 'description', 'example', 'examples', 'nullable', 'title', 'type', // Composition 'allOf', 'anyOf', 'else', 'if', 'not', 'oneOf', 'then', // Enum 'enum', 'const', // Object 'maxProperties', 'minProperties', 'properties', 'patternProperties', 'required', 'dependentRequired', 'dependentSchemas', 'unevaluatedProperties', // Array 'items', 'maxItems', 'minItems', 'prefixItems', 'uniqueItems', // Number 'exclusiveMaximum', 'exclusiveMinimum', 'maximum', 'minimum', 'multipleOf', // String 'contentEncoding', 'contentMediaType', 'format', 'maxLength', 'minLength', 'pattern', ] as const; export const reservedWords = new Set(reservedWordsArr); export type ReservedWords = (typeof reservedWordsArr)[number];