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.

Validations Lifecycle

See original GitHub issue

Hello 😃

Basically, I have a form control that has synchronous validators and asynchronous.

  validations: {
    form: {
      username: {
        required, // sync validator
        min: minLength(6), // sync validator
        uniqueUsername, // async validator
      },
    },
  },

The problem is that even if the synchronous ones already triggered an error the asynchronous ones still executes.

The expected behavior, considering performance issues due to unnecessary API calls, would be to execute the asynchronous validators only if the form has no errors coming from the synchronous validators.

I think that’s the Angular’s forms module behavior.

Is there a way to work around this problem? Is this a nice feature to add to the library?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
lgmf-daitancommented, Sep 3, 2020

I’ve updated my code based on the suggestion

const validationService = {
  async validate(field, value) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          valid: typeof value === "string" && value.length % 2 !== 0
        });
      }, 350 + Math.random() * 300);
    });
  }
};

const hasSyncError = (form, field, syncValidators = []) => {
  return syncValidators.some((syncValidator) => !form[field][syncValidator]);
};

export default function createAsyncValidatorFor(field, { syncValidators }) {
  return async function asyncValidator(value) {
    if (!value || hasSyncError(this.$v.form, field, syncValidators)) {
      return true;
    }

    const { valid } = await validationService.validate(field, value);
    return valid;
  };
}
validations: {
    form: {
      username: {
        required,
        min: minLength(6),
        uniqueUsername: createAsyncValidatorFor("username", {
          syncValidators: ["required", "min"]
        })
      }
    }
  }

And know its working like a charm (https://codesandbox.io/s/gallant-mendel-qdoli?file=/src/components/Form.vue:780-1006)

2reactions
rubens-lopescommented, Sep 2, 2020

Based upon @shentao answer I come up with this, that worked for my case.

  validations: {
    form: {
      username: {
        required, // sync validator
        min: minLength(6), // sync validator
        uniqueUsername: async () => {
            if (!this.syncValidations) return true;

            return await uniqueUsername();
        }
      },
    },
  },
  computed: {
    syncValidations() {
        return this.$v.form.username.required && this.$v.form.username.min;
      }
  }

To avoid infinite loop we have to check each sync validation below username to avoid recursion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation life cycle | CROS - European Commission
The data validation life cycle involves the activities directly linked to each statistical domain for the definition and execution of data validation.
Read more >
Understanding the validator lifecycle - Attestant
This article examines the validator lifecycle in depth, showing what happens in each state and transition, what triggers transitions, ...
Read more >
What's lifecycle validation? - Oracle Help Center
Lifecycle validation enforces compatible lifecycle phases between parent and component items in an item structure, at the structure name level.
Read more >
The validation life cycle - PubMed
The Validation Life Cycle is an implementation mechanism which can assist pharmaceutical (and other types of medical product) manufacturers in the ...
Read more >
Validation Life Cycle Support - Verista
Validation Lifecycle Support ... In fact, continued process verification is a requirement and expectation of validation per FDA requirements and guidance.
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