B
B
bormor2018-06-26 13:24:31
Vue.js
bormor, 2018-06-26 13:24:31

Vue - object validation in props?

I'm looking for the same visual way as in React
. For example, checking that myObject is an object from {color: ... fontSize: ...},

myObject: PropTypes.shape({
    color: PropTypes.string,
    fontSize: PropTypes.number
  })

Is there a similarly visual way for Vue?
Validating an object via a custom function from here doesn't seem like the most obvious way.
Example:
validator(obj) {
          return (obj.id && Number.isInteger(obj.id) && obj.name && obj.name.length );
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-06-26
@bormor

Validation of input parameters
Validation of UPDRemoved from comments.

I saw the solution through the validator function, but it does not seem optimal to me for objects. At least in React, it's much shorter and cleaner.
Is there an equivalent in Vue?

Not out of the box (it should be noted that in React too - as of version 15.5 React.PropTypes is deprecated and prop-types should be used instead ). You can make a class and pass a constructor to props to check that the passed parameter is an instance of the class. Or use third-party libraries, for example - there is an analogue of PropTypes.shape .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question