question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule is called twice, calling backend service, so undesirable

See original GitHub issue

Hi,

Please see code of my constructor below. What I (obviously) want to achieve is that a backend service is only called when the email address is “valid”. This works. When the email address is valid though, I see that the backend service is called twice. Am I doing something wrong? Or is this a bug?

BTW, As you can see I implemented my own regex(this._emailRegex) to test for valid email address. Can I also call the standard validation rule? Why do I need to test this again as I also have the email() already in the chain.

Thanks!

Sander

    constructor(private userService: UserService, private controllerFactory: ValidationControllerFactory) {
        this.controller = controllerFactory.createForCurrentScope();
        this.controller.validateTrigger = validateTrigger.change;
        validationMessages['passwordmatch'] = `Passwords don't match`;
        validationMessages['alreadyexists'] = `Email already exists!`;

        ValidationRules.customRule('passwordmatch',
            (value, obj) => {
                return obj.password === obj.password2;
            }, validationMessages['passwordmatch']);

        ValidationRules.customRule('alreadyexists',
            (value, obj) => {
                if (this._emailRegex.test(obj.email)) {
                    return this.userService.exists(obj.email);
                } else {
                    return true;
                }
            }, validationMessages['alreadyexists']);

        ValidationRules
            .ensure('fullname')
            .required()
            .ensure('email')
            .required()
            .email()
            .satisfiesRule('alreadyexists')
            .ensure('password')
            .required()
            .ensure('password2')
            .required()
            .satisfiesRule('passwordmatch')
            .on(this._user);

        setTimeout(() => this.controller.validate(), 500);
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jdanyowcommented, Sep 25, 2016

You can use the Validator to evaluate rules without causing side-effects in the UI. The .tag(name) fluent API feature along with ValidationRules.taggedRules enables referencing and evaluating specific rules in the set.

import {Validator, ValidationControllerFactory, ValidationRules} from 'aurelia-validation';

@inject(ValidationControllerFactory, Validator)
export class Foo {
  user = { email: '' };

  constructor(controllerFactory, validator) {
    const allRules = ValidationRules
      .ensure('email')
        .required().tag('xyz')
        .email().tag('xyz')
        .then()
        .satisfiesRule('alreadyexists')
      .on(this.user)
      .rules;

    const xyzRules = ValidationRules.taggedRules(allRules, 'xyz');

    validator.validateObject(this.user, allRules).then(errors => ....);
    validator.validateProperty(this.user, allRules).then(errors => ....);

    validator.validateObject(this.user, xyzRules).then(errors => ....);
    validator.validateProperty(this.user, xyzRules).then(errors => ....);
  }

Does this answer the question?

0reactions
sspillemancommented, Sep 26, 2016

Sorry for misusing this closed issue thread to ask questions. Now I have this error:

Inner Error:
Message: Unable to parse accessor function:
function (a) {
            "use strict";

            return a.data.fullname;
        }

thrown by:

getAccessorExpression — validation-parser.js:45

I tried THIS (This one doesn’t throw the exception, but it just doesn’t work):

ValidationRules
            .ensure("data.fullname"})
            .displayName('Full Name')
            .required()

THIS, because I found this this issue (Exception)

ValidationRules
            .ensure(function (a) {"use strict"; return a.data.fullname; })
            .displayName('Full Name')
            .required()

THIS (Exception)

ValidationRules
            .ensure(function (a) { return a.data.fullname; })
            .displayName('Full Name')
            .required()

AND THIS (Exception)

ValidationRules
            .ensure(a => a.data.fullname)
            .displayName('Full Name')
            .required()

None of these work. Bug? Or am I doing something wrong? (again 😉

Sander

BTW (see below), THAT also doesn’t work(no exceptions, but the console.log(‘Im here!!’, obj)) is never called 😉

ValidationRules.customRule('passwordmatch',
    (value, obj) => {
        return obj.password === obj.password2;
    }, validationMessages['passwordmatch']);

ValidationRules.customRule('alreadyexists',
    (value, obj) => {
        if (this._emailRegex.test(obj.email)) {
            return this.userService.exists(obj.email);
        } else {
            return true;
        }
    }, validationMessages['alreadyexists']);

ValidationRules
    .ensureObject()
    .satisfies((obj: IRegisterUser) => {
        console.log('Im here!!', obj);
        return obj.data && obj.data.fullname && obj.data.fullname.length > 10;
    })
    .withMessage('Full Name is required and must be valid.')
    .on(this._user);

ValidationRules
    .ensure('email')
    .required()
    .email()
    .satisfiesRule('alreadyexists')
    .ensure('password')
    .required()
    .ensure('password2')
    .displayName('Password Verification')
    .required()
    .satisfiesRule('passwordmatch')
    .on(this._user);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my fetch getting called twice? : r/reactjs - Reddit
It shows it getting called twice in the console, but only shows once rendered on the page.
Read more >
Why is ngOnInit called twice? - angular - Stack Overflow
Right now, if an error happens during detecting changes of content/view children of a component, ngOnInit will be called twice (seen in DynamicChangeDetector)....
Read more >
The three most important AWS WAF rate-based rules
A URI-specific rule can prevent a single source IP address from connecting to the login page as few as 100 times per 5-minute...
Read more >
502 Bad Gateway Unexpected EOF | Apigee Edge
The HTTP status code 502 means that the client is not receiving a valid response from the backend servers that should actually fulfill...
Read more >
Rule syntax | Semgrep
This document describes Semgrep's YAML rule syntax including required and optional fields. Just getting started with Semgrep rule writing?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found