UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

134 lines (133 loc) 5.85 kB
import type { AnyObject, BaseDBEntity, IsoDate, UnixTimestamp } from '../types.js'; import type { JsonSchema, JsonSchemaAllOf, JsonSchemaAny, JsonSchemaArray, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaOneOf, JsonSchemaRef, JsonSchemaString, JsonSchemaTuple } from './jsonSchema.model.js'; export interface JsonSchemaBuilder<T = unknown> { build: () => JsonSchema<T>; } /** * Fluent (chainable) API to manually create Json Schemas. * Inspired by Joi and Zod. */ export declare const j: { any<T = unknown>(): JsonSchemaAnyBuilder<T, JsonSchemaAny<T>>; const<T = unknown>(value: T): JsonSchemaAnyBuilder<T, JsonSchemaConst<T>>; null(): JsonSchemaAnyBuilder<null, JsonSchemaNull>; ref<T = unknown>($ref: string): JsonSchemaAnyBuilder<T, JsonSchemaRef<T>>; enum<T = unknown>(enumValues: T[]): JsonSchemaAnyBuilder<T, JsonSchemaEnum<T>>; boolean(): JsonSchemaAnyBuilder<boolean, JsonSchemaBoolean>; buffer(): JsonSchemaAnyBuilder<Buffer<ArrayBufferLike>, JsonSchemaAny<Buffer<ArrayBufferLike>>>; number<T extends number = number>(): JsonSchemaNumberBuilder<T>; integer<T extends number = number>(): JsonSchemaNumberBuilder<T>; unixTimestamp(): JsonSchemaNumberBuilder<UnixTimestamp>; unixTimestamp2000(): JsonSchemaNumberBuilder<UnixTimestamp>; string<T extends string = string>(): JsonSchemaStringBuilder<T>; isoDate(): JsonSchemaStringBuilder<IsoDate>; object<T extends AnyObject>(props: { [K in keyof T]: JsonSchemaAnyBuilder<T[K]>; }): JsonSchemaObjectBuilder<T>; rootObject<T extends AnyObject>(props: { [K in keyof T]: JsonSchemaAnyBuilder<T[K]>; }): JsonSchemaObjectBuilder<T>; array<ITEM = unknown>(itemSchema: JsonSchemaAnyBuilder<ITEM>): JsonSchemaArrayBuilder<ITEM>; tuple<T extends any[] = unknown[]>(items: JsonSchemaAnyBuilder[]): JsonSchemaTupleBuilder<T>; oneOf<T = unknown>(items: JsonSchemaAnyBuilder[]): JsonSchemaAnyBuilder<T, JsonSchemaOneOf<T>>; allOf<T = unknown>(items: JsonSchemaAnyBuilder[]): JsonSchemaAnyBuilder<T, JsonSchemaAllOf<T>>; }; export declare class JsonSchemaAnyBuilder<T = unknown, SCHEMA_TYPE extends JsonSchema<T> = JsonSchema<T>> implements JsonSchemaBuilder<T> { protected schema: SCHEMA_TYPE; constructor(schema: SCHEMA_TYPE); /** * Used in ObjectBuilder to access schema.optionalProperty */ getSchema(): SCHEMA_TYPE; $schema($schema: string): this; $schemaDraft7(): this; $id($id: string): this; title(title: string): this; description(description: string): this; deprecated(deprecated?: boolean): this; type(type: string): this; default(v: any): this; oneOf(schemas: JsonSchema[]): this; allOf(schemas: JsonSchema[]): this; instanceof(of: string): this; optional(optional?: boolean): this; /** * Produces a "clean schema object" without methods. * Same as if it would be JSON.stringified. */ build(): SCHEMA_TYPE; clone(): JsonSchemaAnyBuilder<T, SCHEMA_TYPE>; /** * @experimental */ infer: T; } export declare class JsonSchemaNumberBuilder<T extends number = number> extends JsonSchemaAnyBuilder<T, JsonSchemaNumber<T>> { constructor(); integer(): this; multipleOf(multipleOf: number): this; min(minimum: number): this; exclusiveMin(exclusiveMinimum: number): this; max(maximum: number): this; exclusiveMax(exclusiveMaximum: number): this; /** * Both ranges are inclusive. */ range(minimum: number, maximum: number): this; format(format: string): this; int32: () => this; int64: () => this; float: () => this; double: () => this; unixTimestamp: () => this; unixTimestamp2000: () => this; unixTimestampMillis: () => this; unixTimestampMillis2000: () => this; utcOffset: () => this; utcOffsetHours: () => this; } export declare class JsonSchemaStringBuilder<T extends string = string> extends JsonSchemaAnyBuilder<T, JsonSchemaString<T>> { constructor(); pattern(pattern: string): this; min(minLength: number): this; max(maxLength: number): this; length(minLength: number, maxLength: number): this; format(format: string): this; email: () => this; isoDate: () => this; url: () => this; ipv4: () => this; ipv6: () => this; password: () => this; id: () => this; slug: () => this; semVer: () => this; languageTag: () => this; countryCode: () => this; currency: () => this; trim: (trim?: boolean) => this; toLowerCase: (toLowerCase?: boolean) => this; toUpperCase: (toUpperCase?: boolean) => this; private transformModify; } export declare class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyBuilder<T, JsonSchemaObject<T>> { constructor(); addProperties(props: { [k in keyof T]: JsonSchemaBuilder<T[k]>; }): this; /** * Ensures `required` is always sorted and _uniq */ required(required: (keyof T)[]): this; addRequired(required: (keyof T)[]): this; minProps(minProperties: number): this; maxProps(maxProperties: number): this; additionalProps(additionalProperties: boolean): this; baseDBEntity(): JsonSchemaObjectBuilder<T & BaseDBEntity>; extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2>; } export declare class JsonSchemaArrayBuilder<ITEM> extends JsonSchemaAnyBuilder<ITEM[], JsonSchemaArray<ITEM>> { constructor(itemsSchema: JsonSchemaBuilder<ITEM>); min(minItems: number): this; max(maxItems: number): this; unique(uniqueItems: number): this; } export declare class JsonSchemaTupleBuilder<T extends any[]> extends JsonSchemaAnyBuilder<T, JsonSchemaTuple<T>> { constructor(items: JsonSchemaBuilder[]); }