Skip to content

Commit

Permalink
feat(vae): custom 新增默认信息
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 18, 2023
1 parent 2f443af commit 0ecfcfc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/vae/VaeLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type VaeLocaleMessage =
| ((payload: VaeLocaleMessagePayload) => string)

export type VaeLocaleShape = {
base: Record<'required' | 'enum', VaeLocaleMessage>
base: Record<'required' | 'enum' | 'custom', VaeLocaleMessage>
string: Record<
| 'type'
| 'min'
Expand Down Expand Up @@ -63,6 +63,7 @@ export class VaeLocaleBuilder {
`${options.getLabel(
payload,
)}应是下列值之一:${payload.params.enum.join(',')}`,
custom: payload => `${options.getLabel(payload)}应满足自定义规则`,
},

string: {
Expand Down
22 changes: 18 additions & 4 deletions src/vae/VaeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,25 @@ export abstract class VaeSchema<T extends any = any> {

custom(
fn: (value: T) => boolean,
message: VaeLocaleMessage,
dotPath?: DotPath<T>,
messageOrOptions?:
| VaeLocaleMessage
| {
message?: VaeLocaleMessage
path?: DotPath<T>
},
) {
const path = dotPath?.split('.')
return this.check({ fn, message, path })
if (!messageOrOptions || typeof messageOrOptions !== 'object') {
messageOrOptions = {
message: messageOrOptions,
}
}
const message = messageOrOptions.message || VaeLocale.base.custom
const path = messageOrOptions.path?.split('.')
return this.check({
fn,
message,
path,
})
}

clone() {
Expand Down
9 changes: 4 additions & 5 deletions src/vae/vae.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,10 @@ describe('vae', () => {
password: v.string().required(),
password2: v.string().required(),
})
.custom(
({ password, password2 }) => password === password2,
'密码2应等于密码1',
'password2',
)
.custom(({ password, password2 }) => password === password2, {
message: '密码2应等于密码1',
path: 'password2',
})

expect(
schema2.parse({
Expand Down

0 comments on commit 0ecfcfc

Please sign in to comment.