Joi: Match empty array as valid value.

Created on 16 Dec 2016  路  8Comments  路  Source: sideway/joi

Context

  • node version:6.7.0
  • joi version:10.0.6
  • environment (node, browser):
  • used with (hapi, standalone, ...):hapi standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

Attempting to validate some values.

       import {expect} from 'chai';
       import Joi from 'joi';
       expect(Joi.any().strict().valid([
            6,
            "foo",
            [],
            true,
            {
                "foo": 12
            }
        ]).validate([]).error).to.not.exist;

Which result you had ?

AssertionError: expected [ValidationError: "value" must be one of [6, foo, true, [object Object]]] to not exist

What did you expect ?

I expected it to be valid, as [] is specifically listed as a valid value. My guess is it has to do with the flattening of the arguments, flattening the array to deeply.

I tried [[]] but that gets flattened also.

bug

Most helpful comment

@WesTyler Thanks , I wrote a json schema validator plugin for Joi, anyways I needed to validate enums. Enums are deep compared. So fixing this doesn't actually fix my problem. As I ended up implementing an any.eql extension, that takes an optional function for doing deep compares.

Thanks though, this is a bug, i've seen elsewhere though, where args get flattened. So fixing the root of this will fix a lot of real edge cases elsewhere. I'm not blocked, but thanks for the concern!

All 8 comments

Joi is not doing deep equals with valid, you can't expect it to work for arrays and objects, unless you give it the same reference.

Then this should work?
```js
it('should match empty array', ()=>{
const empty = [];
expect(Joi.any().strict().valid([
6,
"foo",
empty,
true,
{
"foo": 12
}
]).validate(empty).error).to.not.exist;
});

but it fails

AssertionError: expected [ValidationError: "value" must be one of [6, foo, true, [object Object]]] to not exist

```

It is indeed flattened by hoek, it could be cleverer but it's not a big priority. In the meantime you should be making your assertions with actual schemas for those cases. Something like :

Joi.validate(empty, [ // <- this is converted to a Joi.alternatives() by Joi.validate
    6,
    "foo",
    Joi.array().length(0),
    true,
    {
        "foo": 12
    }
], { 
    presence: 'required', // everything is required by default
    convert: false // == strict()
})

I'll requalify this as a bug but I don't think I'll be able to work on it anytime soon.

@jspears what is it that you are hoping to accomplish with this assertion? You may have better luck finding a more "official" way of getting to your end goal faster than waiting for this bug to be addressed. (I'll try to take a stab this weekend but make no promises)

It seems like you ran into this bug by trying to get Joi to do something in a way it wasn't exactly intended for.

@WesTyler Thanks , I wrote a json schema validator plugin for Joi, anyways I needed to validate enums. Enums are deep compared. So fixing this doesn't actually fix my problem. As I ended up implementing an any.eql extension, that takes an optional function for doing deep compares.

Thanks though, this is a bug, i've seen elsewhere though, where args get flattened. So fixing the root of this will fix a lot of real edge cases elsewhere. I'm not blocked, but thanks for the concern!

Replaced by #1644

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashrafkm picture ashrafkm  路  3Comments

PaunPrashant picture PaunPrashant  路  3Comments

mohamadresaaa picture mohamadresaaa  路  3Comments

longweiquan picture longweiquan  路  3Comments

n-sviridenko picture n-sviridenko  路  3Comments