UNPKG

juniper

Version:

ESM JSON Schema builder for static Typescript inference.

155 lines 6.67 kB
import { AbstractSchema, type ConditionalResult, type SchemaGenerics, type SchemaParams, type SerializationParams } from '../lib/schema.js'; import type { AbstractClean, ConditionalNullable, JsonSchema, Nullable, SchemaType } from '../lib/types.js'; interface LimitWithExclusive { value: number; exclusive: boolean; } interface NumberParams<T extends number, N extends boolean> extends SchemaParams<Nullable<T, N>> { type?: 'integer' | 'number'; multipleOf?: number | number[]; maximum?: number | LimitWithExclusive; minimum?: number | LimitWithExclusive; } interface NumberGenerics<T extends number, N extends boolean> extends SchemaGenerics<Nullable<T, N>> { params: NumberParams<T, N>; } type StripNumber<T extends number> = AbstractClean<number, T>; type AnyNumberSchema = NumberSchema<number, boolean>; /** * Schema for defining numeric types. * * Defaults to `type=number` (any valid number) but can be overwritten to `type=integer` (no fraction). * * Some `pattern` assertions are built in to help typescript, but most * likely end typescript result will just be `string`. * * Supports multiple `multipleOf`s. * * @template T * @template N */ export declare class NumberSchema<T extends number, N extends boolean = false> extends AbstractSchema<NumberGenerics<T, N>> { #private; protected readonly schemaType: "number" | "integer"; allOf: <S extends NumberSchema<number, boolean>>(this: AnyNumberSchema, schema: S) => NumberSchema<NonNullable<SchemaType<S>> & T, null extends SchemaType<S> ? N : boolean>; anyOf: <S extends NumberSchema<number, boolean>>(this: AnyNumberSchema, schemas: S[]) => NumberSchema<NonNullable<SchemaType<S>> & T, null extends SchemaType<S> ? N : boolean>; if: <IfT extends number, IfN extends boolean, ThenT extends number, ElseT extends number, ThenN extends boolean = true, ElseN extends boolean = true>(this: AnyNumberSchema, schema: NumberSchema<IfT, IfN>, conditionals: ConditionalResult<NumberSchema<ThenT, ThenN>, NumberSchema<ElseT, ElseN>>) => NumberSchema<StripNumber<T & (ElseT | (IfT & ThenT))>, ConditionalNullable<N, IfN, ThenN, ElseN>>; not: <NotN extends boolean>(this: AnyNumberSchema, schema: NumberSchema<number, NotN>) => NotN extends true ? NumberSchema<T, boolean> : this; nullable: (this: AnyNumberSchema) => NumberSchema<T, boolean extends N ? boolean : true>; oneOf: <S extends NumberSchema<number, boolean>>(this: AnyNumberSchema, schemas: S[]) => NumberSchema<NonNullable<SchemaType<S>> & T, null extends SchemaType<S> ? N : boolean>; /** * @override */ constructor(options?: NumberParams<T, N>); /** * Create a new instance of NumberSchema. * * @param [options] - optional * @param [options.minimum] - minimum value of number, defaults to inclusive. * @param [options.maximum] - maximum value of number, defaults to inclusive. * @param [options.multipleOf] - factors of number. * @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 number schema */ static create<T2 extends number>(this: void, options?: NumberParams<T2, false>): NumberSchema<T2>; /** * Set the `type` of the number. * * Does not alter typings. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.1.1} * * @param this - this instance * @param type - `integer` or `number` * @returns cloned schema */ type(this: this, type: 'integer' | 'number'): this; /** * Set the `multipleOf` of the number. * * Does not alter typings. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.1} * * @param this - this instance * @param multipleOf - multiple of property * @returns cloned schema */ multipleOf(this: this, multipleOf: number): this; /** * Set the `maximum` of the number. * Optionally provide `exclusive` (default=false). * 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.2.2} * * @param this - this instance * @param maximum - maximum property * @returns cloned schema */ maximum(this: this, maximum: number | LimitWithExclusive): this; /** * Set the `exclusiveMaximum` of the number. * Convenience method around `maximum` with `exclusive=true`. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.3} * * @param this - this instance * @param exclusiveMaximum - exclusive maximum property * @returns cloned schema */ exclusiveMaximum(this: this, exclusiveMaximum: number): this; /** * Set the `minimum` of the number. * Optionally provide `exclusive` (default=false). * 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.2.4} * * @param this - this instance * @param minimum - minimum property * @returns cloned schema */ minimum(this: this, minimum: number | LimitWithExclusive): this; /** * Set the `exclusiveMinimum` of the number. * Convenience method around `minimum` with `exclusive=true`. * * Does not alter typings. * Overwrites existing restriction. * * @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.5} * * @param this - this instance * @param exclusiveMinimum - exclusive minimum property * @returns cloned schema */ exclusiveMinimum(this: this, exclusiveMinimum: number): this; /** * @override */ protected getCloneParams(): Required<NumberParams<T, N>>; /** * @override */ protected static getDefaultValues(params: SerializationParams): Record<string, unknown>; /** * @override */ protected toSchema(params: SerializationParams): JsonSchema<SchemaType<this>>; } export {}; //# sourceMappingURL=number.d.ts.map