Code Monkey home page Code Monkey logo

Comments (11)

fantactuka avatar fantactuka commented on August 15, 2024

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.

fantactuka avatar fantactuka commented on August 15, 2024

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.

wulftone avatar wulftone commented on August 15, 2024

Yeah, that was my initial instinctive reaction, however you could make a good case for either, I suppose.

from backbone-validator.

wulftone avatar wulftone commented on August 15, 2024

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.

fantactuka avatar fantactuka commented on August 15, 2024

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.

wulftone avatar wulftone commented on August 15, 2024

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.

fantactuka avatar fantactuka commented on August 15, 2024

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.

wulftone avatar wulftone commented on August 15, 2024

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.

fantactuka avatar fantactuka commented on August 15, 2024

Yeah, that's the inconsistency that should be fixed

from backbone-validator.

wulftone avatar wulftone commented on August 15, 2024

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.

fantactuka avatar fantactuka commented on August 15, 2024

👍

from backbone-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.