UNPKG

juniper

Version:

ESM JSON Schema builder for static Typescript inference.

226 lines 13.2 kB
import { AbstractSchema, type ConditionalResult, type SchemaGenerics, type SchemaParams, type SerializationParams } from '../lib/schema.js'; import type { AbstractStrip, ConditionalNullable, EmptyIndex, IsNever, JsonSchema, Nullable, Schema, SchemaType } from '../lib/types.js'; import { MergeSchema } from './merge.js'; import { NeverSchema } from './never.js'; declare const dependentRequiredSym: unique symbol; declare const dependentSchemasSym: unique symbol; declare const patternPropertiesSym: unique symbol; declare const ignoreUnevaluatedProperties: unique symbol; declare const trueSchema: MergeSchema<unknown>; declare const falseSchema: NeverSchema; export type PatternProperties<T extends string> = string & { [patternPropertiesSym]?: T; }; type BaseSchemaObject = Record<string, AbstractSchema<SchemaGenerics<unknown>>>; type BaseParameterSchemaObject = Record<string, boolean | AbstractSchema<SchemaGenerics<unknown>>>; type StripBoolean<S extends boolean | Schema<unknown>> = S extends boolean ? S extends false ? typeof falseSchema : typeof trueSchema : Exclude<S, boolean>; type StripBooleanParameterSchemaObject<P extends BaseParameterSchemaObject> = { [k in keyof P]: StripBoolean<P[k]>; }; export type EmptyObject = Omit<EmptyIndex, number | string>; type StripString<T extends string> = AbstractStrip<T, string>; type ObjectType<P extends BaseSchemaObject, R extends StripString<Extract<keyof P, string>>, A extends boolean | AbstractSchema<SchemaGenerics<unknown>>, X extends Record<string, unknown>, M, N extends boolean, Stripped extends AbstractStrip<P, EmptyIndex> = AbstractStrip<P, EmptyObject>> = Nullable<AbstractStrip<AbstractStrip<AbstractStrip<X, EmptyIndex, unknown> & EmptyObject & M & ([A] extends [true] ? Record<string, unknown> : unknown) & (A extends AbstractSchema<SchemaGenerics<infer V>> ? Record<string, V> : unknown) & (IsNever<Stripped> extends true ? unknown : Partial<{ [K in keyof Stripped]: SchemaType<Stripped[K]>; }> & Required<Pick<{ [K in keyof Stripped]: SchemaType<Stripped[K]>; }, R>>), EmptyObject, EmptyObject>, {}, EmptyObject>, N>; interface ObjectParams<P extends BaseParameterSchemaObject, R extends StripString<Extract<keyof P, string>>, A extends boolean | AbstractSchema<SchemaGenerics<unknown>>, X extends Record<string, unknown>, M, N extends boolean> extends SchemaParams<ObjectType<StripBooleanParameterSchemaObject<P>, R, A, X, M, N>> { additionalProperties?: A; minProperties?: number; maxProperties?: number; properties?: P; required?: R[]; unevaluatedProperties?: boolean | typeof ignoreUnevaluatedProperties; [dependentRequiredSym]?: Record<string, string[]>; [dependentSchemasSym]?: Record<string, AbstractSchema<SchemaGenerics<Record<string, unknown> | null>>>; [patternPropertiesSym]?: Record<string, AbstractSchema<SchemaGenerics<unknown>>>; } interface ObjectGenerics<P extends BaseParameterSchemaObject, R extends StripString<Extract<keyof P, string>>, A extends boolean | AbstractSchema<SchemaGenerics<unknown>>, X extends Record<string, unknown>, M, N extends boolean> extends SchemaGenerics<ObjectType<StripBooleanParameterSchemaObject<P>, R, A, X, M, N>> { params: ObjectParams<P, R, A, X, M, N>; } type AnyObjectSchema = ObjectSchema<any, any, any, any, unknown, boolean>; /** * Schema for defining `object` types. * * @template P * @template R * @template A * @template X * @template M * @template N */ export declare class ObjectSchema<P extends BaseParameterSchemaObject = {}, R extends StripString<Extract<keyof P, string>> = never, A extends boolean | AbstractSchema<SchemaGenerics<unknown>> = boolean, X extends Record<string, unknown> = EmptyIndex, M = unknown, N extends boolean = false> extends AbstractSchema<ObjectGenerics<P, R, A, X, M, N>> { #private; protected readonly schemaType = "object"; allOf: <S extends Schema<Record<string, unknown> | null>>(this: AnyObjectSchema, schema: S) => ObjectSchema<P, R, A, X, M & NonNullable<SchemaType<S>>, null extends SchemaType<S> ? N : boolean>; anyOf: <S extends Schema<Record<string, unknown> | null>>(this: AnyObjectSchema, schemas: S[]) => ObjectSchema<P, R, A, X, M & NonNullable<SchemaType<S>>, null extends SchemaType<S> ? N : boolean>; if: <IfP extends BaseParameterSchemaObject, IfR extends StripString<Extract<keyof IfP, string>>, IfA extends boolean | AbstractSchema<SchemaGenerics<unknown>>, IfX extends Record<string, unknown>, IfM, IfN extends boolean, Then extends Schema<Record<string, unknown> | null> = ObjectSchema, Else extends Schema<Record<string, unknown> | null> = ObjectSchema>(this: AnyObjectSchema, schema: ObjectSchema<IfP, IfR, IfA, IfX, IfM, IfN>, conditionals: ConditionalResult<Then, Else>) => ObjectSchema<P, R, A, X, M & (NonNullable<SchemaType<Else>> | (NonNullable<SchemaType<Then>> & ObjectType<StripBooleanParameterSchemaObject<IfP>, IfR, IfA, IfX, IfM, false>)), ConditionalNullable<N, IfN, null extends SchemaType<Then> ? true : boolean, null extends SchemaType<Else> ? true : boolean>>; not: <NotP extends BaseParameterSchemaObject, NotR extends StripString<Extract<keyof NotP, string>>, NotN extends boolean>(this: AnyObjectSchema, schema: ObjectSchema<NotP, NotR, any, any, any, NotN>) => NotN extends true ? ObjectSchema<P, R, A, X, M, boolean> : this; nullable: (this: AnyObjectSchema) => ObjectSchema<P, R, A, X, M, boolean extends N ? boolean : true>; oneOf: <S extends Schema<Record<string, unknown> | null>>(this: AnyObjectSchema, schemas: S[]) => ObjectSchema<P, R, A, X, M & NonNullable<SchemaType<S>>, null extends SchemaType<S> ? N : boolean>; /** * @override */ constructor(options?: ObjectParams<P, R, A, X, M, N>); /** * Create a new instance of ObjectSchema. * * @param [options] - optional * @param [options.additionalProperties] - allow additional properties * @param [options.minProperties] - minimum properties in object (inclusive) * @param [options.maxProperties] - maximum properties in object (inclusive) * @param [options.properties] - dictionary of property schemas * @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 new object schema */ static create<P2 extends BaseParameterSchemaObject = {}, R2 extends StripString<Extract<keyof P2, string>> = never, A2 extends boolean | AbstractSchema<SchemaGenerics<unknown>> = boolean>(this: void, options?: ObjectParams<P2, R2, A2, EmptyIndex, unknown, false>): ObjectSchema<P2, R2, A2>; /** * Append `properties` to the object. * * Properties are "optional" until explicitly required. * * Duplicate properties are rejected. * * @see {@link https://json-schema.org/understanding-json-schema/reference/object.html#properties} * * @param this - this instance * @param properties - Schemas keyed by property name * @returns cloned object schema */ properties<T extends BaseParameterSchemaObject>(this: this, properties: T, ...invalid: keyof P & keyof T extends never ? [] : [never]): ObjectSchema<P & T, R, A, X, M, N>; /** * Set the `maxProperties` of the object. * 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.5.1} * * @param this - this instance * @param maxProperties - maxProperties property * @returns cloned object schema */ maxProperties(this: this, maxProperties: number): this; /** * Set the `minProperties` of the object. * 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.5.2} * * @param this - this instance * @param minProperties - minProperties property * @returns cloned object schema */ minProperties(this: this, minProperties: number): this; /** * Mark a property as `required`. * Requires property schema to already be set. * * Extends existing `required`. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.5.3} * * @param this - this instance * @param required - required properties * @returns cloned object schema */ required<K extends StripString<Extract<keyof P, string>>>(this: this, required: K | K[]): ObjectSchema<P, K | R, A, X, M, N>; /** * Set `additionalProperties` of object. * * Objects that allow additionalProperties will be indexed with and additional * `Record<string, unknown>` or `Record<string, SchemaType<Schema>>`. * * @see {@link https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties} * * @param this - this instance * @param additionalProperties - additionalProperties property * @returns cloned object schema */ additionalProperties<NewA extends boolean | AbstractSchema<SchemaGenerics<unknown>>>(this: this, additionalProperties: NewA): ObjectSchema<P, R, NewA, X, M, N>; /** * Appends to `patternProperties`. The key is a RegExp pattern with a value * of a JSON Schema. * * This library is not able to deterministically parse the "type" of the pattern * so it relies on manual typing. Use the `PatternProperties` type to case the * RegExp pattern to a Typescript string type. * * This library is not able to detect overlap of patterns, so setting multiple * patterns may have unintentional overlap that is not reflected in the typing. * * __Pattern Properties are not supported in OpenAPI 3.0__ * They will be ignored entirely. * * @see {@link https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties} * * @example * const openApiVendor = objectSchema().patternProperties( * '^x-' as PatternProperties<`x-${string}`>, * unknownSchema() * ); * SchemaType<typeof openApiVendor> // Record<`x-${string}`, unknown> * * @param this - this instance * @param pattern - regexp pattern, typed as PatternProperties * @param schema - Json Schema * @returns cloned object schema */ patternProperties<Pattern extends PatternProperties<string>, S extends boolean | Schema<unknown>>(this: this, pattern: Pattern, schema: S): ObjectSchema<P, R, A, AbstractStrip<X, EmptyIndex, unknown> & Record<NonNullable<Pattern[typeof patternPropertiesSym]>, SchemaType<StripBoolean<S>>>, M, N>; /** * Add a `dependentRequired` property to the JSON schema. * * @see {@link https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentrequired} * * @param this - this instance * @param key - property of object that if exists, `dependents` are required. * @param dependents - dependents that are required if `key` exists. * @returns cloned object schema */ dependentRequired<K extends string & Extract<keyof P, string>, D extends Exclude<Extract<keyof P, string>, K>>(this: this, key: K, dependents: D[]): ObjectSchema<P, R, A, X, M & (Partial<Record<K, never>> | { [k in D]: SchemaType<StripBooleanParameterSchemaObject<P>[k]>; }), N>; /** * Add a `dependentSchema` property to the JSON schema. * * @see {@link https://json-schema.org/understanding-json-schema/reference/conditionals.html#dependentschemas} * * @param this - this instance * @param key - property of object that if exists, `schema` is applied. * @param schema - schema that is applied if `key` exists. * @returns cloned object schema */ dependentSchemas<K extends string & Extract<keyof P, string>, S extends AbstractSchema<SchemaGenerics<Record<string, unknown> | null>>>(this: this, key: K, schema: S): ObjectSchema<P, R, A, X, M & (NonNullable<SchemaType<S>> | Partial<Record<K, never>>), N>; /** * Set `unevaluatedProperties` property on the JSON schema. * * @see {@link https://json-schema.org/understanding-json-schema/reference/object.html#unevaluated-properties} * * @param this - this instance * @param unevaluatedProperties - allow unevaluated properties * @returns cloned object schema */ unevaluatedProperties(this: this, unevaluatedProperties: boolean): this; /** * @override */ protected getCloneParams(): Required<ObjectParams<P, R, A, X, M, N>>; /** * @override */ protected static getDefaultValues(params: SerializationParams): Record<string, unknown>; /** * @override */ protected toSchema(params: SerializationParams): JsonSchema<SchemaType<this>>; } export {}; //# sourceMappingURL=object.d.ts.map