Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: manually format long type definitions #49

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 45 additions & 67 deletions packages/main/src/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,31 @@ import type {Kind} from '@sinclair/typebox';
import type {IfDefined, SchemaFrom} from '@typeschema/core';
import type {CoreValidator} from 'suretype';

type IsTypeboxSchema<TSchema> = [IfDefined<typeof Kind>] extends [never]
? false
: TSchema extends {[Kind]: unknown}
? true
: false;
// prettier-ignore
type IsTypeboxSchema<TSchema> =
[IfDefined<typeof Kind>] extends [never] ? false
: TSchema extends {[Kind]: unknown} ? true
: false;
function isTypeboxSchema(
schema: SchemaFrom<AdapterResolver>,
): schema is SchemaFrom<AdapterResolvers['typebox']> {
return typeof schema === 'object' && Symbol.for('TypeBox.Kind') in schema;
}

type IsSuretypeSchema<TSchema> = [IfDefined<CoreValidator<unknown>>] extends [
never,
]
? false
: TSchema extends CoreValidator<unknown>
? true
: false;
// prettier-ignore
type IsSuretypeSchema<TSchema> =
[IfDefined<CoreValidator<unknown>>] extends [never] ? false
: TSchema extends CoreValidator<unknown> ? true
: false;
function isSuretypeSchema(
schema: SchemaFrom<AdapterResolver>,
): schema is SchemaFrom<AdapterResolvers['suretype']> {
return typeof schema === 'object' && '_annotations' in schema;
}

type IsClassValidatorSchema<TSchema> = TSchema extends new (
...args: unknown[]
) => object
? true
// prettier-ignore
type IsClassValidatorSchema<TSchema> =
TSchema extends new (...args: unknown[]) => object ? true
: false;
function isClassValidatorSchema(
schema: SchemaFrom<AdapterResolver>,
Expand All @@ -41,23 +38,17 @@ function isClassValidatorSchema(
);
}

type IsJSONSchema<TSchema> = TSchema extends {type: unknown}
? true
: TSchema extends {const: unknown}
? true
: TSchema extends {enum: unknown}
? true
: TSchema extends {anyOf: unknown}
? true
: TSchema extends {oneOf: unknown}
? true
: TSchema extends {allOf: unknown}
? true
: TSchema extends {not: unknown}
? true
: TSchema extends {if: unknown}
? true
: false;
// prettier-ignore
type IsJSONSchema<TSchema> =
TSchema extends {type: unknown} ? true
: TSchema extends {const: unknown} ? true
: TSchema extends {enum: unknown} ? true
: TSchema extends {anyOf: unknown} ? true
: TSchema extends {oneOf: unknown} ? true
: TSchema extends {allOf: unknown} ? true
: TSchema extends {not: unknown} ? true
: TSchema extends {if: unknown} ? true
: false;
function isJSONSchema(
schema: SchemaFrom<AdapterResolver>,
): schema is SchemaFrom<AdapterResolvers['json']> {
Expand All @@ -80,43 +71,30 @@ function notJSON<TSchema>(
return schema as any;
}

// prettier-ignore
export type Select<TSchema> =
// eslint-disable-next-line @typescript-eslint/ban-types
TSchema extends Function
? TSchema extends {assert: unknown}
? 'arktype'
: IsClassValidatorSchema<TSchema> extends true
? 'classValidator'
: 'function'
: IsTypeboxSchema<TSchema> extends true
? 'typebox'
: IsSuretypeSchema<TSchema> extends true
? 'suretype'
: TSchema extends {__isYupSchema__: unknown}
? 'yup'
: TSchema extends {_def: unknown}
? 'zod'
: TSchema extends {async: unknown}
? 'valibot'
: TSchema extends {refiner: unknown}
? 'superstruct'
: TSchema extends {_flags: unknown}
? 'joi'
: TSchema extends {encode: unknown}
? 'ioTs'
: TSchema extends {reflect: unknown}
? 'runtypes'
: TSchema extends {ast: unknown}
? 'effect'
: TSchema extends {kind: unknown}
? 'deepkit'
: TSchema extends {addValidator: unknown}
? 'ow'
: TSchema extends {toTerminals: unknown}
? 'valita'
: IsJSONSchema<TSchema> extends true
? 'json'
: 'fastestValidator';
? TSchema extends {assert: unknown} ? 'arktype'
: IsClassValidatorSchema<TSchema> extends true ? 'classValidator'
: 'function'
: TSchema extends object
? IsTypeboxSchema<TSchema> extends true ? 'typebox'
: IsSuretypeSchema<TSchema> extends true ? 'suretype'
: TSchema extends {__isYupSchema__: unknown} ? 'yup'
: TSchema extends {_def: unknown} ? 'zod'
: TSchema extends {async: unknown} ? 'valibot'
: TSchema extends {refiner: unknown} ? 'superstruct'
: TSchema extends {_flags: unknown} ? 'joi'
: TSchema extends {encode: unknown} ? 'ioTs'
: TSchema extends {reflect: unknown} ? 'runtypes'
: TSchema extends {ast: unknown} ? 'effect'
: TSchema extends {kind: unknown} ? 'deepkit'
: TSchema extends {addValidator: unknown} ? 'ow'
: TSchema extends {toTerminals: unknown} ? 'valita'
: IsJSONSchema<TSchema> extends true ? 'json'
: 'fastestValidator'
: never;

export const select: <
TMap extends {
Expand Down
Loading