Skip to content

Commit

Permalink
feat(validator): 为 ref 添加 map 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 29, 2021
1 parent 4f30d4a commit 4bc0a78
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 18 deletions.
14 changes: 14 additions & 0 deletions src/validator/__snapshots__/yup.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ Object {
}
`;

exports[`yup ref 支持 map 1`] = `
Object {
"data": 2,
"error": [ValidationError: 此项必须大于或等于3],
}
`;

exports[`yup ref 支持 map 2`] = `
Object {
"data": 2,
"error": [ValidationError: 此项必须大于或等于3],
}
`;

exports[`yup test 第一个参数可传函数或正则 1`] = `
Object {
"data": Object {
Expand Down
12 changes: 12 additions & 0 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,16 @@ describe('yup', () => {
expect(rule.validatePlusSync(100)).toMatchSnapshot()
expect(rule.validatePlusSync(-100.3)).toMatchSnapshot()
})

test('ref 支持 map', () => {
const rule = yup.number().min(yup.ref('.', value => value + 1))
expect(rule.validatePlusSync(2)).toMatchSnapshot()

const rule2 = yup.number().min(
yup.ref('.', {
map: value => value + 1,
}),
)
expect(rule2.validatePlusSync(2)).toMatchSnapshot()
})
})
6 changes: 6 additions & 0 deletions src/validator/yupSource/Reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const prefixes = {

export default class Reference {
constructor(key, options = {}) {
if (typeof options === 'function') {
options = {
map: options,
}
}

if (typeof key !== 'string')
throw new TypeError('ref must be a string, got: ' + key)

Expand Down
4 changes: 2 additions & 2 deletions src/validator/yupTypes/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export interface ArraySchema<T extends any = any> extends MixedSchema<T[]> {

required(message?: MixedLocale['required']): this

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

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

ensure(): this

Expand Down
4 changes: 2 additions & 2 deletions src/validator/yupTypes/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { MixedSchema } from './mixed'
import { Ref } from './ref'

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

max(limit: Date | Ref, message?: DateLocale['max']): this
max(limit: Date | Ref<Date>, message?: DateLocale['max']): this
}

export declare function date<T extends Date = Date>(): DateSchema<T>
8 changes: 4 additions & 4 deletions src/validator/yupTypes/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { Ref } from './ref'

export interface NumberSchema<T extends number = number>
extends MixedSchema<T> {
min(limit: number | Ref, message?: NumberLocale['min']): this
min(limit: number | Ref<number>, message?: NumberLocale['min']): this

max(limit: number | Ref, message?: NumberLocale['max']): this
max(limit: number | Ref<number>, message?: NumberLocale['max']): this

lessThan(max: number | Ref, message?: NumberLocale['lessThan']): this
lessThan(max: number | Ref<number>, message?: NumberLocale['lessThan']): this

moreThan(min: number | Ref, message?: NumberLocale['moreThan']): this
moreThan(min: number | Ref<number>, message?: NumberLocale['moreThan']): this

positive(message?: NumberLocale['positive']): this

Expand Down
20 changes: 13 additions & 7 deletions src/validator/yupTypes/ref.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
export interface Ref {
readonly __isYupRef: true
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-unused-vars
export interface Ref<T> {}

export interface RefOptions {
contextPrefix?: string
export interface RefOptions<T> {
map?: (value: any) => T
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export declare function ref<T>(path: string, options?: RefOptions): Ref
export declare function ref<X, T extends X>(
path: string,
options?: RefOptions<T>,
): Ref<X>

export declare function ref<X, T extends X>(
path: string,
map?: NonNullable<RefOptions<T>['map']>,
): Ref<X>
6 changes: 3 additions & 3 deletions src/validator/yupTypes/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export interface StringSchema<T extends string = string>
extends MixedSchema<T> {
required(message?: MixedLocale['required']): this

length(limit: number | Ref, message?: StringLocale['length']): this
length(limit: number | Ref<number>, message?: StringLocale['length']): this

min(limit: number | Ref, message?: StringLocale['min']): this
min(limit: number | Ref<number>, message?: StringLocale['min']): this

max(limit: number | Ref, message?: StringLocale['max']): this
max(limit: number | Ref<number>, message?: StringLocale['max']): this

matches(regex: RegExp, message?: StringLocale['matches']): this

Expand Down

0 comments on commit 4bc0a78

Please sign in to comment.