Skip to content

custom validation does not run inside shape #176

@davidbeijinho

Description

@davidbeijinho

I was not able to make custom validation run inside of a shape validator, I have modified the example for vue2 slightly to create an example for this problem

import Vue from 'vue2'
import VueTypes from 'vue-types'
import { PropValidator } from 'vue2/types/options'

Vue.config.silent = false

interface ModelItem {
  id: string
  pet: 'dog' | 'cat'
  isNew: boolean
  modelname: any
  modelprop: any
}

function minLength(value: any) {
  return typeof value === 'string' && value.length >= 6
}

const Model = Vue.extend({
  template: '<li>{{ model.id }} {{ isNew }} (pet: {{ model.pet }})</li>',
  props: {
    model: VueTypes.shape<ModelItem>({
      id: VueTypes.string.isRequired,
      pet: VueTypes.oneOf(['dog', 'cat'] as const).isRequired,
      isNew: VueTypes.bool,
      modelname: VueTypes.custom(minLength).isRequired,
      modelprop: VueTypes.custom<string>(minLength).isRequired,
    }).isRequired as PropValidator<ModelItem>,
    username: VueTypes.custom(minLength).isRequired,
    theprop: VueTypes.custom<string>(minLength).isRequired,
  },
  computed: {
    isNew(): string {
      return this.model.isNew ? '- new' : ''
    },
  },
})

const App = Vue.extend({
  template: `
    <section>
      <h1>A list of models</h1>
      <button type="button" @click="addPet('parrot')">Add parrot (invalid, logs to console)</button>
      <button type="button" @click="addPet('dog')">Add dog</button>
      <ul><Model v-for="model in models" :model="model" :key="model.id" :username="'aaaaaaaa'" :theprop="'aaaaaaaa'" /></ul>
    </section>
  `,
  data: () => ({
    models: [] as ModelItem[],
  }),
  components: {
    Model,
  },
  methods: {
    addPet(pet: any) {
      const newId = 'model-' + this.models.length
      this.models.forEach((model) => {
        model.isNew = false
      })
      this.models.push({
        id: newId,
        pet,
        isNew: true,
        modelname: false,
        modelprop: {},
      })
    },
  },
})

new Vue({ render: (h) => h(App) }).$mount('#app')

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions