Suggestion for server side validation example and implementation enhancements
- Dominant language
- TypeScript
- Stars
- 129
- Forks
- 125
- PR merge metrics
- No merged PRs in 30d
Description
**I'm submitting a feature request**
* **Library Version:**
1.1.1
**Please tell us about your environment:**
* **Operating System:**
Windows 10
* **Node Version:**
6.9.4
* **NPM Version:**
3.10.10
* **JSPM OR Webpack AND Version**
N/A
* **Browser:**
all
* **Language:**
TypeScript 2.2.2
**Current behavior:**
There is no documentation for the server side validation
**Expected/desired behavior:**
I would like to suggest the snippet down below for the remote validation example. Meanwhile while implementing it I found some caveats in the current library version that should be tackled somehow.
* Custom rule _config_ and _message_ properties are evaluated while creating the custom rule. The only way I found to pass the error result from the server to the validator was to manipulate the source viewmodel itself which is not the most elegant solution. Better would be to have the ability to specify the config or error message as functions while defining the custom rule. Examples:
```javascript
ValidationRules.customRule('titleCheck',
(value, object) => this.service.checkTitle(value, object),
() => this.service.getLastErrorMessage()
);
```
or using the config object
```javascript
ValidationRules.customRule('titleCheck',
(value, object) => this.service.checkTitle(value, object),
"\${$config.error}",
() => { error: () => this.service.getLastErrorMessage() });
```
* The standard validator don't implement the reject for the promises. In case of an uncaught exception the field is not validated at all. I believe that if the Promise is rejected, the validation should fail with an "unexpected" error message
* The i18n example is outdated. The function is no longer called parseMessage but only parse. You should add also a fallback for the case where the message key doesn't exist. It happens in the case where a custom rule is added.
```javascript
ValidationMessageProvider.prototype.standardGetMessage = ValidationMessageProvider.prototype.getMessage;
ValidationMessageProvider.prototype.getMessage = function (key) {
if (i18n.i18next.exists(key)) {
const translation = i18n.tr(key);
return this.parser.parse(translation);
} else {
return this.standardGetMessage(key);
}
};
```
* **Documentation Suggestion**
***View Model and Service Result Definition***
```javascript
export class MyViewModel{
title: string;
lastResult: CheckTitleResult = new CheckTitleResult();
}
class CheckTitleResult {
success: boolean;
message: string;
titleSuggestion: string;
}
```
***Service call***
```javascript
checkTitle(title: string, object: MyViewModel): Promise {
return new Promise((resolve, reject) => {
this.httpClient.get("service/checkTitle?t=" + title).then((response) => {
try {
if (response.isSuccess) {
object.lastResult = JSON.parse(response.response);
if (object.lastResult.titleSuggestion) {
object.title = object.lastResult.titleSuggestion;
}
resolve(object.lastResult.success);
} else {
object.lastResult = { success: false, message: this.i18n.tr("service.checkTitleError", { error: response.statusText }), titleSuggestion: "" };
resolve(false);
}
}
catch (exception) {
object.lastResult = { success: false, message: this.i18n.tr("service.checkTitleError", { error: exception }), titleSuggestion: "" };
resolve(false);
}
}).catch((reason) => {
object.lastResult = { success: false, message: this.i18n.tr("service.checkTitleError", { error: reason }), titleSuggestion: "" };
resolve(false);
})
})
}
```
***Rule definition***
```javascript
ValidationRules.customRule('titleCheck',
(value, object) => this.service.checkTitle(value, object),
"\${$object.lastResult.message}"
);
ValidationRules.
ensure('title').displayName('Title')
.required()
.then()
.satisfiesRule('titleCheck')
.on(this.myViewModel);
```
***Translation file***
```javascript
(...)
"service": {
"checkTitleError": "An unexpected error happened while validating the title, please try again later. ERROR: {{error}}"
},
(...)
```
Contributor guide
Assessment
This issue has not been assessed yet.