Skip to content

Commit

Permalink
fix(validation): use withAsync for async rule (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking authored Mar 13, 2024
1 parent 5971393 commit c309efe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/validation/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { _required } from './validators'

export interface RuleOptions {
optional?: boolean
async?: boolean
message(params: MessageProps): string
validation(value: unknown): boolean | Promise<boolean>
}
Expand All @@ -18,14 +19,14 @@ export function createRule(
): ValidationRuleWithParams {
const lang = useLang()

function validation(value: unknown) {
return options.optional && !_required(value)
? true
: options.validation(value)
}

return helpers.withMessage(
(params) => {
return options.message({ ...params, lang })
},
(value) => {
return options.optional && !_required(value)
? true
: options.validation(value)
}
(params) => options.message({ ...params, lang }),
options.async ? helpers.withAsync(validation) : validation
)
}
1 change: 1 addition & 0 deletions lib/validation/rules/requiredIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function requiredIf(
msg?: string
) {
return createRule({
async: true,
message: ({ lang }) => msg ?? message[lang],
validation: (value) => baseRequiredIf(value, condition)
})
Expand Down

0 comments on commit c309efe

Please sign in to comment.