aurelia / aurelia/validation

Suggestion: ValidationRules.satisfies(...) should accept 'config' parameter as a function.

Open
#463 3 comments 0 reactions 0 assignees View on GitHub
enhancement question
Dominant language
TypeScript
Stars
129
Forks
125
PR merge metrics
No merged PRs in 30d

Description

We have a valid use case for the ValidationRules.satisfies() to take 'config' parameter as a function to be evaluated when validation get triggered.

**Use case:**
In our application "Business Registration Number" is an input filed which changes it's accepted character length depend on the selected country.
e.g
Australia -> accepted length: 11
China -> accepted length: 15

We validate this control on blur and display a message if the expected length is not met.

e.g
Australia :
"Business registration number must be 11 characters in length"
China :
"Business registration number must be 15 characters in length"

**Implementation:**
Our current implementation is look like as follows:

```
ValidationRules
.ensure((m: Registration) => m.businessNumber)
.displayName(() => this._i18N.tr(this.businessNumberPlaceholderKey))
.required().withMessageKey("validationMessages_FieldIsRequired_Client")
.then()
.satisfies(() => this.businessNumber.length === this.businessNumberLimit,
{exact: this.businessNumberLimit})
.withMessageKey("validationMessages_FieldHasInvalidLengthExact_Client")
```

`validationMessages_FieldHasInvalidLengthExact_Client` has been parametrized as follows:
`${$displayName} must be ${$config.exact} characters in length.`

**Current behaviour:**
we noticed that the `{ exact: this.businessNumberLimit}` does not provide the value as expected. It always prints the default value.
The reason could be, it couldn't read the latest value stored on `this.businessNumberLimit`.

**Expectation:**
We are expecting `satisfies(...)` function to accept the configuration values as a function which could be evaluated and return an object at the time of validation.

e.g:
```
.satisfies(condition: (value: TValue, object: TObject) => boolean | Promise,
config?: () => object): FluentRuleCustomizer;

```

So then we could set the configure as follows:
```
.satisfies(() => this.businessNumber.length === this.businessNumberLimit,
() => {exact: this.businessNumberLimit})
.withMessageKey("validationMessages_FieldHasInvalidLengthExact_Client")

```

Feel free to shoot at me if require further clarifications.

Contributor guide

Open the contributing guide

Research direction

No file or test is named in the issue. Start by tracing ValidationRules.satisfies and how its config reaches validation messages; the work is done when a function-valued config is evaluated at validation time and the message receives the current exact value.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.