Skip to content

Commit

Permalink
feat(vae): 相同 tag 的定义会被覆盖
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 18, 2023
1 parent dd0648a commit 583e87b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/vae/VaeArraySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class VaeArraySchema<T extends any[] = any[]> extends VaeSchema<T> {
return this.check({
fn: v => v.length > 0,
message: message,
tag: 'nonempty',
})
}

Expand All @@ -47,6 +48,7 @@ export class VaeArraySchema<T extends any[] = any[]> extends VaeSchema<T> {
messageParams: {
min: value,
},
tag: 'min',
})
}

Expand All @@ -57,6 +59,7 @@ export class VaeArraySchema<T extends any[] = any[]> extends VaeSchema<T> {
messageParams: {
max: value,
},
tag: 'max',
})
}

Expand All @@ -67,6 +70,7 @@ export class VaeArraySchema<T extends any[] = any[]> extends VaeSchema<T> {
messageParams: {
length: value,
},
tag: 'length',
})
}
}
2 changes: 2 additions & 0 deletions src/vae/VaeDateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class VaeDateSchema<T extends Date = Date> extends VaeSchema<T> {
messageParams: {
min: minDate,
},
tag: 'min',
})
}

Expand All @@ -40,6 +41,7 @@ export class VaeDateSchema<T extends Date = Date> extends VaeSchema<T> {
messageParams: {
max: maxDate,
},
tag: 'max',
})
}
}
10 changes: 10 additions & 0 deletions src/vae/VaeNumberSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class VaeNumberSchema<T extends number = number> extends VaeSchema<T> {
messageParams: {
min: value,
},
tag: 'min',
})
}

Expand All @@ -30,6 +31,7 @@ export class VaeNumberSchema<T extends number = number> extends VaeSchema<T> {
messageParams: {
max: value,
},
tag: 'max',
})
}

Expand All @@ -43,6 +45,7 @@ export class VaeNumberSchema<T extends number = number> extends VaeSchema<T> {
messageParams: {
lessThan: value,
},
tag: 'lessThan',
})
}

Expand All @@ -56,41 +59,47 @@ export class VaeNumberSchema<T extends number = number> extends VaeSchema<T> {
messageParams: {
moreThan: value,
},
tag: 'moreThan',
})
}

integer(message: VaeLocaleMessage = VaeLocale.number.integer) {
return this.check({
fn: isInteger,
message: message,
tag: 'integer',
})
}

positive(message: VaeLocaleMessage = VaeLocale.number.positive) {
return this.check({
fn: v => v > 0,
message: message,
tag: 'positive',
})
}

nonpositive(message: VaeLocaleMessage = VaeLocale.number.nonpositive) {
return this.check({
fn: v => v <= 0,
message: message,
tag: 'nonpositive',
})
}

negative(message: VaeLocaleMessage = VaeLocale.number.negative) {
return this.check({
fn: v => v < 0,
message: message,
tag: 'negative',
})
}

nonnegative(message: VaeLocaleMessage = VaeLocale.number.nonnegative) {
return this.check({
fn: v => v >= 0,
message: message,
tag: 'nonnegative',
})
}

Expand All @@ -100,6 +109,7 @@ export class VaeNumberSchema<T extends number = number> extends VaeSchema<T> {
return this.check({
fn: v => v > 0 && isInteger(v),
message: message,
tag: 'positiveInteger',
})
}

Expand Down
12 changes: 6 additions & 6 deletions src/vae/VaeObjectSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PartialBy, RequiredBy } from '../types'
import { difference, intersection, isPlainObject } from '../utils'
import { difference, intersection, isPlainObject, startsWith } from '../utils'
import { VaeLocale, VaeLocaleMessage } from './VaeLocale'
import { VaeSchema, VaeSchemaOf } from './VaeSchema'

Expand Down Expand Up @@ -36,15 +36,15 @@ export class VaeObjectSchema<
fn: shape[key] as any,
path: [key],
message: '',
tag: 'field',
tag: `field_${key}`,
})
})
return this
}

pickFields<K extends keyof T>(keys: K[]): VaeObjectSchema<Pick<T, K>> {
this._options.processors = this._options.processors.filter(item =>
typeof item === 'object' && item.tag === 'field'
typeof item === 'object' && startsWith(item.tag, 'field_')
? (keys as any).includes(item.path![0])
: true,
)
Expand All @@ -57,7 +57,7 @@ export class VaeObjectSchema<

omitFields<K extends keyof T>(keys: K[]): VaeObjectSchema<Omit<T, K>> {
this._options.processors = this._options.processors.filter(item =>
typeof item === 'object' && item.tag === 'field'
typeof item === 'object' && startsWith(item.tag, 'field_')
? !(keys as any).includes(item.path![0])
: true,
)
Expand All @@ -71,7 +71,7 @@ export class VaeObjectSchema<
this._options.processors.forEach(item => {
if (
typeof item === 'object' &&
item.tag === 'field' &&
startsWith(item.tag, 'field_') &&
(keys ? (keys as any).includes(item.path![0]) : true)
) {
;(item.fn as VaeSchema).optional()
Expand All @@ -88,7 +88,7 @@ export class VaeObjectSchema<
this._options.processors.forEach(item => {
if (
typeof item === 'object' &&
item.tag === 'field' &&
startsWith(item.tag, 'field_') &&
(keys ? (keys as any).includes(item.path![0]) : true)
) {
;(item.fn as VaeSchema).required()
Expand Down
17 changes: 16 additions & 1 deletion src/vae/VaeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
assign,
castArray,
cloneDeepFast,
findIndex,
get,
includes,
isArray,
Expand Down Expand Up @@ -146,7 +147,17 @@ export abstract class VaeSchema<T extends any = any> {
}

check(payload: VaeSchemaCheckPayload<T>) {
this._options.processors.push(payload)
const index = payload.tag
? findIndex(
this._options.processors,
item => typeof item === 'object' && item.tag === payload.tag,
)
: -1
if (index !== -1) {
this._options.processors[index] = payload
} else {
this._options.processors.push(payload)
}
return this
}

Expand Down Expand Up @@ -188,6 +199,7 @@ export abstract class VaeSchema<T extends any = any> {
messageParams: {
enum: enumValues,
},
tag: 'enum',
})
}

Expand All @@ -198,6 +210,7 @@ export abstract class VaeSchema<T extends any = any> {
| {
message?: VaeLocaleMessage
path?: DotPath<T>
tag?: string
},
) {
if (!messageOrOptions || typeof messageOrOptions !== 'object') {
Expand All @@ -207,10 +220,12 @@ export abstract class VaeSchema<T extends any = any> {
}
const message = messageOrOptions.message || VaeLocale.base.custom
const path = messageOrOptions.path?.split('.')
const tag = messageOrOptions.tag && `custom_${messageOrOptions.tag}`
return this.check({
fn,
message,
path,
tag,
})
}

Expand Down
11 changes: 11 additions & 0 deletions src/vae/VaeStringSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
min: value,
},
tag: 'min',
})
}

Expand All @@ -41,6 +42,7 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
max: value,
},
tag: 'max',
})
}

Expand All @@ -51,20 +53,23 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
length: value,
},
tag: 'length',
})
}

email(message: VaeLocaleMessage = VaeLocale.string.email) {
return this.check({
fn: isEmail,
message: message,
tag: 'email',
})
}

url(message: VaeLocaleMessage = VaeLocale.string.url) {
return this.check({
fn: isUrl,
message: message,
tag: 'url',
})
}

Expand All @@ -78,6 +83,7 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
regex: value,
},
tag: 'regex',
})
}

Expand All @@ -91,6 +97,7 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
includes: value,
},
tag: 'includes',
})
}

Expand All @@ -104,6 +111,7 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
startsWith: value,
},
tag: 'startsWith',
})
}

Expand All @@ -117,20 +125,23 @@ export class VaeStringSchema<T extends string = string> extends VaeSchema<T> {
messageParams: {
endsWith: value,
},
tag: 'endsWith',
})
}

phoneNumber(message: VaeLocaleMessage = VaeLocale.string.phoneNumber) {
return this.check({
fn: isPossibleChineseMobilePhoneNumber,
message: message,
tag: 'phoneNumber',
})
}

idCardNumber(message: VaeLocaleMessage = VaeLocale.string.idCardNumber) {
return this.check({
fn: isChineseIDCardNumber,
message: message,
tag: 'idCardNumber',
})
}

Expand Down
38 changes: 38 additions & 0 deletions src/vae/__snapshots__/vae.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1283,3 +1283,41 @@ Object {
"success": true,
}
`;

exports[`vae 可覆盖定义 1`] = `
Object {
"issues": Array [
Object {
"message": ".应至少包含3位字符",
"path": Array [],
},
],
"success": false,
}
`;

exports[`vae 可覆盖定义 2`] = `
Object {
"data": "12",
"success": true,
}
`;

exports[`vae 可覆盖定义 3`] = `
Object {
"issues": Array [
Object {
"message": ".应小于或等于3",
"path": Array [],
},
],
"success": false,
}
`;

exports[`vae 可覆盖定义 4`] = `
Object {
"data": 4,
"success": true,
}
`;
8 changes: 8 additions & 0 deletions src/vae/vae.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,12 @@ describe('vae', () => {
),
).toMatchSnapshot()
})

test('可覆盖定义', () => {
expect(v.string().min(3).parse('12')).toMatchSnapshot()
expect(v.string().min(3).min(2).parse('12')).toMatchSnapshot()

expect(v.number().max(3).parse(4)).toMatchSnapshot()
expect(v.number().max(3).max(8).parse(4)).toMatchSnapshot()
})
})

0 comments on commit 583e87b

Please sign in to comment.