Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for server side validation example and implementation enhancements #459

Open
bmarotta opened this issue Jul 11, 2017 · 0 comments

Comments

@bmarotta
Copy link

bmarotta commented Jul 11, 2017

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:
ValidationRules.customRule('titleCheck',
            (value, object) => this.service.checkTitle(value, object),
            () => this.service.getLastErrorMessage()
        );

or using the config object

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.

  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

export class MyViewModel{
    title: string;
    lastResult: CheckTitleResult = new CheckTitleResult();
}

class CheckTitleResult {
    success: boolean;
    message: string;
    titleSuggestion: string;
}

Service call

    checkTitle(title: string, object: MyViewModel): Promise<boolean> {
        return new Promise<boolean>((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

        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

(...)
  "service": {
    "checkTitleError": "An unexpected error happened while validating the title, please try again later. ERROR: {{error}}"
  },
(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants