Joi: How to allow a string or null?

Created on 12 Dec 2014  路  18Comments  路  Source: sideway/joi

How can I add validation that allows null or a string?

support

Most helpful comment

name: Joi.string().allow(null)

All 18 comments

Actually digging into this further, this is probably correct huh:

name: [Joi.string().optional(), Joi.allow(null)]

name: Joi.string().allow(null)

I'd be nice if this was in the documentation :). The documentation doesn't really talk about how to handle null.

Documentation is opensource, PR welcome.

Hi,

Is there any way to allow null for all optional fields automatically (whitout having to set .allow(null)) everywhere ?

Thanks.

I don't think so, this is very unusual.

What is the point of 'required()' when an optional (ie nullable) field can't exist?

What is the point of 'required()' when an optional (ie nullable) field can't exist?

I'm honestly not sure what you mean by this. "Optional" is both the default behavior and an explicit option, and "nullable" properties are discussed above.

const schema = Joi.object().keys({
    a: Joi.string().required(),
    b: Joi.string(),
    c: Joi.string().optional(),
    d: Joi.string().allow(null).required(),
    e: Joi.string().allow(null)
});

schema.validate({});
// ValidationError: child "a" fails because ["a" is required]
schema.validate({ a: 'a is present' });
// ValidationError: child "d" fails because ["d" is required]
schema.validate({ a: 'a is present', d: 'so is d' });
// error: null

// "d" is required, but is allowed to be null:
schema.validate({ a: 'a is present', d: null });
// error: null

// note that the keys "b", "c", and "e" were all optional. They will only be validated if present:
schema.validate({ a: 'a is present', b: null, d: 'so is d' });
// ValidationError: child "b" fails because ["b" must be a string]
// we did not specify that "b" can be null
schema.validate({ a: 'a is present', c: null, d: 'so is d' });
// ValidationError: child "c" fails because ["c" must be a string]
// same problem as above
schema.validate({ a: 'a is present', d: 'so is d', e: null });
// error: null

// But then all 3 "optional" keys can in fact exist, regardless of being "nullable" or not:
schema.validate({ a: 'a is present', b: 'here', c: 'also here', d: 'so is d', e: 'everybody is here' });
// error: null

What I mean is the difference in the definition of optional: having a property not present vs having property being null. The question is: why is a property that's set to null, not the same as a non existent property?

Because "non existing" in javascript terms strictly means having the value undefined.
A property with the value null is still present, it just has a "missing" value.

null === undefined;
// false

```js
// having property being null:
const obj = {a: null};
obj.hasOwnProperty('a');
// true

// having the property not present:
const obj2 = {};
obj2.hasOwnProperty('a');
// false

Yes, I understand that it's a difference technically, but when I have an object to be validated, I don't care if a property is null or non-existent when I say it's "optional".

Well that's a requirement specific to your needs then.

In my daily work we have lots of properties for which we care a great deal whether they are null or strictly not there.
Another example from my work is when we need the data consistency of keeping our database records in the same shape, but may not care about a particular property when it is "missing". So we make the property required (to maintain shape) but allow it to be null (because we don't strictly need it for that particular record).

I understand. It's a matter of perspective in that case. Thank you for the explanation.

To add my 2 cent, optional means the property can be there or not, at this point it's not checking for value or type just if the property exists or not. Allowing null simply means the property is definitely there but now we are trying to check for the value. Hope this can help somebody.

By the way, as a side note, this will enforce that it must be null:
Joi.valid(null).required()

I would strongly recommend watching Why Not? by Rich Hickey on youtube... delves into clojure.spec

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

Dreamystify picture Dreamystify  路  4Comments

REBELinBLUE picture REBELinBLUE  路  3Comments

sergibondarenko picture sergibondarenko  路  3Comments

a-c-m picture a-c-m  路  3Comments

mohamadresaaa picture mohamadresaaa  路  3Comments