UNPKG

juniper

Version:

ESM JSON Schema builder for static Typescript inference.

246 lines 11.2 kB
import { AbstractSchema, type ConditionalResult, type SchemaGenerics, type SchemaParams, type SerializationParams } from '../lib/schema.js'; import type { ConditionalNullable, IsAny, IsNever, JsonSchema, Nullable, Schema, SchemaType, ToBaseType } from '../lib/types.js'; import { NeverSchema } from './never.js'; declare const containsSym: unique symbol; export declare const prefixItemsSym: unique symbol; export type ArrayType<T, P extends any[], M, N extends boolean> = Nullable<M & (IsAny<T> extends true ? [...P, ...unknown[]] : IsNever<T> extends true ? P : [...P, ...T[]]), N>; export interface ArrayParams<T, P extends any[], C extends P[number] | T, M, N extends boolean> extends SchemaParams<ArrayType<T, P, M, N>> { items?: AbstractSchema<SchemaGenerics<T>> | null; maxContains?: number; maxItems?: number; minContains?: number; minItems?: number; uniqueItems?: boolean; [containsSym]?: AbstractSchema<SchemaGenerics<C>> | null; [prefixItemsSym]?: AbstractSchema<SchemaGenerics<P[number]>>[]; } interface ArrayGenerics<T, P extends any[], C extends P[number] | T, M, N extends boolean> extends SchemaGenerics<ArrayType<T, P, M, N>> { params: ArrayParams<T, P, C, M, N>; } export type AnyArraySchema = ArraySchema<any, any[], any, unknown, boolean>; /** * Schema for defining `array` types. * * Note that some implementations of `uniqueItems=true` may convert to a `Set` * but that is not reflected here. * * Tuples are supported via `prefixItems`. * * `items` may only be set once. * Similarly `contains` may only be set once, after `items`. * * __Tuples and Contains are not supported in OpenAPI 3.0__ * * If generating a schema for OpenApi 3.0, the result will just be an array * where any item can be in any location. * `contains` will be ignored altogether. * * @template T * @template P * @template C * @template M * @template N */ export declare class ArraySchema<T = any, P extends any[] = [], C extends P[number] | T = never, M = unknown, N extends boolean = false> extends AbstractSchema<ArrayGenerics<T, P, C, M, N>> { #private; /** * Specific instance of `NeverSchema`. * * Pass it to `items` to result in `items: false` rather then `items: { not: {} }`. * Required for a "strict" tuple. */ static readonly falseItems: NeverSchema; protected readonly schemaType = "array"; allOf: <S extends ArraySchema<any, any[], any, unknown, boolean>>(this: AnyArraySchema, schema: S) => ArraySchema<T, P, C, M & NonNullable<SchemaType<S>>, null extends SchemaType<S> ? N : boolean>; anyOf: <S extends ArraySchema<any, any[], any, unknown, boolean>>(this: AnyArraySchema, schemas: S[]) => ArraySchema<T, P, C, M & NonNullable<SchemaType<S>>, null extends SchemaType<S> ? N : boolean>; if: <IfT, IfP extends any[], IfC extends IfP[number] | IfT, IfM, IfN extends boolean, Then extends Schema<unknown[] | null> = ArraySchema, Else extends Schema<unknown[] | null> = ArraySchema>(this: AnyArraySchema, schema: ArraySchema<IfT, IfP, IfC, IfM, IfN>, conditionals: ConditionalResult<Then, Else>) => ArraySchema<T, P, C, M & (NonNullable<SchemaType<Else>> | (ArrayType<IfT, IfP, IfM, false> & NonNullable<SchemaType<Then>>)), ConditionalNullable<N, IfN, null extends SchemaType<Then> ? true : boolean, null extends SchemaType<Else> ? true : boolean>>; not: <NotN extends boolean>(this: AnyArraySchema, schema: ArraySchema<any, any[], any, unknown, NotN>) => NotN extends true ? ArraySchema<T, P, C, unknown, boolean> : this; nullable: (this: AnyArraySchema) => ArraySchema<T, P, C, unknown, boolean extends N ? boolean : true>; oneOf: <S extends ArraySchema<any, any[], any, unknown, boolean>>(this: AnyArraySchema, schemas: S[]) => ArraySchema<T, P, C, M & NonNullable<SchemaType<S>>, null extends SchemaType<S> ? N : boolean>; /** * @override */ constructor(items?: AbstractSchema<SchemaGenerics<T>> | ArrayParams<T, P, C, M, N>); /** * Create a new instance of ArraySchema. * * Conforming to other Schema constructors, all input is optional and can be appended via * methods. * However the `items` schema may be passed as the only input. * If it is not passed, the `items()` method may be used to type the array. * * @example * // The following 3 constructors will create identical types/schemas: `number[]`. * const arr = ArraySchema().items(NumberSchema()); * const arr2 = ArraySchema({ items: NumberSchema() }); * const arr3 = ArraySchema(NumberSchema()); * * @param [this] - this instance * @param [options] - options or schema * @param [options.items] - array items entries * @param [options.minItems] - minimum items in array (inclusive) * @param [options.maxItems] - maximum items of array (inclusive) * @param [options.minContains] - minimum instances of contained schema (inclusive) * @param [options.maxContains] - maximum instances of contained schema (inclusive) * @param [options.uniqueItems] - each item is unique * @param [options.title] - Add title to schema * @param [options.description] - Add description to schema * @param [options.deprecated] - flag schema as deprecated * @param [options.readOnly] - value should not be modified * @param [options.writeOnly] - value should be hidden * @returns array schema instance */ static create<T2 = any>(this: void, options?: AbstractSchema<SchemaGenerics<T2>> | ArrayParams<T2, [], never, unknown, false>): ArraySchema<T2>; /** * Set the `items` of the array. * * Can only be set once. * * @see {@link https://json-schema.org/understanding-json-schema/reference/array.html#items} * * @param this - this instance * @param items - items property * @returns newly typed array schema */ items<T2>(this: this, items: AbstractSchema<SchemaGenerics<T2>>, invalid: IsAny<T> extends true ? void : never): ArraySchema<T2, P, C, unknown, N>; /** * Set the `maxItems` of the array. * Set to `Infinity` to effectively clear restriction. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.1} * * @param this - this instance * @param maxItems - max items property * @returns cloned schema */ maxItems(this: this, maxItems: number): this; /** * Set the `minItems` of the array. * Set to `0` to effectively clear restriction. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.2} * * @param this - this instance * @param minItems - min items property * @returns cloned schema */ minItems(this: this, minItems: number): this; /** * Set the `uniqueItems` of the array. * Set to `false` to effectively clear restriction. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.3} * * @param this - this instance * @param uniqueItems - unique items property * @returns cloned schema */ uniqueItems(this: this, uniqueItems: boolean): this; /** * Set the `contains` of the array. * * Can only be set once. * * __Not supported by OpenApi 3.0__. * * @see {@link https://json-schema.org/understanding-json-schema/reference/array.html#contains} * * @param this - this instance * @param contains - contains schema property * @returns cloned schema */ contains<C2 extends ToBaseType<P[number] | T>>(this: this, items: AbstractSchema<SchemaGenerics<C2>>, invalid: IsNever<C> extends true ? void : never): ArraySchema<T, P, C2, M, N>; /** * Set the `maxContains` of the array. * Set to `Infinity` to effectively clear restriction. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.4} * * @param this - this instance * @param maxContains - max contains property * @returns cloned schema */ maxContains(this: this, maxContains: number): this; /** * Set the `minContains` of the array. * Set to `1` to effectively clear restriction. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.4.5} * * @param this - this instance * @param minContains - min contains property * @returns cloned schema */ minContains(this: this, minContains: number): this; /** * Append a `prefixItem` to the tuple. * * __Not supported by OpenApi 3.0__. * * @param this - this instance * @param schema - schema of tuple item * @returns cloned schema */ prefixItem<NewP>(this: this, schema: AbstractSchema<SchemaGenerics<NewP>>): ArraySchema<T, [...P, NewP], C, M, N>; /** * Prepend a schema to `prefixItem`. * * __Not supported by OpenApi 3.0__. * * @param this - this instance * @param schema - schema of tuple item * @returns cloned schema */ prependPrefixItem<NewP>(this: this, schema: AbstractSchema<SchemaGenerics<NewP>>): ArraySchema<T, [NewP, ...P], C, M, N>; /** * @override */ protected getCloneParams(): Required<ArrayParams<T, P, C, M, N>>; /** * @override */ protected static getDefaultValues(params: SerializationParams): Record<string, unknown>; /** * @override */ protected toSchema(this: this, params: SerializationParams): JsonSchema<SchemaType<this>>; } /** * Used by TupleSchema to allow overriding declarations. * * @template T * @template P * @template C * @template M * @template N */ export declare class IArraySchemaOverride<T, P extends any[], C extends P[number] | T, M, N extends boolean> extends ArraySchema<T, P, C, M, N> { static create: any; allOf: (this: any, schema: ArraySchema<any, any[], any, unknown, boolean>) => any; anyOf: <S extends ArraySchema<any, any[], any, unknown, boolean>>(this: any, schemas: S[]) => any; if: <IfT, IfP extends any[], IfC extends IfP[number] | IfT, IfM, IfN extends boolean, Then extends Schema<unknown[] | null> = ArraySchema, Else extends Schema<unknown[] | null> = ArraySchema>(this: any, schema: ArraySchema<IfT, IfP, IfC, IfM, IfN>, conditionals: ConditionalResult<Then, Else>) => any; not: any; nullable: (this: any) => any; oneOf: <S extends ArraySchema<any, any[], any, unknown, boolean>>(this: any, schemas: S[]) => any; contains: any; prefixItem: any; prependPrefixItem: any; } export declare const ArraySchemaOverride: typeof IArraySchemaOverride; export {}; //# sourceMappingURL=array.d.ts.map