[6a3a178] | 1 | /** numeric strings */
|
---|
| 2 | declare type NumberType = "float32" | "float64" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32";
|
---|
| 3 | /** string strings */
|
---|
| 4 | declare type StringType = "string" | "timestamp";
|
---|
| 5 | /** Generic JTD Schema without inference of the represented type */
|
---|
| 6 | export declare type SomeJTDSchemaType = (// ref
|
---|
| 7 | {
|
---|
| 8 | ref: string;
|
---|
| 9 | } | {
|
---|
| 10 | type: NumberType | StringType | "boolean";
|
---|
| 11 | } | {
|
---|
| 12 | enum: string[];
|
---|
| 13 | } | {
|
---|
| 14 | elements: SomeJTDSchemaType;
|
---|
| 15 | } | {
|
---|
| 16 | values: SomeJTDSchemaType;
|
---|
| 17 | } | {
|
---|
| 18 | properties: Record<string, SomeJTDSchemaType>;
|
---|
| 19 | optionalProperties?: Record<string, SomeJTDSchemaType>;
|
---|
| 20 | additionalProperties?: boolean;
|
---|
| 21 | } | {
|
---|
| 22 | properties?: Record<string, SomeJTDSchemaType>;
|
---|
| 23 | optionalProperties: Record<string, SomeJTDSchemaType>;
|
---|
| 24 | additionalProperties?: boolean;
|
---|
| 25 | } | {
|
---|
| 26 | discriminator: string;
|
---|
| 27 | mapping: Record<string, SomeJTDSchemaType>;
|
---|
| 28 | } | {}) & {
|
---|
| 29 | nullable?: boolean;
|
---|
| 30 | metadata?: Record<string, unknown>;
|
---|
| 31 | definitions?: Record<string, SomeJTDSchemaType>;
|
---|
| 32 | };
|
---|
| 33 | /** required keys of an object, not undefined */
|
---|
| 34 | declare type RequiredKeys<T> = {
|
---|
| 35 | [K in keyof T]-?: undefined extends T[K] ? never : K;
|
---|
| 36 | }[keyof T];
|
---|
| 37 | /** optional or undifined-able keys of an object */
|
---|
| 38 | declare type OptionalKeys<T> = {
|
---|
| 39 | [K in keyof T]-?: undefined extends T[K] ? K : never;
|
---|
| 40 | }[keyof T];
|
---|
| 41 | /** type is true if T is a union type */
|
---|
| 42 | declare type IsUnion_<T, U extends T = T> = false extends (T extends unknown ? ([U] extends [T] ? false : true) : never) ? false : true;
|
---|
| 43 | declare type IsUnion<T> = IsUnion_<T>;
|
---|
| 44 | /** type is true if T is identically E */
|
---|
| 45 | declare type TypeEquality<T, E> = [T] extends [E] ? ([E] extends [T] ? true : false) : false;
|
---|
| 46 | /** type is true if T or null is identically E or null*/
|
---|
| 47 | declare type NullTypeEquality<T, E> = TypeEquality<T | null, E | null>;
|
---|
| 48 | /** gets only the string literals of a type or null if a type isn't a string literal */
|
---|
| 49 | declare type EnumString<T> = [T] extends [never] ? null : T extends string ? string extends T ? null : T : null;
|
---|
| 50 | /** true if type is a union of string literals */
|
---|
| 51 | declare type IsEnum<T> = null extends EnumString<Exclude<T, null>> ? false : true;
|
---|
| 52 | /** true only if all types are array types (not tuples) */
|
---|
| 53 | declare type IsElements<T> = false extends IsUnion<T> ? [T] extends [readonly unknown[]] ? undefined extends T[0.5] ? false : true : false : false;
|
---|
| 54 | /** true if the the type is a values type */
|
---|
| 55 | declare type IsValues<T> = false extends IsUnion<Exclude<T, null>> ? TypeEquality<keyof Exclude<T, null>, string> : false;
|
---|
| 56 | /** true if type is a proeprties type and Union is false, or type is a discriminator type and Union is true */
|
---|
| 57 | declare type IsRecord<T, Union extends boolean> = Union extends IsUnion<Exclude<T, null>> ? null extends EnumString<keyof Exclude<T, null>> ? false : true : false;
|
---|
| 58 | /** actual schema */
|
---|
| 59 | export declare type JTDSchemaType<T, D extends Record<string, unknown> = Record<string, never>> = (// refs - where null wasn't specified, must match exactly
|
---|
| 60 | (null extends EnumString<keyof D> ? never : ({
|
---|
| 61 | [K in keyof D]: [T] extends [D[K]] ? {
|
---|
| 62 | ref: K;
|
---|
| 63 | } : never;
|
---|
| 64 | }[keyof D] & {
|
---|
| 65 | nullable?: false;
|
---|
| 66 | }) | (null extends T ? {
|
---|
| 67 | [K in keyof D]: [Exclude<T, null>] extends [Exclude<D[K], null>] ? {
|
---|
| 68 | ref: K;
|
---|
| 69 | } : never;
|
---|
| 70 | }[keyof D] & {
|
---|
| 71 | nullable: true;
|
---|
| 72 | } : never)) | (unknown extends T ? {
|
---|
| 73 | nullable?: boolean;
|
---|
| 74 | } : never) | ((true extends NullTypeEquality<T, number> ? {
|
---|
| 75 | type: NumberType;
|
---|
| 76 | } : true extends NullTypeEquality<T, boolean> ? {
|
---|
| 77 | type: "boolean";
|
---|
| 78 | } : true extends NullTypeEquality<T, string> ? {
|
---|
| 79 | type: StringType;
|
---|
| 80 | } : true extends NullTypeEquality<T, Date> ? {
|
---|
| 81 | type: "timestamp";
|
---|
| 82 | } : true extends IsEnum<T> ? {
|
---|
| 83 | enum: EnumString<Exclude<T, null>>[];
|
---|
| 84 | } : true extends IsElements<Exclude<T, null>> ? T extends readonly (infer E)[] ? {
|
---|
| 85 | elements: JTDSchemaType<E, D>;
|
---|
| 86 | } : never : true extends IsValues<T> ? T extends Record<string, infer V> ? {
|
---|
| 87 | values: JTDSchemaType<V, D>;
|
---|
| 88 | } : never : true extends IsRecord<T, false> ? ([RequiredKeys<Exclude<T, null>>] extends [never] ? {
|
---|
| 89 | properties?: Record<string, never>;
|
---|
| 90 | } : {
|
---|
| 91 | properties: {
|
---|
| 92 | [K in RequiredKeys<T>]: JTDSchemaType<T[K], D>;
|
---|
| 93 | };
|
---|
| 94 | }) & ([OptionalKeys<Exclude<T, null>>] extends [never] ? {
|
---|
| 95 | optionalProperties?: Record<string, never>;
|
---|
| 96 | } : {
|
---|
| 97 | optionalProperties: {
|
---|
| 98 | [K in OptionalKeys<T>]: JTDSchemaType<Exclude<T[K], undefined>, D>;
|
---|
| 99 | };
|
---|
| 100 | }) & {
|
---|
| 101 | additionalProperties?: boolean;
|
---|
| 102 | } : true extends IsRecord<T, true> ? {
|
---|
| 103 | [K in keyof Exclude<T, null>]-?: Exclude<T, null>[K] extends string ? {
|
---|
| 104 | discriminator: K;
|
---|
| 105 | mapping: {
|
---|
| 106 | [M in Exclude<T, null>[K]]: JTDSchemaType<Omit<T extends {
|
---|
| 107 | [C in K]: M;
|
---|
| 108 | } ? T : never, K>, D>;
|
---|
| 109 | };
|
---|
| 110 | } : never;
|
---|
| 111 | }[keyof Exclude<T, null>] : never) & (null extends T ? {
|
---|
| 112 | nullable: true;
|
---|
| 113 | } : {
|
---|
| 114 | nullable?: false;
|
---|
| 115 | }))) & {
|
---|
| 116 | metadata?: Record<string, unknown>;
|
---|
| 117 | definitions?: {
|
---|
| 118 | [K in keyof D]: JTDSchemaType<D[K], D>;
|
---|
| 119 | };
|
---|
| 120 | };
|
---|
| 121 | declare type JTDDataDef<S, D extends Record<string, unknown>> = (S extends {
|
---|
| 122 | ref: string;
|
---|
| 123 | } ? D extends {
|
---|
| 124 | [K in S["ref"]]: infer V;
|
---|
| 125 | } ? JTDDataDef<V, D> : never : S extends {
|
---|
| 126 | type: NumberType;
|
---|
| 127 | } ? number : S extends {
|
---|
| 128 | type: "boolean";
|
---|
| 129 | } ? boolean : S extends {
|
---|
| 130 | type: "string";
|
---|
| 131 | } ? string : S extends {
|
---|
| 132 | type: "timestamp";
|
---|
| 133 | } ? string | Date : S extends {
|
---|
| 134 | enum: readonly (infer E)[];
|
---|
| 135 | } ? string extends E ? never : [E] extends [string] ? E : never : S extends {
|
---|
| 136 | elements: infer E;
|
---|
| 137 | } ? JTDDataDef<E, D>[] : S extends {
|
---|
| 138 | properties: Record<string, unknown>;
|
---|
| 139 | optionalProperties?: Record<string, unknown>;
|
---|
| 140 | additionalProperties?: boolean;
|
---|
| 141 | } ? {
|
---|
| 142 | -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
|
---|
| 143 | } & {
|
---|
| 144 | -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
|
---|
| 145 | } & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
|
---|
| 146 | properties?: Record<string, unknown>;
|
---|
| 147 | optionalProperties: Record<string, unknown>;
|
---|
| 148 | additionalProperties?: boolean;
|
---|
| 149 | } ? {
|
---|
| 150 | -readonly [K in keyof S["properties"]]-?: JTDDataDef<S["properties"][K], D>;
|
---|
| 151 | } & {
|
---|
| 152 | -readonly [K in keyof S["optionalProperties"]]+?: JTDDataDef<S["optionalProperties"][K], D>;
|
---|
| 153 | } & ([S["additionalProperties"]] extends [true] ? Record<string, unknown> : unknown) : S extends {
|
---|
| 154 | values: infer V;
|
---|
| 155 | } ? Record<string, JTDDataDef<V, D>> : S extends {
|
---|
| 156 | discriminator: infer M;
|
---|
| 157 | mapping: Record<string, unknown>;
|
---|
| 158 | } ? [M] extends [string] ? {
|
---|
| 159 | [K in keyof S["mapping"]]: JTDDataDef<S["mapping"][K], D> & {
|
---|
| 160 | [KM in M]: K;
|
---|
| 161 | };
|
---|
| 162 | }[keyof S["mapping"]] : never : unknown) | (S extends {
|
---|
| 163 | nullable: true;
|
---|
| 164 | } ? null : never);
|
---|
| 165 | export declare type JTDDataType<S> = S extends {
|
---|
| 166 | definitions: Record<string, unknown>;
|
---|
| 167 | } ? JTDDataDef<S, S["definitions"]> : JTDDataDef<S, Record<string, never>>;
|
---|
| 168 | export {};
|
---|