validate.js icon indicating copy to clipboard operation
validate.js copied to clipboard

Better support for arrays

Open ansman opened this issue 11 years ago • 3 comments

This would be very useful for validating input from checkbox for example where you can choose more than one alternative.

The length validator already works with arrays (though the default message is for strings) but perhaps inclusion should (optionally?) support arrays saying that each item in the array must be in the inclusion list.

Another useful thing to validate would be uniqueness of the items in the array to make sure you haven't picked the same item twice.

ansman avatar Oct 05 '14 13:10 ansman

This would be particularly useful for me at the moment. I am trying to figure out the best way to validate all child objects inside an array.

I have an object that looks something like this:

{
  jobs:[
    {
      key: value
    },
    {
      key: value,
      anotherKey: 'anotherValue'
    },
    {
      invalidKey: false
    }
  ]
}

Just brainstorming syntax I was figuring something like this...

jobs: {
  presence: true,
  length: {
    minimum: 1,
    maximum: 5
  }
  // maybe this only supports arrays?
  each: {
    key: {
      presence: {
        message: 'is required!'
      }
    },
    anotherKey: {
      datetime: true
    }
  }
}

Also clean attributes would have to be updated to toss invalidKey from the 3rd item in the job object as it is not defined in the constraints under each.

Thoughts?

am17torres avatar Nov 03 '15 16:11 am17torres

Any idea when this is going out? Must say this is much needed feature.

activebiz avatar Jul 05 '18 19:07 activebiz

You could do something like this: https://stackoverflow.com/a/51406790/2847302

paulevmo avatar Jul 19 '18 15:07 paulevmo