Validating array items with joi in a Foxx app

677 views Asked by At

I am having problems with using joi array-item validation in foxx applications as well as the arango-shell with arangodb-2.5.5. The joi-documentation suggests to use something like:

var Joi = require('joi');
Joi.array().items({'name': Joi.string().required()});

for validating a dataset similar to:

[{'name': 'Peter'}, {'name': 'Edeltraut'}, ...]

However, using it in a Foxx application results in the application to stop working. Pasting the snippet from above into the arango-shell produces the following output:

JavaScript exception: TypeError: undefined is not a function
!Joi.array().items({'name': Joi.string().required()});
!            ^
stacktrace: TypeError: undefined is not a function
    at <shell command>:1:13

Is there something I am missing, or is arangodb using a modified / smaller version of joi that has this feature stripped out?

2

There are 2 answers

0
Alan Plum On BEST ANSWER

ArangoDB 2.5 uses an older version of joi. The method in question was renamed to items in joi 6.0 and was previously called includes.

You can find the documentation for joi 4.9.0 (the version shipped with ArangoDB 2.5) at https://github.com/hapijs/joi/tree/v4.9.0

ArangoDB 2.6 will ship with joi 6.4.3, which is the latest version at this time.

If you want to know the version of an NPM dependency shipped with ArangoDB, you can also find out the version number by importing its package.json file like this:

require('joi/package.json').version
0
Christian Pekeler On

You don't have to wait for ArangoDB 2.6 to use the latest version of joi. If you npm install joi --save inside of your Foxx app's APP folder, it'll be used instead of the one bundled with ArangoDB. I've been doing that for a while now.