Comments (11)
Isn't it reasonable that empty field is failing on minLength
? I'd say that in such cases it's good to go with custom validation using fn
validator:
validation: ->
name:
fn: -> (value)
value || value.length >= 3
message: 'Name is too short'
Thoughts?
from backbone-validator.
On the other hand you're probably right, it makes sense to run length check only if value is present, which could be controlled via required
from backbone-validator.
Yeah, that was my initial instinctive reaction, however you could make a good case for either, I suppose.
from backbone-validator.
But that means required must be considered in all validations, which I've been trying to wrap my head around in either the Validator.validate
or the Validator._validateAll
function.
from backbone-validator.
I think it's possible just to check whether attribute value is not empty in such attributes without checking for required
. So all string validators (minLength
, maxLength
, format
) will run only if string is not empty.
from backbone-validator.
Hmm but if someone does not have required
specified, you'd still want to run minLength
even if the string is empty...
from backbone-validator.
Yep, in this case you have to add required: true
:)
It's a kind of tricky case to decide how to validate strings here, but using validators only on non-empty strings making it a bit more flexible as you can always define that value should not be empty with required
validator
from backbone-validator.
I see your point. Just because you specified a minLength
doesn't necessarly mean the field should be implicitly required
... and I see why the format
validator has been passing anyway, but the minLength
and maxLength
check that a value is there, so they weren't. : )
from backbone-validator.
Yeah, that's the inconsistency that should be fixed
from backbone-validator.
It's always good to see solutions that take barely any code. Let me take this chance to say thanks for building a small and effective validation plugin, and keeping up with maintenance! : )
from backbone-validator.
👍
from backbone-validator.
Related Issues (20)
- Consider whitespace-only fields to fail `required` validation? HOT 5
- Pass attrs to fn validator HOT 3
- Release? HOT 1
- Support for different message per validator HOT 3
- Add UMD & publish to npm? HOT 4
- Collections validator throws error if collection is undefined or null HOT 1
- Nested Collection & Model validation + casting HOT 1
- Is 'numeric' format validator wrong? HOT 1
- Collection validation error paths HOT 5
- Publish 0.3.0 to npm - also, perhaps automate it? HOT 1
- Validating simple nested properties HOT 4
- call function that performs custom validation on entire model HOT 8
- Use template to generate better error messages
- Access to model from `fn` validation? HOT 1
- Improve format validator that it can accept patterns assigned to specific error messages HOT 1
- Return object error instead of array of strings HOT 1
- Numeric min / max
- _.chain not terminated by value() -- lodash compatibility
- Model.validate() method cannot found HOT 1
- root@localhost is considered an invalid email address
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 backbone-validator.