question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty Array [] won't pass to custom validator

See original GitHub issue
Validator.register('fail_array', function(value, requirement, attribute) { 
    return false;
}, ':attribute Failed');
var fields = {attr: []}
var rules = {attr: 'fail_array'}
new Validator(fields, rules);

This validation will won’t fail. There is use case that I want to check an array should include a value, empty array should fail as well.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
garygreencommented, Sep 15, 2016
var Validator = require('./src/validator');

var validator = new Validator({ users: [] }, { users: 'array|min:1' });
console.log(validator.passes()); // true

In Laravel the above fails the validation rule, this seems like a bug.

0reactions
dcworldwidecommented, Jan 16, 2020

I require it… because I need to write code that handles soft deletes, like the following:

Validator.register(
    'requiredArray',
    (value: Vm<any>[], requirement, attribute) => {
        console.log({ value, requirement, attribute })
        return value &&
            value.length > 0 &&
            value.filter(x => x.changeStatus != ChangeStatus.MARKED_FOR_DELETION).length > 0
    },
    'At least 1 :attribute is required'
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Empty Array [] won't pass to custom validator #131 - GitHub
This validation will won't fail. There is use case that I want to check an array should include a value, empty array should...
Read more >
Angular FormArray custom validator not firing - Stack Overflow
I want my FormArray to be able to know whether or not any of the FormControls have an email validation error, and if...
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
The custom validator Closure receives four arguments: the name of the $attribute being validated, the $value of the attribute, an array of $parameters...
Read more >
validate.js
If you don't want to give any options to a validator you may pass true instead of an empty object. The validator will...
Read more >
Custom Rules - VeeValidate
You can recieve an object instead of an array for your validation rule by providing a paramNames array in the extend options (third...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found