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.

How to make mock/make it optional?

See original GitHub issue

I integrated Vee-Validate in my Form-Group component, but if the consumer doesn’t use VV, I need a graceful degrade.

Figured I can do this:

inject: {
  $validator: {
    from: '$validator',
    default() {
      return {}
    }
  }
}

Edit 2: Scrap that, it has to be more empiric:

created() {
  if (this.$root.$validator) {
    this.$validator = this.$root.$validator
  } else {
    this.$validator = {}
  }
}

But I still have to deal with the missing directive exception.

Edit 3: Ended up with this:

// Resources
import VeeValidate from 'vee-validate'

// Implementation
export default {
  directives: {
    validate: {
      ...VeeValidate.directive
    }
  },
  data() {
    return {
      $validator: {}
    }
  },
  created() {
    if (this.$root.$validator) {
      this.$validator = this.$root.$validator
    } else {
      this.$validator = new VeeValidate.Validator()
    }
  },
  computed: {
    errors() {
      return this.$validator.errors
    }
  }
}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
logaretmcommented, Dec 28, 2017

Yes you can, vee-validate supports custom component validation and v-model directive if used on the same element node. here is a basic implementation:

// Input.vue

<template lang="pug">
  .form-field
    label.u-label {{ label }}
    .input.is-rounded.is-white(:class="{ 'has-error': error }")
      input(:type="type" :value="value" ref="input" :name="name" @input="$emit('input', $event.target.value)" @blur="$emit('blur')" @change="$emit('change')")
    span.input-message(v-show="error") {{ error }}

</template>

<script>
export default {
  $_veeValidate: {
    value () {
      // the current value is the current value of the input element
      return this.$refs.input.value; 
    },
    name () {
      // use the component name prop as the field name
      return this.name;
    },
    events: 'change' // validate only on change for this component
  },
  name: 'bi-input',
  props: {
    label: {
      type: String,
      required: true
    },
    name: {
      type: String
    },
    error: {
      type: String
    },
    value: {
      type: null
    },
    type: {
      type: String,
      default: 'text',
      validator: (val) => ['text', 'password', 'tel', 'url', 'email'].includes(val)
    }
  }
};
</script>

Now you can use it like this:

<template lang="pug">
 // .... 
      ox-input(
        name="firstName"
        label="First Name"
        v-model="formData.firstName"
        data-vv-as="First Name"
        v-validate="'required'"
        :error="errors.first('firstName')"
      )
</template>

<script>
import BiInput from '~/components/Input';

export default {
  name: 'user-form',
  components: {
    BiInput 
  }
};
</script>
0reactions
adi518commented, Dec 28, 2017

Good point. In my case, I’m developing an internal components library, so I actually want to have my validation implementation centralized and under control.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make a mock return 'false' for 'Optional.isPresent()'?
If you just want an Optional returning false for isPresent() , you don't need to mock the Optional at all but just create...
Read more >
Make it easier to to mock java.lang.Optional as return value of ...
When you do extensive testing/mocking on some APIs which return Optionals on you end up writing somehting like Mockito.when(foo.getBaa()).
Read more >
How to mock repository findById thenReturn() Optional?
Try to mock a repository findById method, but no idea use thenReturn() to return an object, as it accepts an Optional ?
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
To create a spy, you need to call Mockito's static method spy() and pass it an instance to spy on. Calling methods of...
Read more >
Create hydrated mock | TS auto mock
Do you need to mock optional properties or union types that may be undefined? Say hello to createHydratedMock, it will help you create...
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