Comments (9)
Your proposal would require to add a validator registration step. What {minLength: 4}
means on its own? Current API relies on the fact that all validators are simple functions that you can simply import and all labels in the object are just validation names. It allows you to use any validator functions from any library you want or even use same validator multiple times with different settings.
Take that as an example:
name: {
required,
barelyTooShort: minLength(6),
minLength: minLength(5)
}
Now you can use two separate error messages and display to the user something like
Just one more character
span.form-group__message(v-if="!$v.name.required") Name is required
span.form-group__message(v-else-if="!$v.name.minLength") Name is too short
span.form-group__message(v-else-if="!$v.name.barelyTooShort") Just one more character
from vuelidate.
I would like to avoid a need of additional registration in any form. You don't even have to Vue.install
this plugin, it supports being used as a standalone mixin, for those who want to import it directly in every component that needs it ;)
Explicit imports on every point of usage are a good thing for you. That way any validator you don't use won't be compiled into your app's bundle and you don't have to remember to cleanup or add new dependency somewhere in the configuration. It's also much easier and faster to get started.
You usually want to avoid importing the whole set of validators anyway. Just do this with any bundler that supports tree-shaking and you are both concise and safe.
import { required, minLength } from 'vuelidate/lib/validators'
from vuelidate.
@JounQin My point is – we want the validators to not affect the total bundle size. If we would include all of them now – that’s not a problem as there are just a few of them. However, as we plan to expand the validators (and utils) significantly that would very soon become an antipattern in terms of bundle size. Thus encouraging people to register everything up front would make it worse for the users. So if we can avoid a suboptimal solution then I guess we should.
Also, this would introduce a different way to declare validators which could potentially confuse less experienced people. As it is right now – I guess – it’s pretty straightforward, as validators are just functions. No magic inside.
Anyway, congrats on the v-validator! 👍
from vuelidate.
There is no conflict.
If the value of validations is function, call it, if not, pass the value to the exist validator with same name of the key.
And also, we should be able to extend global validators by ourselves.
name: {
required: true,
barelyTooShort(val) {
return minLength(6)(val)
},
// `barelyTooShort: minLength(6)` is also fine
minLength: 5,
between: [2,3] // which will call `between(2,3)` inside
}
Besides, for now there is no error message ecosystem, I prefer an API like jquery-validate
.
{
...other,
validations: {
rules:{}, // if there is no key named `rules`, treat the whole validator object as rules.
messages:{}
}
}
from vuelidate.
@JounQin This issue is being discussed here #3
from vuelidate.
@JounQin You still forgot about one detail.
If the value of validations is function, call it, if not, pass the value to the exist validator with same name of the key.
Vuelidate doesn't know anything about validators. It's totally decoupled. We have no internal list of names pointing to specific functions. Having that would defeat the purpose, as now you have to register all of those somehow. Moreover, adding all builtin validators to a list like that would force us to import all of them with the core library. What's the purpose? Current syntax leaves that decoupled. You have to import and pass the function. Simple as that.
If you don't like the need to specify the label of the object, you can always prepare the function beforehand.
import minLength from 'vuelidate/lib/validators/minLength'
const nameMinLength = minLength(6)
export default {
data () {
name: ''
},
validators: {
name: {
required,
nameMinLength
}
}
}
You can even prepare a few common validators like that in separate module and import them directly.
from vuelidate.
We can provide a few or none built-in validators, but the user can imports their needed validators when they install the plugin at the first time globally.
import Vuelidate from 'vuelidate'
import validators from 'vuelidate/lib/validators'
Vue.use(Vuelidate, {
validators
})
// in Vuelidate
const validators = {}
export default {
install(Vue, options = {}) {
Object.assign(validators, options.validators)
// ... then if calling validator is function, call it, else try the same named validator in validators.
}
}
My point is that, for now we must import every validator by ourselves in every component, it is too long-winded and unnecessary. There will be a lot of duplicate codes of validators.
Maybe I am too lazy. And my purpose is to write less code but do more. :)
This is also why we are looking for a third-party plugin to validate.
from vuelidate.
What @Frizi said actually ^
from vuelidate.
I've said it is OK to use the original grammar in my scenario, but for the other people, they can try another experience.
You usually want to avoid importing the whole set of validators anyway. Just do this with any bundler that supports tree-shaking and you are both concise and safe.
Of course I know that, but it's so stupid to write something like import { required, minLength } from 'vuelidate/lib/validators'
everywhere for me what means it will add a few bytes in every chunk file.
Actually it just needs to add a few lines(less than 10) in the core code what will make it possible and short the API.
Anyway, I've finished a very early alpha version of v-validator which is totally under my control. :)
from vuelidate.
Related Issues (20)
- $validationGroups: there's not a way to group errors from nested forms.
- useVuelidate doesn't signal that it unwraps the validation ref when used in a template HOT 2
- sameAs doesn't work HOT 10
- Support ref (and possibly computed) HOT 2
- Not supporting this package in latest vue3 with typescript HOT 3
- InternalError: too much recursion: access validation states from a validation HOT 1
- How does i18n work when validators are chained?
- Validation inconsistent on Mobile Devices HOT 2
- Support Vue 3.x HOT 1
- a way to programatically set form/$model value without triggering validators
- root instance does not capture nested instances validations results if they have `$rewardEarly: true` and root does not.
- Getting path of validation error when validation fails in child component
- nuxt 3, $each helper does not work?
- $externalResults are not automatically cleared on $model change when using dynamic/computed rules HOT 1
- lazy validation option doesn't work HOT 1
- useVuelidate doesnt work in options API
- $rewardEarly stops working if a previous field is invalid HOT 1
- Vuelidate - foreach helper validation does not include $dirty property
- BUG: base-validation type is inferred to undefined
- Vuelidate not working when computed list is provided HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuelidate.