Home > OS >  Vue props validator how to set a default value if dont pass validator
Vue props validator how to set a default value if dont pass validator

Time:06-14

Is there a way to set a default value to a prop with validator if the current prop dont pass the validator, i want to set the value to 1 if it is less than 3, here a exemple.

currentStep: {
      type      : Number,
      required  : true,
      validator : (value) => {
        return value <= 3;
      },
      default   : 1
} 

CodePudding user response:

The validator method always returns a boolean value, it's not used to return any other value, you could use a computed property based on the prop and validate it :

props:{
   currentStep: {
      type      : Number,
      required  : true,
      default   : 1
 },
},
computed:{
  step(){
      return this.currentStep<=3 ? 1 : this.currentStep
  }
}
  • Related