Skip to content

Commit

Permalink
chore: improve validator context (#2439)
Browse files Browse the repository at this point in the history
* chore: improve validator context

* chore: improve typings
  • Loading branch information
janryWang authored Nov 16, 2021
1 parent 8e4a6a9 commit f3f2989
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const validateToFeedbacks = async (
const results = await validate(field.value, field.validator, {
triggerType,
validateFirst: field.props.validateFirst || field.form.props.validateFirst,
context: field,
context: { field, form: field.form },
})

batch(() => {
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Validator, ValidatorTriggerType } from '@formily/validator'
import {
IValidatorRules,
Validator,
ValidatorTriggerType,
} from '@formily/validator'
import { FormPath } from '@formily/shared'
import {
Form,
Expand Down Expand Up @@ -293,7 +297,13 @@ export type FieldPatternTypes =
| 'readPretty'
| ({} & string)

export type FieldValidator = Validator
export type FieldValidatorContext = IValidatorRules & {
field?: Field
form?: Form
value?: any
}

export type FieldValidator = Validator<FieldValidatorContext>

export type FieldDataSource = {
label?: any
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const parseIValidatorRules = (
const getContext = (context: any, value: any) => {
return {
...rules,
...context,
value,
context,
}
}
const createValidate =
Expand All @@ -70,7 +70,7 @@ export const parseIValidatorRules = (
type: 'error',
message,
},
{ ...scope, ...context_ }
Object.assign(context_, scope)
)?.message
}
)
Expand Down

0 comments on commit f3f2989

Please sign in to comment.