juniper
Version:
ESM JSON Schema builder for static Typescript inference.
185 lines • 7.95 kB
TypeScript
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 StringParams<T extends string, N extends boolean> extends SchemaParams<Nullable<T, N>> {
/**
* Content encoding.
*/
contentEncoding?: string | null;
contentMediaType?: string | null;
format?: string | null;
maxLength?: number;
minLength?: number;
pattern?: string | string[];
}
interface StringGenerics<T extends string, N extends boolean> extends SchemaGenerics<Nullable<T, N>> {
params: StringParams<T, N>;
}
type StripString<T extends string> = AbstractClean<string, T>;
type AnyStringSchema = StringSchema<string, boolean>;
/**
* Schema for defining `string` types.
*
* Some `pattern` assertions are built in to help typescript, but most
* likely end typescript result will just be `string`.
*
* Supports multiple `patterns`.
*
* @template T
* @template N
*/
export declare class StringSchema<T extends string, N extends boolean = false> extends AbstractSchema<StringGenerics<T, N>> {
#private;
protected readonly schemaType = "string";
allOf: <S extends StringSchema<string, boolean>>(this: AnyStringSchema, schema: S) => StringSchema<NonNullable<SchemaType<S>> & T, null extends SchemaType<S> ? N : boolean>;
anyOf: <S extends StringSchema<string, boolean>>(this: AnyStringSchema, schemas: S[]) => StringSchema<NonNullable<SchemaType<S>> & T, null extends SchemaType<S> ? N : boolean>;
if: <IfT extends string, IfN extends boolean, ThenT extends string, ElseT extends string, ThenN extends boolean = true, ElseN extends boolean = true>(this: AnyStringSchema, schema: StringSchema<IfT, IfN>, conditionals: ConditionalResult<StringSchema<ThenT, ThenN>, StringSchema<ElseT, ElseN>>) => StringSchema<StripString<T & (ElseT | (IfT & ThenT))>, ConditionalNullable<N, IfN, ThenN, ElseN>>;
not: <NotN extends boolean>(this: AnyStringSchema, schema: StringSchema<string, NotN>) => NotN extends true ? StringSchema<T, boolean> : this;
nullable: (this: AnyStringSchema) => StringSchema<T, boolean extends N ? boolean : true>;
oneOf: <S extends StringSchema<string, boolean>>(this: AnyStringSchema, schemas: S[]) => StringSchema<NonNullable<SchemaType<S>> & T, null extends SchemaType<S> ? N : boolean>;
/**
* @override
*/
constructor(options?: StringParams<T, N>);
/**
* Create a new instance of StringSchema.
*
* @param [options] - optional
* @param [options.contentEncoding] - contentEncoding property
* @param [options.contentMediaType] - contentMediaType property
* @param [options.format] - text format of string
* @param [options.minLength] - minimum length of string (inclusive)
* @param [options.maxLength] - maximum length of string (inclusive)
* @param [options.pattern] - RegExp patterns to describe string
* @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 string schema
*/
static create<T2 extends string>(this: void, options?: StringParams<T2, false>): StringSchema<T2>;
/**
* Set the `format` of the string.
* Set to `null` to effectively clear restriction.
*
* Usage and restrictions of `format` are implementation-specific. No enforcement is guaranteed
* by default in JSON Schema.
*
* Does not alter typings.
* Overwrites existing restriction.
*
* @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.7}
*
* @param this - this instance
* @param format - format property
* @returns cloned schema
*/
format(this: this, format: string | null): this;
/**
* Set the `maxLength` of the string.
* 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.3.1}
*
* @param this - this instance
* @param maxLength - max length property
* @returns cloned schema
*/
maxLength(this: this, maxLength: number): this;
/**
* Set the `minLength` of the string.
* 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.3.2}
*
* @param this - this instance
* @param minLength - min length property
* @returns cloned schema
*/
minLength(this: this, minLength: number): this;
/**
* Add a `pattern` to the string.
*
* Optionally alters typings, which restrict outputted string type.
* Use with caution, as there is no validation that pattern actually asserts type.
*
* @see {@link https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.3.3}
*
* @param this - this instance
* @param pattern - regular expression pattern
* @returns cloned schema
*/
pattern<Constraint extends string>(this: this, pattern: string): StringSchema<StripString<Constraint & T>, N>;
/**
* Add a special case `pattern` that enforces a string occurs as the start.
*
* @param this - this instance
* @param start - string literal
* @returns cloned schema
*/
startsWith<Start extends string>(this: this, start: Start): StringSchema<StripString<`${Start}${string}` & T>, N>;
/**
* Add a special case `pattern` that enforces a string occurs as the end.
*
* @param this - this instance
* @param end - string literal
* @returns cloned schema
*/
endsWith<End extends string>(this: this, end: End): StringSchema<StripString<`${string}${End}` & T>, N>;
/**
* Add a special case `pattern` that enforces a string occurs somewhere in the string.
*
* @param this - this instance
* @param contain - string literal
* @returns cloned schema
*/
contains<Contain extends string>(this: this, contain: Contain): StringSchema<StripString<`${string}${Contain}${string}` & T>, N>;
/**
* Set `contentEncoding` of string.
* Set to `null` to remove.
*
* Does not alter typings.
* Overwrites existing encoding.
*
* @see {@link https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentencoding}
*
* @param this - this instance
* @param contentEncoding - content encoding property
* @returns cloned string schema
*/
contentEncoding(this: this, contentEncoding: string | null): this;
/**
* Set `contentMediaType` of string.
* Set to `null` to remove.
*
* Does not alter typings.
* Overwrites existing encoding.
*
* @see {@link https://json-schema.org/understanding-json-schema/reference/non_json_data.html#contentmediatype}
*
* @param this - this instance
* @param contentMediaType - content media type property
* @returns cloned string schema
*/
contentMediaType(this: this, contentMediaType: string | null): this;
/**
* @override
*/
protected getCloneParams(): Required<StringParams<T, N>>;
/**
* @override
*/
protected static getDefaultValues(params: SerializationParams): Record<string, unknown>;
/**
* @override
*/
protected toSchema(params: SerializationParams): JsonSchema<SchemaType<this>>;
}
export {};
//# sourceMappingURL=string.d.ts.map