0

I would like to know how to validate empty object using vuelidate. I tried to give a demonstration on jsfiddle as links follows

Vue.use(window.vuelidate.default)
const { required, minLength } = window.validators

new Vue(
    {
        el: "#app",
        data: {
            companies: [
                {
                    id: 1,
                    name: 'facebook'
                }, 
                {
                    id: 2, 
                    name: 'apple'
                }
            ],
            text: {
                id: null,
                name: null
            }
        },
        validations: {
            text: {
                required
            }
        }
    }
)

jsfiddle

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
agriboz
  • 4,724
  • 4
  • 35
  • 49

3 Answers3

4

$v.text is valid because it is a non-empty object. That means it doesn't have 'falsy' value so it meets the requirement. One way to make it work:

validations: {
    text: {
        id: {required},
        name: {required},
    },
},

JSFiddle

If you don't want to repeat items object structure, you can write a custom validator.

oniondomes
  • 2,034
  • 13
  • 22
1

There is missing information about how to use withParams in the documentation of vuelidate page. So i have searched on its github page and found this link . According to link i came up with that solution

import { withParams } from 'vuelidate'

export const checkIfNull = withParams(
  { type: 'required' },
  value => (value.id === null ? false : true)
)
agriboz
  • 4,724
  • 4
  • 35
  • 49
0

There is nothing special about validating an object, you just need to define the structure and add any validation rules you require.

Please see the example I created and take another look at the Collections docs.

Phunky
  • 481
  • 4
  • 16