feat: add x-validator-merge prop && registerMergeRules api #4208
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before submitting a pull request, please make sure the following is done...
fix #4204
master
orformily_next
.npm test
).npm run lint
) - we've done our best to make sure these rules match our internal linting guidelines.Please do not delete the above content
What have you changed?
description
This PR addresses the following issues and provides the following features:
1 Added the
x-validator-merge
attribute to tags for merging with global validation. Custom global validation is sometimes needed, but due to Formity’s previous design, global validation, likerequired
, couldn’t have custom messages. Even if attributes are set inx-validator
, they are added as additional rules rather than replacing the existing ones. Thex-validator-merge
attribute allows merging global and local validations.2 Introduced the
registerMergeRules
API, which works similarly toregisterValidateRules
, allowing functions to override default global validation rules in Formity.3 Distinguished the state of rules into four types, with the first three being mergeable and the last one overriding the previous ones (refer to
packages/validator/src/registry.ts
):required
,maximum
,minimum
x-validator-merge
orregisterMergeRules
x-validator
, which do not participate in merging operations, as determined by Ant Design's properties4 Add the
schema
variable to the third function in the validation function. This helps in accessing schema information within custom validation functions. Previously, thecontext
parameter was transformed by theSchemaStateMap
variable inpackages/json-schema/src/shared.ts
, making it impossible for developers to retrieve information such asrequired
,maximum
, orminimum
registerMergeRules(api)
example
x-validator-merge(prop)
example