Skip to content

Commit

Permalink
feat(vae): 支持抛出错误作为 message
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 16, 2024
1 parent 8e13b0a commit 0c94fb4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 44 deletions.
103 changes: 59 additions & 44 deletions src/vae/VaeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type VaeSchemaOptions<T, S> = {
export type VaeSchemaPath = Array<string | number>

export type VaeSchemaCheckPayload<T> = {
fn: ((value: T) => boolean) | VaeSchema
fn: ((value: T) => boolean | void) | VaeSchema
message: VaeLocaleMessage
messageParams?: Record<string, any>
path?: VaeSchemaPath
Expand Down Expand Up @@ -224,7 +224,7 @@ export abstract class VaeSchema<
}

custom(
fn: (value: T) => boolean,
fn: (value: T) => boolean | void,
messageOrOptions?:
| VaeLocaleMessage
| {
Expand Down Expand Up @@ -405,7 +405,8 @@ export abstract class VaeSchema<
for (let i = 0; i < processors.length; i++) {
const processor = processors[i]
if (typeof processor === 'object') {
const { fn, message, messageParams, path = [], tag } = processor
// eslint-disable-next-line prefer-const
let { fn, message, messageParams, path = [], tag } = processor
const fullPath = [...curPath, ...path]
if (fn instanceof VaeSchema) {
const pathData = path.length ? get(data, path) : data
Expand Down Expand Up @@ -448,50 +449,64 @@ export abstract class VaeSchema<
}
}
}
} else if (
!fn(
// @ts-expect-error
data,
)
) {
if (options.cast) {
data = dataIsNil
? data
: schema._options.type === 'string'
? String(data)
: schema._options.type === 'number'
? Number(data)
: schema._options.type === 'boolean'
? Boolean(data)
: schema._options.type === 'date'
? new Date(data as any)
: schema._options.type === 'array'
? toArray(data)
: schema._options.type === 'object'
? toPlainObject(data)
: null
} else {
ctx.addIssue({
path: fullPath,
message:
typeof message === 'function'
? message({
path: fullPath,
params: messageParams || {},
value: data,
label: schema._options.label,
})
: message,
})
if (options.abortEarly) {
return {
success: false,
issues: ctx.issues,
message: ctx.issues[0].message,
} else {
let fnRes: boolean | void
let fnErr: any

try {
fnRes = fn(
// @ts-expect-error
data,
)
} catch (err) {
fnErr = err
}

if (fnErr != null) {
fnRes = false
message = fnErr instanceof Error ? fnErr.message : String(fnErr)
}

if (fnRes === false) {
if (options.cast) {
data = dataIsNil
? data
: schema._options.type === 'string'
? String(data)
: schema._options.type === 'number'
? Number(data)
: schema._options.type === 'boolean'
? Boolean(data)
: schema._options.type === 'date'
? new Date(data as any)
: schema._options.type === 'array'
? toArray(data)
: schema._options.type === 'object'
? toPlainObject(data)
: null
} else {
ctx.addIssue({
path: fullPath,
message:
typeof message === 'function'
? message({
path: fullPath,
params: messageParams || {},
value: data,
label: schema._options.label,
})
: message,
})
if (options.abortEarly) {
return {
success: false,
issues: ctx.issues,
message: ctx.issues[0].message,
}
}
}
break
}
break
}
} else {
// @ts-expect-error
Expand Down
39 changes: 39 additions & 0 deletions src/vae/__snapshots__/vae.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,45 @@ Object {
}
`;

exports[`vae 支持抛出错误作为 message 1`] = `
Object {
"issues": Array [
Object {
"message": "x err",
"path": Array [
"id",
],
},
],
"message": "x err",
"success": false,
}
`;

exports[`vae 支持抛出错误作为 message 2`] = `
Object {
"issues": Array [
Object {
"message": "y err",
"path": Array [
"id",
],
},
],
"message": "y err",
"success": false,
}
`;

exports[`vae 支持抛出错误作为 message 3`] = `
Object {
"data": Object {
"id": "z00003",
},
"success": true,
}
`;

exports[`vae 数组处理 1`] = `
Array [
"hello",
Expand Down
22 changes: 22 additions & 0 deletions src/vae/vae.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,26 @@ describe('vae', () => {
)
expect(schema.cast(['hello', 'echo hello', 'Hello'])).toMatchSnapshot()
})

test('支持抛出错误作为 message', () => {
const schema: VaeObjectSchema<{
id: string
}> = v.create(_ =>
_.object({
id: _.string($ =>
$.custom(v => {
if (v.startsWith('x')) {
throw 'x err'
}
if (v.startsWith('y')) {
throw new Error('y err')
}
}),
),
}),
)
expect(schema.parse({ id: 'x1' })).toMatchSnapshot()
expect(schema.parse({ id: 'y5555' })).toMatchSnapshot()
expect(schema.parse({ id: 'z00003' })).toMatchSnapshot()
})
})

0 comments on commit 0c94fb4

Please sign in to comment.