Skip to content

Commit

Permalink
fix: yup 的类型定义不再挂在 yup/es 下
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 3, 2021
1 parent 95728eb commit 6aa8c6e
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 373 deletions.
3 changes: 1 addition & 2 deletions src/validator/yup.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './yupTypes'
import * as yup from 'yup/es'
import * as yup from './yupTypes'

export { yup }
128 changes: 62 additions & 66 deletions src/validator/yupTypes/Locale.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,73 @@
declare module 'yup/es' {
export type LocaleValueFnParams<TExtra extends {} = {}> = {
path: string
type: string
label?: string
value: any
originalValue: any
} & TExtra
export type LocaleValueFnParams<TExtra extends {} = {}> = {
path: string
type: string
label?: string
value: any
originalValue: any
} & TExtra

export type LocaleValueFn<TExtra extends {} = {}> = (
params: LocaleValueFnParams<TExtra>,
) => string
export type LocaleValueFn<TExtra extends {} = {}> = (
params: LocaleValueFnParams<TExtra>,
) => string

export type LocaleValue<TExtra extends {} = {}> =
| string
| LocaleValueFn<TExtra>
export type LocaleValue<TExtra extends {} = {}> = string | LocaleValueFn<TExtra>

export interface MixedLocale {
default: LocaleValue
required: LocaleValue
oneOf: LocaleValue<{ values: string }>
notOneOf: LocaleValue<{ values: string }>
notType: LocaleValue
defined: LocaleValue
}
export interface MixedLocale {
default: LocaleValue
required: LocaleValue
oneOf: LocaleValue<{ values: string }>
notOneOf: LocaleValue<{ values: string }>
notType: LocaleValue
defined: LocaleValue
}

export interface StringLocale {
length: LocaleValue<{ length: number }>
min: LocaleValue<{ min: number }>
max: LocaleValue<{ max: number }>
matches: LocaleValue<{ regex: RegExp }>
email: LocaleValue<{ regex: RegExp }>
url: LocaleValue<{ regex: RegExp }>
trim: LocaleValue
lowercase: LocaleValue
uppercase: LocaleValue
chineseMobilePhoneNumber: LocaleValue
chineseIDCardNumber: LocaleValue
}
export interface StringLocale {
length: LocaleValue<{ length: number }>
min: LocaleValue<{ min: number }>
max: LocaleValue<{ max: number }>
matches: LocaleValue<{ regex: RegExp }>
email: LocaleValue<{ regex: RegExp }>
url: LocaleValue<{ regex: RegExp }>
trim: LocaleValue
lowercase: LocaleValue
uppercase: LocaleValue
chineseMobilePhoneNumber: LocaleValue
chineseIDCardNumber: LocaleValue
}

export interface NumberLocale {
min: LocaleValue<{ min: number }>
max: LocaleValue<{ max: number }>
lessThan: LocaleValue<{ less: number }>
moreThan: LocaleValue<{ more: number }>
positive: LocaleValue<{ more: number }>
negative: LocaleValue<{ less: number }>
integer: LocaleValue
}
export interface NumberLocale {
min: LocaleValue<{ min: number }>
max: LocaleValue<{ max: number }>
lessThan: LocaleValue<{ less: number }>
moreThan: LocaleValue<{ more: number }>
positive: LocaleValue<{ more: number }>
negative: LocaleValue<{ less: number }>
integer: LocaleValue
}

export interface DateLocale {
min: LocaleValue<{ min: Date | string }>
max: LocaleValue<{ max: Date | string }>
}
export interface DateLocale {
min: LocaleValue<{ min: Date | string }>
max: LocaleValue<{ max: Date | string }>
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface BooleanLocale {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface BooleanLocale {}

export interface ObjectLocale {
noUnknown: LocaleValue<{ unknown: string }>
}
export interface ObjectLocale {
noUnknown: LocaleValue<{ unknown: string }>
}

export interface ArrayLocale {
min: LocaleValue<{ min: number }>
max: LocaleValue<{ max: number }>
}
export interface ArrayLocale {
min: LocaleValue<{ min: number }>
max: LocaleValue<{ max: number }>
}

export interface Locale {
mixed: MixedLocale
string: StringLocale
number: NumberLocale
date: DateLocale
boolean: BooleanLocale
object: ObjectLocale
array: ArrayLocale
}
export interface Locale {
mixed: MixedLocale
string: StringLocale
number: NumberLocale
date: DateLocale
boolean: BooleanLocale
object: ObjectLocale
array: ArrayLocale
}
27 changes: 12 additions & 15 deletions src/validator/yupTypes/ValidationError.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
declare module 'yup/es' {
export class ValidationError {
value: any
import { LocaleValue, LocaleValueFnParams } from './Locale'

path: string
export declare class ValidationError {
value: any

type: string
path: string

errors: string[]
type: string

inner: any[]
errors: string[]

message: string
inner: any[]

stack?: any
message: string

static isError<T>(err: T): boolean
stack?: any

static formatError(
message: LocaleValue,
params: LocaleValueFnParams,
): string
}
static isError<T>(err: T): boolean

static formatError(message: LocaleValue, params: LocaleValueFnParams): string
}
68 changes: 37 additions & 31 deletions src/validator/yupTypes/addMethod.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
declare module 'yup/es' {
export function addMethod<
TSchemaType extends
| typeof mixed
| typeof string
| typeof number
| typeof boolean
| typeof date
| typeof array
| typeof object,
TSchema extends TSchemaType extends typeof mixed
? MixedSchema
: TSchemaType extends typeof string
? StringSchema
: TSchemaType extends typeof number
? NumberSchema
: TSchemaType extends typeof boolean
? BooleanSchema
: TSchemaType extends typeof date
? DateSchema
: TSchemaType extends typeof array
? ArraySchema
: TSchemaType extends typeof object
? ObjectSchema
: MixedSchema
>(
schemaType: TSchemaType,
name: string,
fn: (this: TSchema, ...args: any[]) => TSchema,
): void
}
import { array, ArraySchema } from './array'
import { boolean, BooleanSchema } from './boolean'
import { date, DateSchema } from './date'
import { mixed, MixedSchema } from './mixed'
import { number, NumberSchema } from './number'
import { object, ObjectSchema } from './object'
import { string, StringSchema } from './string'

export declare function addMethod<
TSchemaType extends
| typeof mixed
| typeof string
| typeof number
| typeof boolean
| typeof date
| typeof array
| typeof object,
TSchema extends TSchemaType extends typeof mixed
? MixedSchema
: TSchemaType extends typeof string
? StringSchema
: TSchemaType extends typeof number
? NumberSchema
: TSchemaType extends typeof boolean
? BooleanSchema
: TSchemaType extends typeof date
? DateSchema
: TSchemaType extends typeof array
? ArraySchema
: TSchemaType extends typeof object
? ObjectSchema
: MixedSchema,
>(
schemaType: TSchemaType,
name: string,
fn: (this: TSchema, ...args: any[]) => TSchema,
): void
22 changes: 12 additions & 10 deletions src/validator/yupTypes/array.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
declare module 'yup/es' {
export interface ArraySchema<T extends any = any> extends MixedSchema<T[]> {
of(type: GetSchema<T>): this
import { ArrayLocale, MixedLocale } from './Locale'
import { GetSchema, MixedSchema } from './mixed'
import { Ref } from './ref'

required(message?: MixedLocale['required']): this
export interface ArraySchema<T extends any = any> extends MixedSchema<T[]> {
of(type: GetSchema<T>): this

min(limit: number | Ref, message?: ArrayLocale['min']): this
required(message?: MixedLocale['required']): this

max(limit: number | Ref, message?: ArrayLocale['max']): this
min(limit: number | Ref, message?: ArrayLocale['min']): this

ensure(): this
max(limit: number | Ref, message?: ArrayLocale['max']): this

compact(rejector: (value: T) => boolean): this
}
ensure(): this

export function array<T extends any = any>(): ArraySchema<T>
compact(rejector: (value: T) => boolean): this
}

export declare function array<T extends any = any>(): ArraySchema<T>
10 changes: 5 additions & 5 deletions src/validator/yupTypes/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'yup/es' {
export interface BooleanSchema<T extends boolean = boolean>
extends MixedSchema<T> {}
import { MixedSchema } from './mixed'

export function boolean<T extends boolean = boolean>(): BooleanSchema<T>
}
export interface BooleanSchema<T extends boolean = boolean>
extends MixedSchema<T> {}

export declare function boolean<T extends boolean = boolean>(): BooleanSchema<T>
14 changes: 8 additions & 6 deletions src/validator/yupTypes/date.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
declare module 'yup/es' {
export interface DateSchema<T extends Date = Date> extends MixedSchema<T> {
min(limit: Date | Ref, message?: DateLocale['min']): this
import { DateLocale } from './Locale'
import { MixedSchema } from './mixed'
import { Ref } from './ref'

max(limit: Date | Ref, message?: DateLocale['max']): this
}
export interface DateSchema<T extends Date = Date> extends MixedSchema<T> {
min(limit: Date | Ref, message?: DateLocale['min']): this

export function date<T extends Date = Date>(): DateSchema<T>
max(limit: Date | Ref, message?: DateLocale['max']): this
}

export declare function date<T extends Date = Date>(): DateSchema<T>
6 changes: 3 additions & 3 deletions src/validator/yupTypes/getLocale.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare module 'yup/es' {
export function getLocale(): Locale
}
import { Locale } from './Locale'

export declare function getLocale(): Locale
34 changes: 17 additions & 17 deletions src/validator/yupTypes/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// @index('./*.ts', f => `import '${f.path}'`)
import './addMethod'
import './array'
import './boolean'
import './date'
import './getLocale'
import './lazy'
import './Locale'
import './mixed'
import './number'
import './object'
import './printValue'
import './reach'
import './ref'
import './setLocale'
import './string'
import './ValidationError'
// @index('./*.ts', f => `export * from '${f.path}'`)
export * from './addMethod'
export * from './array'
export * from './boolean'
export * from './date'
export * from './getLocale'
export * from './lazy'
export * from './Locale'
export * from './mixed'
export * from './number'
export * from './object'
export * from './printValue'
export * from './reach'
export * from './ref'
export * from './setLocale'
export * from './string'
export * from './ValidationError'
// @endindex
8 changes: 4 additions & 4 deletions src/validator/yupTypes/lazy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'yup/es' {
export interface Lazy extends MixedSchema {}
import { MixedSchema } from './mixed'

export function lazy<X extends MixedSchema>(fn: (value: any) => X): Lazy
}
export interface Lazy extends MixedSchema {}

export declare function lazy<X extends MixedSchema>(fn: (value: any) => X): Lazy
Loading

0 comments on commit 6aa8c6e

Please sign in to comment.