A
A
Artem Prokhorov2021-05-14 13:54:00
Vue.js
Artem Prokhorov, 2021-05-14 13:54:00

How to strictly validate parameters in vue routing?

That is, it doesn't work for me:

props: {
     id: Number.
}


Since vue will just give a warning in the console, but the data will still be passed if it is a string.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya, 2021-05-15
@kotcich

Everything is written in the router dock: https://router.vuejs.org/en/guide/essentials/dynam...

vue-router uses path-to-regexp as an engine to check for route matches, allowing for many advanced features...

We go to the lib's dock and read what it can do ("type" based on the \d+ regular expression): https://github.com/pillarjs/path-to-regexp/tree/v1...

A
Alexey Yarkov, 2021-05-14
@yarkov Vue.js

Props have a validator property. Validate there.

A
Aetae, 2021-05-14
@Aetae

First: it's not at all clear how "strict" validation should look like in your opinion. Crash the app?
Second: this validation is in the component, not in the routing.
Third: any route input parameter is, by definition, a string, because is part of the string - url.
As a result: propsin the route properties it can be a function . In this function, you can add both your own validator and type casting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question